1

我有这个gridview,我不知道里面的按钮有什么问题。我有这个 asp 代码:

<asp:GridView ID="gvList" runat="server">
            <Columns>
                <asp:TemplateField HeaderText="User Name" HeaderStyle-ForeColor="Black"  HeaderStyle-Font-Bold="true">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblUsername" Text='<%# Eval("cUserName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Dept User" HeaderStyle-ForeColor="Black"  HeaderStyle-Font-Bold="true">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblDept" Text='<%#  iif(Eval("lDeptUser"),"Yes","No")  %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Actions" HeaderStyle-ForeColor="black" HeaderStyle-Font-Bold="true">
                    <ItemTemplate>
                        <asp:Button  ID="btnedit" runat="server" Text="Edit" />
                        <asp:Button ID="btnDelete" OnClick="DeleteRow" runat="server" Text="Delete" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            </asp:GridView>

当我在任何行上按删除或编辑时,它会给我错误!Invalid postback or callback argument.

这是我在 vb.net 中的服务器端代码:

Public Function GetList() As DataTable
    Dim Query As String = "Select cUserName,lDeptUser FROM Intranet.dbo.Gn_ISCoordinators"
    Dim dt As DataTable = New DataTable()
    Using adapter = New SqlDataAdapter(Query, ConfigurationManager.ConnectionStrings("IntranetConnectionString").ConnectionString)
        adapter.Fill(dt)
        gvList.DataSource = dt
        gvList.DataBind()
        Return dt
    End Using

End Function

Public Function DelRow() As DataTable

    Dim strusername As String = CType(gvList.FindControl("lblUsername"), Label).Text.Trim()

    Dim Query As String = "Delete FROM Intranet.dbo.Gn_ISCoordinators where cUserName='" & strusername & "'"
    Dim dt As DataTable = New DataTable()
    Using Adapter = New SqlDataAdapter(Query, ConfigurationManager.ConnectionStrings("IntranetConnectionString").ConnectionString)
        Adapter.Fill(dt)
        Return dt
    End Using

End Function

Protected Sub DeleteRow(ByVal sender As Object, ByVal e As System.EventArgs)
    DelRow()
End Sub

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    GetList()

End Sub

Protected Sub gv(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvList.RowDataBound
    e.Row.Cells(3).Visible = False
    e.Row.Cells(4).Visible = False


End Sub

End Class

我认为它来自客户端。请帮我解决这个问题。顺便说一句,到目前为止我还没有使用任何 ajaxtoolkit,EnableEventValidation="true"在页面中以及 web.config

是什么问题及其解决方法,请帮助我。

提前致谢。

4

2 回答 2

1

您不能使用此函数调用。因为

CType(gvList.FindControl("lblUsername"), Label).Text.Trim()

仅允许在按钮单击事件上。按钮单击事件本身中的功能代码也是如此。

于 2013-03-14T10:11:11.867 回答
1

首先,我将代码包装在Page_Load

If Not IsPostBack Then GetList()

因此,您只想在第一次加载而不是回发时对网格进行数据绑定。它的状态将通过ViewwState默认保持。

中的下一个问题DeleteRow,您试图FindControl通过GridView. 但NamingContainer它是GridViewRow因为 aGridView包含多行(和标签)而不仅仅是一个。

所以你必须首先获得对行的引用。只需NamingContainer使用Button:

Dim btn = DirectCast(sender, Button)
Dim row = DirectCast(btn.NamingContainer, GridViewRow)
Dim lblUsername = DirectCast(row.FindControl("lblUsername"), Label)
' ... '
于 2013-03-14T10:16:00.577 回答