代码之家  ›  专栏  ›  技术社区  ›  Ryan French

从DataGrid向数据库表添加新行

  •  0
  • Ryan French  · 技术社区  · 15 年前

    我正在尝试使用事件处理程序和itemcommand从数据报更新数据库表。我设法调用了这个例程,除了插入到我的数据库中的文本是空的之外,一切都正常工作。我设法将此跟踪回没有从DataGrids页脚传递到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>
    

    我是不是错过了什么?就像在我调用控件之前,页脚中的文本没有绑定到该控件。

    2 回复  |  直到 15 年前
        1
  •  0
  •   JonH    15 年前

    嗨,Ryan,我们需要更多的代码。你在哪里?除了这个参数,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
    
        2
  •  0
  •   Ryan French    15 年前

    因此,本质上,我发现发生的问题是,在调用事件本身之前,vb.net正在调用page_load,在page_load方法中,我正在调用填充数据报的方法,在读取其中的值之前,该方法清除了页脚中的文本框。

    为了停止这个操作,我在页面加载中的方法调用周围放置了一个条件

    If Not IsPostBack Then
    
        FillDataGrid()
    
    End If
    

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