Application of several buttons in the GridView

Source: Internet
Author: User

There are three ways to add a button in the GridView, CommandField, ButtonField, TemplateField plus button in three ways. The same functionality can be achieved in three ways, but the implementation method is different when implementing certain functions, let's introduce:

First, get a field value for the selected row

1. Add button in the template and use CommandArgument to bind a field in the database.

A, first, the template button is set CommandName to a value such as "Selectid", binding commandargument to a field of the database, commandargument= ' <%# Bind ("SHQXDJ")% > '.

Then, in the source of the page, find the GridView code, add the program name onrowcommand= "Gridview1_rowcommand". Allows the page to find the name of the executing program.

<asp:gridview id= "GridView1" runat= "Server" datakeynames= "pid" datasourceid= "SqlDataSource1" onrowcommand= " Gridview1_rowcommand ">

Then in the background CS code added:

protected void Gridview1_rowcommand (object sender, Gridviewcommandeventargs e)

{

if (E.commandname = = "Selectid")

{

Label1.Text = E.commandargument.tostring ();

}

}

B, the previous method is through the commandname of the button, in the GridView in the Rowcommand to execute the command, which is the same form as ButtonField, the button in the template has another way to execute the click Command, is to write the Button_Click event directly, just like the normal button.

<asp:templatefield headertext= "user category Selection" > <ItemTemplate> <asp:label id= "LBYHDJ" runat= "Server" text= ' <%# Bind ("SHQXMC")%> ' width= "45px" ></asp:Label> <asp:button id= "Btnxzyh" runat= "Server" font-size= "9pt" text= "Select specific user" onclick= "btnxzyh_click" commandargument= ' <%# Bind ("Shqxdj")%> '/> </ItemTemplate> </asp:TemplateField>

protected void Btnxzyh_click (object sender, EventArgs e) {this. Txttest.text = ((Button) sender). Commandargument.tostring ();

}

2, you can first get the index value of the current row of the button, and then take the value of a column of the current row according to the index value, or the current row DataKeys to get the data.

A. Add CommandName property in ButtonField control

ASPX page:

<asp:buttonfield buttontype= "button" headertext= "unsubscribe" text= "unsubscribe" commandname= "BTCXTBM"/>

CS Page:

protected void Gvxkall_rowcommand (object sender, Gridviewcommandeventargs e) {int rowIndex = Int32.Parse ((String) e.commandargument);if (E.commandname = = "Bttkall") {string m_whfs, m_xsxh, M_xq, M_LBDM, m_message; int m_kcxh; M_WHFS = "Credit System elective course"; m_xq = (st Ring) viewstate["XKXQ"]; M_xsxh = (string) viewstate["Xsxh"]; m_kcxh = 0;M_LBDM = Gvxkall.datakeys[rowindex]. Values[0]. ToString ();sqlparameter[] Parameters = new Sqlparameter[5]; Parameters[0] = new SqlParameter ("@_whfs", M_WHFS); PARAMETERS[1] = new SqlParameter ("@_xh", m_xsxh); PARAMETERS[2] = new SqlParameter ("@_xq", M_XQ); PARAMETERS[3] = new SqlParameter ("@_lbdm", M_LBDM); PARAMETERS[4] = new SqlParameter ("@_kcxh", m_kcxh); DataSet DSALLTK = dbhelpersql.runprocedure ("Pr_gx_ty_tx", Parameters, "ALLTKMP"); M_message = DSALLTK. tables["ALLTKMP"]. rows[0]["message"]. ToString (); This. Response.Write ("<script language=javascript>alert ('" + M_message + "); </script> "); Bindview6gvxkall (); } }

Note: 1. Note here that only the ButtonField control is in the Gvxkall_rowcommand (object sender, Gridviewcommandeventargs e) event,E. The index value of the current row is not recorded in CommandArgument, and if the button in the template does not automatically record the index value of the current row, you can only manually add the CommandArgument property to the button in the template as previously written in the 1th.

If you want to click the button in the template column, you want to get the rowindex of the current line just need:

1.RowDataBound, set the line number to the property of the button: BTN. attributes["id"] = (e.row.rowindex) 2.gridview inside button click, execute Rowcommand

In this event, E, convert to a button, get (e as button). attributes["id"]

2, if the HeaderTemplate or FooterTemplate add button, if you do not give this button add CommandArgument attribute, because click on it also to trigger the GridView Rowcommand event, Because there is an int rowIndex = Int32.Parse (String) in the Rowcommand event, this sentence is not in the button CommandArgument attribute will be error, the solution is to give the Button CommandArgument property, and is an int type, such as <FooterTemplate> <asp:button id= " Btnshjbxg "runat=" Server "onclick=" Btnshjbxg_click "text=" Modify audit level commandargument= "0" /> </ Footertemplate>

This can be solved, in fact, can also be int rowIndex = Int32.Parse (String) e.commandargument This sentence in each if judgment inside, so do not start to execute it , will not error .

For uniform formatting, it is easy to see that all the buttons in the GridView Add the CommandName property and then write the button-click Command in the Rowcommand event without having to write the Button_Click event again.

protected void Gvshjb_rowcommand (object sender, Gridviewcommandeventargs e) {int rowIndex = Int32.Parse ((String) E. CommandArgument); if (e.commandname = = "Xzyh") {//this. Txttest.text = Gvshjb.datakeys[rowindex]. Values[0]. ToString (); This. Txttest.text = E.commandargument.tostring (); int m_shqxdj = Int. Parse (E.commandargument.tostring ()); String M_ZCDM = ""; String M_WHFS = "Query teacher information by permission level";

sqlparameter[] Parameters = new Sqlparameter[3]; Parameters[0] = new SqlParameter ("@_whfs", M_WHFS); PARAMETERS[1] = new SqlParameter ("@_dm", M_SHQXDJ); PARAMETERS[2] = new SqlParameter ("@_zcdm", M_ZCDM);

DataSet dslcnew = dbhelpersql.runprocedure ("Pr_jsdm_sele", Parameters, "nlcmp");

This. Gvqxdjyh.datasource = dslcnew; This. Gvqxdjyh.databind ();

} }

Application of several buttons in the GridView

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.