C# 获取ASP.NET中Buttonfield的值
假设我的GridView ID&Name中只有两列,每次单击它时都需要获取ID的值。例如,如果我点击123,我应该得到123。但是在这里。。。事实并非如此。。。当我点击任何ID时,它会返回该ID旁边的名称,例如;在123的例子中,我得到了Tony Stark,但是当我在单击时得到ID的值时,它总是返回一个空字符串 换句话说,当X=1时,我在id中得到了正确的名称,但是当X=0时,id变成了一个空字符串。。。intx和stringid:参见下面的C代码 我的GridView中的XAML代码:C# 获取ASP.NET中Buttonfield的值,c#,asp.net,gridview,buttonfield,C#,Asp.net,Gridview,Buttonfield,假设我的GridView ID&Name中只有两列,每次单击它时都需要获取ID的值。例如,如果我点击123,我应该得到123。但是在这里。。。事实并非如此。。。当我点击任何ID时,它会返回该ID旁边的名称,例如;在123的例子中,我得到了Tony Stark,但是当我在单击时得到ID的值时,它总是返回一个空字符串 换句话说,当X=1时,我在id中得到了正确的名称,但是当X=0时,id变成了一个空字符串。。。intx和stringid:参见下面的C代码 我的GridView中的XAML代码: 出什
出什么事了?!!如何解决这个问题?谢谢 在RowCommand处理程序中,我认为这就是您想要的:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int id = (int)(DataBinder.Eval(e.Row.DataItem, "ID"));
Response.Write(id);
}
}
我正在从vb转换,所以我可能错过了一些语法,但你明白了 更复杂但更好的解决方案是: 在GridView中,使用DataKeyNames标记和RowCommand事件,并将绑定值放入DataKeyName中,例如:
DataKeyNames="ID" OnRowCommand="GridView1_RowCommand"
在gridview中使用模板字段您不需要仅使用imagebutton,如下例所示:
<asp:TemplateField HeaderText="Action">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Left" Wrap="false" />
<ItemTemplate>
<div class="GridViewOpc">
<asp:ImageButton ID="buttonDelete" runat="server" Width="17" Height="17" AlternateText="Update"
CausesValidation="False" Visible="false" CommandName="Delete" Enabled="true"
ImageUrl="~/yourDeletepicture.gif" />
<asp:ImageButton ID="buttonUpdate" runat="server" AlternateText="Update" Width="17"
Height="17" CausesValidation="False" Visible="false" CommandName="Update" ImageUrl="~/yourUpdatePicture.gif" />
</div>
</ItemTemplate>
</asp:TemplateField>
X应该是什么?X是单元格的索引参见C代码。名称列为1,ID列为0,但情况并非如此。0总是给我一个空字符串。一般来说,对于.net网格,单元格中会有另一个控件。如果在调试时检查单元格[0]。控件或.Children,是否存在此类实例?另外,您确定行索引正确吗?是的,行和单元格都正确。我在尝试字符串id=EmployeeList.Rows[Convert.ToInt32e.CommandArgument].Cells[0].Controls.ToString时获得此输出;System.Web.UI.ControlCollectionEmployeeList.Rows[Convert.ToInt32e.CommandArgument]的类型是什么?单元格[0]。控件[0]
DataKeyNames="ID" OnRowCommand="GridView1_RowCommand"
<asp:TemplateField HeaderText="Action">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Left" Wrap="false" />
<ItemTemplate>
<div class="GridViewOpc">
<asp:ImageButton ID="buttonDelete" runat="server" Width="17" Height="17" AlternateText="Update"
CausesValidation="False" Visible="false" CommandName="Delete" Enabled="true"
ImageUrl="~/yourDeletepicture.gif" />
<asp:ImageButton ID="buttonUpdate" runat="server" AlternateText="Update" Width="17"
Height="17" CausesValidation="False" Visible="false" CommandName="Update" ImageUrl="~/yourUpdatePicture.gif" />
</div>
</ItemTemplate>
</asp:TemplateField>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Control ctl = e.CommandSource as Control;
GridViewRow CurrentRow = ctl.NamingContainer as GridViewRow;
if (e.CommandName == "Update")
{
int ID = Convert.ToInt32(GridView1.DataKeys[CurrentRow.RowIndex][0]);
}
if (e.CommandName == "Delete")
{
int ID = Convert.ToInt32(GridView1.DataKeys[CurrentRow.RowIndex][0]);
}
}