The GridView control is used to browse database data on the web page. There are many ways to make the GridView control have good convenient functions, making it easy for users to use. Example: Use the value of one field in the GridView as a hyperlink connecting to another webpage. Here, we will explain how it is implemented.
To complete the above mentioned functions, you must first know the functions provided by the two asp.net. It is GridView_RowDataBound (object sender, GridViewRowEventArgs e ). When DataBind is called to bind data to the GridView, The RowDataBound event is triggered by the GridView. This event calls GridView_RowDataBound (object sender, GridViewRowEventArgs e) to process data binding operations. Therefore, the hyperlink operation can be added at this time. Next we will introduce another function, which is the Eval function in DataBinder. The Eval function can easily obtain the values of specific fields in the bound data source. For example: DataBinder. Eval (e. Row. DataItem, "the field you want to obtain the value"). ToString ();
Now, we have started to implement the function. First, add the GirdView control to the front-end code. As shown below:
<Asp: GridView ID = "gvView"...>
<Columns>
<Asp: BoundField DataField = "..." HeaderText = "..."/>
...
<ItemTemplate>
<Asp: LinkButton ID = "lnkType" runat = "server"/> // This is the key to linking.
</ItemTemplate>
</Columns>
</Asp: GridView>
Then, after binding the background code using the DataBind function, note that the RowDataBound logic is written.
Protected void GridView_RowDataBound (object sender, GridViewRowEventArgs e)
{
LinkButton objlnkType = null // This is the object used to obtain the front-end LinkButton
String strlnkTypeScript = ""
If (e. Row. RowType = DataControlRowType. DataRow)
{
ObjlnkType = (LinkButton) e. Row. FindControl ("lnkType"); // obtain the front-end LinkButton: lnkType
StrlnkTypeScript = string. Format ("DisplayTransaction ()"); // defines the client javascript
ObjlnkType. OnClientClick = strlnkTypeScript; // assign Javascript to OnClientClick.
}
}
In this way, when you browse the Web page, click the LinkButton in the GridView to execute the defined javascript. As long as javascript is well defined, many functions can be achieved. For example, the hyperlink function mentioned above.
As long as you know more about e. Row. RowType, you can do more. For example, the full row response user. The method is to use e. Row. Attributes. Add ("OnClick", "javascript javascript") to respond to the javascript called by the user.