The function of the GridView to respond to users

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.