【问题标题】:Add new row to database table from Datagrid从 Datagrid 向数据库表添加新行
【发布时间】:2010-12-18 03:45:32
【问题描述】:

我正在尝试使用事件处理程序和 ItemCommand 从我的数据网格更新数据库表。我设法调用了该例程,除了插入我的数据库的文本为空之外,一切正常。我设法将其追溯到未从我的数据网格页脚传递到 sql 参数的文本。我尝试先使用字符串,然后将其传递给参数,但它们也是空的。我正在使用以下行访问控件。

sqlcmd.Parameters.Add("@GoodsDesc", SqlDbType.VarChar).Value = CType(e.Item.FindControl("txtGoodsDesc"), TextBox).Text

控件本身是使用定义的

<asp:TemplateColumn HeaderText="Goods Descriptions">
    <ItemTemplate>
        <asp:Label runat="server" ID="lblGoodsDesc" Text='<%# Eval("GoodsDesc") %>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="txtGoodsDesc" runat="server" TextMode="MultiLine" Rows="3"></asp:TextBox>
    </FooterTemplate>
</asp:TemplateColumn>

我在这里遗漏了什么吗?这就像页脚中的文本在我调用它之前没有绑定到控件。

【问题讨论】:

  • 您好,感谢您的建议。当我单击链接以添加行时,我发现程序正在调用 Page_Load,因此文本框实际上在尝试添加它们之前已被清空。我不知道会首先调用 Page_Load。我对如何解决这个问题有一些想法,现在我只希望我能让它发挥作用。

标签: asp.net vb.net datagrid


【解决方案1】:

所以基本上我发现发生的问题是 VB.Net 在事件本身被调用之前调用 Page_Load,并且在 Page_Load 方法中我调用了填充数据网格的方法,这清除了在读取其中的值之前的页脚。

为了阻止这种情况,我在 Page_Load 中的方法调用周围设置了一个条件

If Not IsPostBack Then

    FillDataGrid()

End If

然后我调用了 FillDataGrid() 函数作为处理程序的最后一步,这意味着数据被读入,然后数据网格被绑定到新值。

【讨论】:

    【解决方案2】:

    嗨,Ryan,我们将需要更多代码。你在哪里 .Add +ing 这个参数另外在哪里 e.Item.FindControl 是什么事件?

    您需要检查您是否在页脚控件上:

    protected void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Footer)
            {
                if (dg.EditItemIndex != -1)
                {
                    ((TextBox)e.Item.FindControl("txtGoodsDesc")).Text
                }
            }
        }
    

    或者在 vb.net 中

    if (e.Item.ItemType = ListItemType.Footer) then
      Dim s as String=String.Empty
      s=CType(e.Item.FindControl("txtGoodsDesc"), TextBox).Text
    end if
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-01
      • 2011-08-14
      • 2018-03-26
      • 1970-01-01
      • 2017-04-23
      • 2018-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多