不用session記住選了哪一列,而是改用CommandArgument(user點了這個小控制項,而這個小控制項帶有的特殊參數)。
重點:
1.
在前置程式碼部分,對加在樣板中的Button加入CommandArgument
<asp:Button ID="Button1" runat="server" Text="自訂按鈕" CommandArgument='<%# Container.DataItemIndex %>' />
而Container.DataItemIndex: 這個可以幫助抓到Button在GridView的第幾列。
2.
GridView1_RowCommand:只要GridView內產生任何事件都會引發此事件
3.
my_DLL.ClientID: 取的ASP.NET產生之HTML標籤的控制項id ex:GridView1_DropDownList1_2 ,知道是在GridView中的DropDownList然後第幾列。
後置程式碼:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//新增一個DropDownList
DropDownList my_DLL = new DropDownList();
//GridView1.Rows[Convert.ToInt32(e.CommandArgument)],利用CommandArgument知道user點到第幾列,再抓這列的DropDownList1
my_DLL = (DropDownList)GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("DropDownList1");
//my_DLL.ClientID: 取的ASP.NET產生之HTML標籤的控制項id
Response.Write(my_DLL.ClientID+"<br/>");
Response.Write(my_DLL.SelectedValue);
}