【发布时间】:2011-08-19 18:38:48
【问题描述】:
我有一个 GridView,我绑定到 Page_Load 上的 SqlDataReader。它有一个带有按钮的列,当使用以下代码单击按钮时,我试图获取该行:
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
编辑:从 cmets 部分粘贴 .aspx 页面
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" OnRowCommand="GridView1_RowCommand" DataKeyNames="id" GridLines="None"> <AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnChange" runat="server" Text="Change" CommandName="Test" Visible='<%# Convert.ToBoolean(Eval("Tested")) == true ? true : false %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</aspx:GridView>
我收到以下错误:“System.FormatException:输入字符串的格式不正确。”在线'int index = Convert.ToInt32(e.CommandArgument);'。
有什么想法吗?
【问题讨论】:
-
您是否在 int index=.. 上设置了断点并检查了 e.CommandArgument 是什么?它很可能是一个无法转换为整数的值。
-
您是否费心检查 e.CommandArgument 的值?文档说
FormatException将在“值不包含可选符号后跟一系列数字(0 到 9)”时抛出。 msdn.microsoft.com/en-us/library/sf1aw27b.aspx -
你是在 OnRowCommand 上做这个吗?
-
我检查了 commandArgument 并且是 ""。但是为什么会发生这种情况? @Emaad Ali 是的,我在 GridView1_RowCommand 上这样做
-
你能把你设置了命令参数的.aspx页面贴出来吗?