I used this today, reproduced "things in person", the original address: http://www.cnblogs.com/andhm/archive/2010/05/07/1730024.html
Datakeynames indicates the name of the primary key column. You can use gridviewentity. datakeys [rowindex] ["columsname"] to obtain its value. Of course, it is not displayed. In fact, I found it in a sqldatasource, we can see that there is such a primary key to implement updates. Of course, this is not required in our multi-layer development. How can we get the value when we use the template? We can also set that column to datakeynames. Remember to separate multiple columns with commas.
If it is not set to datakeynames, you can only get the value through gridviewentity. Rows [rowindex]. Cell [Index]. Text. Do you know if my brother has seen some HTML characters? & Nbsp; this is the most common problem. We can solve it with httpunility. htmldecode!
In the process of using the gridview, we often encounter this problem. We select a row to edit, or select a row to delete or ....... we need to obtain some information about the current row, especially the primary key information of the current row. The primary key information is generally not displayed on the page. At this time, we have three methods to deal with it. The first method is to use datakeynames. Here we will focus on it. The second is to use the commandargument attribute of the button to specify the required information. The third is the oldest and most common method to display data using hidden methods.
Method 1: Use datakeynames. datakeynames can be a column or multiple columns. Foreground: datakeynames = "FID" bind a value to the backend: gridview1.datakeys [E. row. rowindex]. value. tostring ();
Foreground: datakeynames = "FID, fname" bind two values to the background: gridview1.datakeys [E. row. rowindex]. values [0]. tostring (); Background: gridview1.datakeys [E. row. rowindex]. values [1]. tostring (); or backend: gridview1.datakeys [E. row. rowindex]. values ["FID"]. tostring (); Background: gridview1.datakeys [E. row. rowindex]. values ["fname"]. tostring ();
Method 2: Use the commandargument attribute of the button to specify the required information.
Typical Example: Download the attachment column (the link for displaying and downloading attachments is available, and the link for displaying and downloading attachments without attachments is blank) <asp: templatecolumn headertext = "attachment">
In the dghkstock_itemcommand event: If ("Download" = E. commandname) {mybc. systemovertime0 (); // determines whether the session has expired. mybc. rightmanage ("16010500"); // determines whether the user has the permission to open this webpage. http_downloadfile (E. commandargument. tostring (); // path for storing attachments that obtain the current inventory information
}
The oldest and most common method uses the hidden Method for display.
<Style type = "text/CSS">. Test {display: None ;}</style>
<Asp: templatefield headertext = "name" sortexpression = "name" itemstyle-cssclass = "test" headerstyle-cssclass = "test"> <itemtemplate> <asp: label id = "label1" runat = "server" text = '<% # eval ("name") %>'> </ASP: label> </itemtemplate> </ASP: templatefield>
Convert a hidden column into a template column and access the content of the template column through findcontrol:
<Asp: templatefield visible = "false"> <itemtemplate> <asp: Label runat = "server" text = '<% # eval ("ID ") %> 'id = "lblid"> </ASP: Label> </itemtemplate> </ASP: templatefield>
Protected void gridviewinclurowediting (Object sender, gridviewediteventargs e) {label lblid = gridview1.rows [E. neweditindex]. cells [0]. findcontrol ("lblid") as label; response. write (lblid. text );}
I used this today, reproduced "things in person", the original address: http://www.cnblogs.com/andhm/archive/2010/05/07/1730024.html
Datakeynames indicates the name of the primary key column. You can use gridviewentity. datakeys [rowindex] ["columsname"] to obtain its value. Of course, it is not displayed. In fact, I found it in a sqldatasource, we can see that there is such a primary key to implement updates. Of course, this is not required in our multi-layer development. How can we get the value when we use the template? We can also set that column to datakeynames. Remember to separate multiple columns with commas.
If it is not set to datakeynames, you can only get the value through gridviewentity. Rows [rowindex]. Cell [Index]. Text. Do you know if my brother has seen some HTML characters? & Nbsp; this is the most common problem. We can solve it with httpunility. htmldecode!
In the process of using the gridview, we often encounter this problem. We select a row to edit, or select a row to delete or ....... we need to obtain some information about the current row, especially the primary key information of the current row. The primary key information is generally not displayed on the page. At this time, we have three methods to deal with it. The first method is to use datakeynames. Here we will focus on it. The second is to use the commandargument attribute of the button to specify the required information. The third is the oldest and most common method to display data using hidden methods.
Method 1: Use datakeynames. datakeynames can be a column or multiple columns. Foreground: datakeynames = "FID" bind a value to the backend: gridview1.datakeys [E. row. rowindex]. value. tostring ();
Foreground: datakeynames = "FID, fname" bind two values to the background: gridview1.datakeys [E. row. rowindex]. values [0]. tostring (); Background: gridview1.datakeys [E. row. rowindex]. values [1]. tostring (); or backend: gridview1.datakeys [E. row. rowindex]. values ["FID"]. tostring (); Background: gridview1.datakeys [E. row. rowindex]. values ["fname"]. tostring ();
Method 2: Use the commandargument attribute of the button to specify the required information.
Typical Example: Download the attachment column (the link for displaying and downloading attachments is available, and the link for displaying and downloading attachments without attachments is blank) <asp: templatecolumn headertext = "attachment">
In the dghkstock_itemcommand event: If ("Download" = E. commandname) {mybc. systemovertime0 (); // determines whether the session has expired. mybc. rightmanage ("16010500"); // determines whether the user has the permission to open this webpage. http_downloadfile (E. commandargument. tostring (); // path for storing attachments that obtain the current inventory information
}
The oldest and most common method uses the hidden Method for display.
<Style type = "text/CSS">. Test {display: None ;}</style>
<Asp: templatefield headertext = "name" sortexpression = "name" itemstyle-cssclass = "test" headerstyle-cssclass = "test"> <itemtemplate> <asp: label id = "label1" runat = "server" text = '<% # eval ("name") %>'> </ASP: label> </itemtemplate> </ASP: templatefield>
Convert a hidden column into a template column and access the content of the template column through findcontrol:
<Asp: templatefield visible = "false"> <itemtemplate> <asp: Label runat = "server" text = '<% # eval ("ID ") %> 'id = "lblid"> </ASP: Label> </itemtemplate> </ASP: templatefield>
Protected void gridviewinclurowediting (Object sender, gridviewediteventargs e) {label lblid = gridview1.rows [E. neweditindex]. cells [0]. findcontrol ("lblid") as label; response. write (lblid. text );}