【发布时间】:2015-03-14 22:49:18
【问题描述】:
我想通过 javascript 确认从我的 gridview 中删除一行。
我的网格视图看起来像:
<asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:ButtonField CommandName="delete" Text="Delete" />
</Columns>
</asp:GridView>
我已经尝试过使用 TempleField 并且它可以工作,但问题是获取要从数据库中删除的选择行的值。
我试过这样:
<asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnSave" runat="server" Text="Delete" OnClientClick="return confirm('Do you want to delete?')" CommandName="delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
并尝试获取这样的值:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "delete")
{
int index = Convert.ToInt32(e.CommandArgument);
string id = GridView1.Rows[index].Cells[1].Text.ToString();
Response.Write(id);
}
}
但它会在
上引发 FormatExceptionint index = Convert.ToInt32(e.CommandArgument);
有人可以帮助我吗?抱歉,我的英语不好。
【问题讨论】:
-
“我试过这样” 那么成功了吗?如果没有,发生了什么?
-
需要在按钮标签中添加
CommandArgument='<%# Eval("Yourrowidfieldvalue") %>' -
好的,我现在添加了那个错误:“ArgumentOutOfRangeException”在线'string id = GridView1.Rows[index].Cells[1].Text.ToString();'
-
你添加了什么作为你的命令参数?如果您将其用作索引,请使用
<%# ((GridViewRow) Container).RowIndex %> -
我使用的方式与您在第二个示例中的方式相同。但在我确认的最后,我使用了
;,删除的是Delete。工作正常。