开发者

Add new row to database table from Datagrid

I am trying to update a database table from my datagrid using an event handler and ItemCommand. I manage to ca开发者_运维问答ll the routine and everything is working fine except the text that is inserted into my database is empty. I managed to track this back to the text not being passed from my datagrids footer to the sql parameters. I tried using a string first and then passing that to the parameters but they were also empty. I am accessing the control using the following line.

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

The control itself is defined using

<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>

Am I missing something here? It's like the text in the footer isnt being tied to the control before I call it.


HI Ryan, we will need more code then just that. Where are you .Add +ing this parameter in addition where is e.Item.FindControl what event is it?

You need to check if you are on the footer control:

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

Or in 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


So essentially I discovered that the issue that was occurring was that VB.Net was calling Page_Load before the event itself was being called, and in the Page_Load method I was calling the method that filled the datagrid, which cleared the textboxes in the footer before the values in them were being read.

To stop this I placed a condition around the call to the method in Page_Load

If Not IsPostBack Then

    FillDataGrid()

End If

Then I called the FillDataGrid() function as the very last step in the handler, meaning that the data was read in and then the datagrid was bound to the new values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜