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.
-
Go back to Design mode for the web form and select the DataGrid.
-
In the Properties window, click on the little lightning bolt to get its Events list.
-
Click the empty listbox for the ItemDataBound event, and choose the DataGrid1_ItemDataBound method as the handler.
-
Do the same for the SortCommand, choosing the DataGrid1_SortCommand method.
-
In the Solution Explorer, choose your new page as the start page, rebuild, and run the page.