在GridView放一個Linkbutton或是ImageButton時
RowCommand event裡面沒有可以直接讀取index
如果要取得DataKeyNames的值就需要RowIndex才有辦法
有二種讀取的方式
第一種
<asp:GridView ID="dgvCountersignSetList" runat="server" AutoGenerateColumns="False"
OnRowCommand="dgvCountersignSetList_RowCommand" DataKeyNames="event_id" >
<Columns>
<asp:TemplateField InsertVisible="False" ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl="xxx.gif" Text="刪除" CommandName="DelData"
//直接這樣就可以給序號了
CommandArgument='<%# Container.DataItemIndex%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DATA01" HeaderText="xxx">
<HeaderStyle Font-Size="Small" Width="75px" />
<ItemStyle Font-Size="Small" />
</asp:BoundField>
<asp:BoundField DataField="DATA02" HeaderText="xxx">
<HeaderStyle Font-Size="Small" Width="80px" />
<ItemStyle Font-Size="Small" />
</asp:BoundField>
<asp:BoundField DataField="DATA03" HeaderText="xxx">
<HeaderStyle Font-Size="Small" Width="120px" />
<ItemStyle Font-Size="Small" />
</asp:BoundField>
</Columns>
</asp:GridView>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DelData")
{
//這樣就可以讀到RowIndex
int index = Convert.ToInt32(e.CommandArgument);
//這樣就可以取得Keys值了
string keyId = GridView1.DataKeys[index].Value.ToString();
}
}
第二種
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DelData")
{
//這樣就可以讀到RowIndex
int index = ((GridViewRow)((ImageButton)e.CommandSource).NamingContainer).RowIndex;
//這樣就可以取得Keys值了
string keyId = GridView1.DataKeys[index].Value.ToString();
}
}
第二種方式本來我是不知道後來看到的
參考:http://hi.baidu.com/brian_chiu/blog/item/1a5f3e2b0462f4f2e6cd40d2.html
全站熱搜
留言列表
