Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 插入新行,不从文本框中获取文本_C#_Asp.net_Gridview - Fatal编程技术网

C# 插入新行,不从文本框中获取文本

C# 插入新行,不从文本框中获取文本,c#,asp.net,gridview,C#,Asp.net,Gridview,我在使用web应用程序时遇到问题。我有一个与SQL datatable连接的gridview,在页脚中有文本框(或下拉列表)和一个“插入”按钮。当我单击该按钮并希望在SQL表中插入新行时,就会出现问题。它不从文本框中读取文本(我们需要输入一些字符串)。当然,insert不会发生,因为某些数据不能为null。 我在编辑行时也有同样的问题 这是我的代码: Gridview: <asp:GridView ID="gvUser" runat="server" AutoGenerateColumns

我在使用web应用程序时遇到问题。我有一个与SQL datatable连接的gridview,在页脚中有文本框(或下拉列表)和一个“插入”按钮。当我单击该按钮并希望在SQL表中插入新行时,就会出现问题。它不从文本框中读取文本(我们需要输入一些字符串)。当然,insert不会发生,因为某些数据不能为null。 我在编辑行时也有同样的问题

这是我的代码:

Gridview:

<asp:GridView ID="gvUser" runat="server" AutoGenerateColumns="False" 
DataKeyNames="ID" OnRowDataBound="gvUser_RowDataBound"
OnRowCommand="gvUser_RowCommand" ShowFooter="True">
    <Columns>
        <asp:TemplateField HeaderText="ID">
            <ItemTemplate>
                <asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="lblName" runat="server" Text='<%# Bind("name") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtNewName" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Last name">
            <ItemTemplate>
                <asp:Label ID="lblLname" runat="server" Text='<%# Bind("lname") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtLname" runat="server" Text='<%# Bind("lname") %>'></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtNewLname" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Edit" ShowHeader="False">
            <FooterTemplate>
                <asp:LinkButton ID="lnkAdd" runat="server" CausesValidation="False" CommandName="Insert"
                    Text="Shrani"></asp:LinkButton>
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
编辑: 好吧,它现在起作用了。发现了一个类似的问题,作者说他可以通过在Gridview中添加EnableViewState=“false”来解决

我试过了,结果成功了。:)


这里有人能回答为什么会这样吗?这将如何与其他gv功能相对应?

尝试使用此代码-基于
e.Item.FindControl

 if (e.CommandName.Equals("Insert"))
    {
        TextBox txtNewName = (TextBox)e.Item.FindControl("txtNewName");
        TextBox txtNewLame = (TextBox)e.Item.FindControl("txtNewLname");

....

    }

OnRowCommand的名称不同-代码隐藏中的gvUser_RowCommand,GridView中的GVuporabniki_RowCommand对不起,这是我在这里键入时的错误。gridview实际上有很多mor列,但我想如果我在这里包括2作为示例,就足以找出错误。我已经编辑了你的标题。请看,“,其中的共识是“不,他们不应该”。@John:很抱歉。会记住我可能会遇到的问题。我找到了解决办法。编辑了该问题。我收到此错误:“System.Web.UI.WebControl.GridViewCommandEventArgs”不包含“Item”的定义,并且找不到接受“System.Web.UI.WebControl.GridViewCommandEventArgs”类型的第一个参数的扩展方法“Item”(是否缺少using指令或程序集引用?)
 if (e.CommandName.Equals("Insert"))
    {
        TextBox txtNewName = (TextBox)e.Item.FindControl("txtNewName");
        TextBox txtNewLame = (TextBox)e.Item.FindControl("txtNewLname");

....

    }