前台:<asp:TemplateField HeaderText="数量">
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:ImageButton ID="sub" runat="server" CommandName="sub" CommandArgument='<%#Eval("ID") %>'
CausesValidation="false" CssClass="as" ImageUrl="~/Images/sub.gif" />
<asp:TextBox ID="count" runat="server" onchange="check(this)" Text='<%#Eval("数量") %>'></asp:TextBox>
<asp:ImageButton CssClass="as" ID="add" CommandName="add" runat="server" CausesValidation="false"
ImageUrl="~/Images/add.gif" /></ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>后台:在GridView1_RowCreated事件中
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton add = (ImageButton)GridView1.Rows[e.Row.RowIndex].FindControl("add");
add.CommandArgument = e.Row.RowIndex.ToString();
ImageButton sub = (ImageButton)GridView1.Rows[e.Row.RowIndex].FindControl("sub");
sub.CommandArgument = e.Row.RowIndex.ToString();
}
运行出现错误
索引超出范围。必须为非负值并小于集合大小。
参数名: index 调试了一下,发现是控件为null
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:ImageButton ID="sub" runat="server" CommandName="sub" CommandArgument='<%#Eval("ID") %>'
CausesValidation="false" CssClass="as" ImageUrl="~/Images/sub.gif" />
<asp:TextBox ID="count" runat="server" onchange="check(this)" Text='<%#Eval("数量") %>'></asp:TextBox>
<asp:ImageButton CssClass="as" ID="add" CommandName="add" runat="server" CausesValidation="false"
ImageUrl="~/Images/add.gif" /></ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>后台:在GridView1_RowCreated事件中
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton add = (ImageButton)GridView1.Rows[e.Row.RowIndex].FindControl("add");
add.CommandArgument = e.Row.RowIndex.ToString();
ImageButton sub = (ImageButton)GridView1.Rows[e.Row.RowIndex].FindControl("sub");
sub.CommandArgument = e.Row.RowIndex.ToString();
}
运行出现错误
索引超出范围。必须为非负值并小于集合大小。
参数名: index 调试了一下,发现是控件为null
{
if(e.Row.DataItemIndex>=0)
{
ImageButton add = (ImageButton)e.Row.FindControl("add");
....
}
}是RowDataBound
应该是判断if(e.Row.RowIndex>=0)