我的gridview btnOne,btnTwo 中有两列按钮。当我单击其中一个或出现此错误时。
我有这个错误:无效的回发或回调参数。使用配置或页面中的 <%@ Page EnableEventValidation="true" %> 启用事件验证。
<asp:GridView ID="GridViewStrength" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" DataKeyNames="ItemID"
AllowPaging="True" AllowSorting="True" onrowcommand="GridViewStrength_RowCommand" Width="100%">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Note" InsertVisible="False"
SortExpression="ItemID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ItemID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="btnOne" runat="server" Text="One" CommandName = "AddNoteClick" CommandArgument = '<%# Bind("ItemID") %>' />
</ItemTemplate>
<HeaderStyle
HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="ItemDesc" HeaderText="Item Description" SortExpression="ItemDesc" ><ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField HeaderText="Two" InsertVisible="False"
SortExpression="Two">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ItemID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="btnTwo" runat="server" Text="Vote" CommandName = "VoteClick" CommandArgument = '<%# Bind("ItemID") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<br /><br /><p>ITEM:
</ContentTemplate>
</asp:UpdatePanel></td>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:LineConnectionString %>"
SelectCommand="SELECT * FROM [tblMain] WHERE TypeID = 1 AND Admin = 0">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1Admin" runat="server" ConnectionString="<%$ ConnectionStrings:LineConnectionString %>"
SelectCommand="SELECT * FROM [tblMain] WHERE TypeID = 1 AND Admin = 1">
</asp:SqlDataSource>
C# 代码
//If not admin
if (isAdmin == 0)
{
GridViewStrength.DataSource = SqlDataSource1;
}
//If admin
else if (isAdmin == 1)
{
GridViewStrength.DataSource = SqlDataSource1Admin;
}
GridViewStrength.DataBind();
当我仅在 SQL 数据源上使用时它工作正常,但是当我使用两个时它给了我错误。
我上网查了一下,发现了这个。但我不知道如何用我的代码来做到这一点。
actually i gave unique id's for all buttons and other item template column controls in the grid view and it worked.
foreach (GridViewRow grdRw in grdVwUsersList.Rows)
{
int cnt = grdRw.Cells[6].Controls.Count;
ImageButton editBtn = (ImageButton)grdRw.Cells[6].Controls[1];
editBtn.ID = "editBtn_" + grdRw.RowIndex.ToString();
ImageButton saveBtn = (ImageButton)grdRw.Cells[6].Controls[3];
saveBtn.ID = "saveBtn_" + grdRw.RowIndex.ToString();
ImageButton saveCancelBtn = (ImageButton)grdRw.Cells[6].Controls[5];
saveCancelBtn.ID = "saveCancelBtn_" + grdRw.RowIndex.ToString();
}