The function of GridView in response to user

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Browse the database data will be used in the GridView control in the webpage. There are many methods can make the GridView control has good convenience function, make the user feel very convenient. An example: let one of GridView field values as hyperlinks to another webpage. Here to explain its realization is how.


To complete the above functions must first know the function of two asp.net to provide. It is GridView_RowDataBound(object sender, GridViewRowEventArgs e). In the call to DataBind when the data bound to GridView, GridView will trigger the RowDataBound event. The event will be called GridView_RowDataBound (object sender, GridViewRowEventArgs E) to deal with data binding time of operation. So the hyperlink can operate at this time. Next we introduce another function is Eval function in DataBinder. The Eval function can be very convenient to obtain the specific field of the bound data source inside the value. For example: DataBinder.Eval (e.Row.DataItem, "you want to get the value of the field").ToString();


Now it is beginning to realize the function of. First of all the code with the GirdView control. As shown below:

<asp:GridView ID="gvView" ...>

<Columns>

<asp:BoundField DataField="..." HeaderText="..."/>

...

<ItemTemplate>

<asp:LinkButton ID="lnkType" runat="server"/>

//This is the key link to do

</ItemTemplate>

</Columns>

</asp:GridView>


Then the code using the DataBind function after binding note is writing the RowDataBound logic

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)

{

LinkButton objlnkType = null

//This is used to obtain the LinkButton object

string strlnkTypeScript = ""


if (e.Row.RowType == DataControlRowType.DataRow)

{

objlnkType = (LinkButton)e.Row.FindControl("lnkType");

//Gets the LinkButton: lnkType

strlnkTypeScript = string.Format("DisplayTransaction()");

//Define client JavaScript

objlnkType.OnClientClick = strlnkTypeScript;

//Will Javascript give to OnClientClick

}

}

So when browsing webpage click inside the GridView LinkButton will perform well defined javascript. As long as the definition of how JavaScript can achieve many functions. For example the hyperlink function.  

As long as to better understand the e.Row.RowType actually can do more things. For example, the response to the user. The practice is to use the e.Row.Attributes.Add ("OnClick", "defined JavaScript") can achieve full line in response to a user calls to JavaScript.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Nicholas at November 16, 2013 - 8:02 PM