Override the Standard Status Bar Message of the DataGrid
page 3 of 5
by Steven Archibald
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 23405/ 87

The Code

First, I present the .aspx code that we'll use for the demonstration. I suggest you simply create a new web form in a sample project, and leave it empty. Switch Visual Studio to HTML view, and copy and paste the code in Listing 1 between the opening and closing server-side <form> tags:

Listing 1: The Web Form Code

Test standard anchor:<br>
<a id="tstanchor" runat="server" href="This is the href text"
    onmouseover="self.status='mouseover';return true;"
    onmousedown="self.status='mousedown';return true;"
    onmouseout="self.status='';return true;"
    onmouseup="self.status='';return true;">Test this link</a>
<br>
Test ASP.NET LinkButton<br>
<asp:LinkButton id="LinkButton1" runat="server">LinkButton</asp:LinkButton>
<br>
Test Datagrid Status bar messaging
<br>
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False"
    AllowSorting="True" allowpaging="True">
    <Columns>
        <asp:BoundColumn DataField="col1" HeaderText="Col 1" 
             SortExpression="col1" />
        <asp:BoundColumn DataField="col2" HeaderText="Col 2" 
             SortExpression="col2" />
        <asp:BoundColumn DataField="col3" HeaderText="Col 3" 
             SortExpression="col3" />
    </Columns>
</asp:datagrid>

Switch Visual Studio to Design mode. This will automatically populate the controls in your code-behind. You will see on your page that you have a standard anchor element, an ASP.NET LinkButton control, and an ASP.NET DataGrid control with sortable columns. I've cleverly named the columns and sort expressions "col1," "col2," and "col3." Fortunately, for the purpose of easy maintenance, I applied those names to the first, second, and third columns respectively (I've actually seen examples in the real world where folks weren't that simple-minded). Ten years from now, people will marvel at the prescience of my coding abilities.

Now, for the code-behind file. Listing 2 provides the Page_Load method and utility methods needed for the sample page that we are building.

Listing 2 (the code-behind code):

private void Page_Load(object sender, System.EventArgs e)
{
    // Put user code to initialize the page here
    if (!IsPostBack)
    {
        DataSet mDS = MakeTestData3By3();
        DataGrid1.DataSource    = mDS;
        DataGrid1.DataKeyField  = "col1";
        DataGrid1.DataBind();
    }
 }
private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Header)
    {
        SetWebUICtrlStsBarMsg("Sort by Col1", e.Item.Controls[0].Controls[0]);
        SetWebUICtrlStsBarMsg("Sort by Col2", e.Item.Controls[1].Controls[0]);
    }
}
private void DataGrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
{
    string mSortText;
    mSortText = e.SortExpression;
    Response.Write("You selected: " + mSortText);
}
private DataSet MakeTestData3By3()
{
    DataSet mDS = new DataSet();
    DataTable   mTbl = new DataTable("TestTable3By3");
    mTbl.Columns.Add(new DataColumn("col1", System.Type.GetType("System.String")));
    mTbl.Columns.Add(new DataColumn("col2", System.Type.GetType("System.String")));
    mTbl.Columns.Add(new DataColumn("col3", System.Type.GetType("System.String")));
    DataRow mRow1 = mTbl.NewRow();
    mRow1["col1"] = "data_1_1";
    mRow1["col2"] = "data_1_2";
    mRow1["col3"] = "data_1_3";
    mTbl.Rows.Add(mRow1);
 
    DataRow mRow2 = mTbl.NewRow();
    mRow2["col1"] = "data_2_1";
    mRow2["col2"] = "data_2_2";
    mRow2["col3"] = "data_2_3";
    mTbl.Rows.Add(mRow2);
 
    DataRow mRow3 = mTbl.NewRow();
    mRow3["col1"] = "data_3_1";
    mRow3["col2"] = "data_3_2";
    mRow3["col3"] = "data_3_3";
    mTbl.Rows.Add(mRow3);
 
    mTbl.AcceptChanges();
    mDS.Tables.Add(mTbl);
    return mDS;
}
private  void SetWebUICtrlStsBarMsg(string Message, Control mSrcControl) 
{
    System.Web.UI.WebControls.WebControl SrcControl = 
        (System.Web.UI.WebControls.WebControl) mSrcControl;
    string mMouseDownOver = "self.status='" + Message + "';return true;" ;
    string mMouseOutUp    = "self.status='';return true;" ;
    SrcControl.Attributes.Add("onmouseout"  , mMouseOutUp);
    SrcControl.Attributes.Add("onmouseup"   , mMouseOutUp);
    SrcControl.Attributes.Add("onMouseover" , mMouseDownOver);
    SrcControl.Attributes.Add("onMousedown" , mMouseDownOver);
    SrcControl.Attributes.Add("onClick"     , mMouseDownOver);
}

In my teeny-weeny universe, the last two methods, MakeTestData3By3 and SetWebUICtrlStsBarMsg, are in a separate utility class. I've included them as part of the code-behind file to make things as easy as possible for the reader.

Finally, the last steps are to hook up the web form to the code-behind so your event handlers will be called, and then build this little puppy.

  1. Go back to Design mode for the web form and select the DataGrid.
  2. In the Properties window, click on the little lightning bolt to get its Events list.
  3. Click the empty listbox for the ItemDataBound event, and choose the DataGrid1_ItemDataBound method as the handler.
  4. Do the same for the SortCommand, choosing the DataGrid1_SortCommand method.
  5. In the Solution Explorer, choose your new page as the start page, rebuild, and run the page.

View Entire Article

User Comments

Title: Solution to 'Unpreventable' standard behavior   
Name: Your Daddy
Date: 2007-01-04 12:30:26 PM
Comment:
The javascript "onfocus" event for the anchor element can be used to get rid of that pesky status bar message that displays when you hold the left mouse button down. In addition to using onmouseover, onmousedown, onmouseup, onmouseout, etc, add an onfocus attribute to the anchor element.

Cheers,

Your Daddy
Title: self.status doesn't work in Firefox   
Name: slolife
Date: 2005-10-17 7:53:59 PM
Comment:
self.status='Hello' doesn't work in Firefox. Is this a security/anti phishing thing?
Title: Mr   
Name: Abdul Salam
Date: 2005-10-12 12:23:38 AM
Comment:
very good Thanks
Title: Link is always visible   
Name: kraftspl
Date: 2005-09-23 6:45:18 PM
Comment:
Nice work, but the referenced link is always visible in the status bar, when you click a link. You will furthermore see a status message.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-29 9:50:27 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search