单击“模板字段”按钮时,GridView会导致无效回发

时间:2013-06-20 15:20:37

标签: c# asp.net gridview

我的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();

  }

0 个答案:

没有答案