Getting the value of hidden fields after an update button has been clicked

Discussion in 'ASP .Net Datagrid Control' started by George, May 6, 2004.

  1. George

    George Guest

    All,

    I have a data grid that uses template controls to support editing
    data, a footer template to support adding new data, and a delete
    button to delete a row. I store the unique id for the row from the
    database in a hidden column and I need to use this value if the user
    updates or deletes the row. (The HTML code from VS is below.. )

    The problem is that after I click the edit button, enter edit
    mode, then click the update button, I cannot find a way to get the
    unique id from the hidden field in that row so I can do the update.
    Any suggestions?

    Thanks
    George Ceaser



    --------------------------------------------------
    html
    --------------------------------------------------
    <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="testgrid1.aspx.vb" Inherits="OurTeamSite.testgrid1"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>testgrid1</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5"
    name="vs_targetSchema">
    </HEAD>
    <body style="FONT: 10pt verdana">
    <form id="Form1" runat="server">
    <h3><font face="Verdana">Using an Edit Command Column in
    DataGrid</font></h3>
    <asp:datagrid id="MyDataGrid" runat="server" Width="544px"
    Font-Names="Verdana" AutoGenerateColumns="False"
    OnUpdateCommand="MyDataGrid_Update"
    OnCancelCommand="MyDataGrid_Cancel" OnEditCommand="MyDataGrid_Edit"
    HeaderStyle-BackColor="#aaaadd" Font-Size="8pt"
    Font-Name="Verdana" CellPadding="3" BorderWidth="1px"
    BorderColor="Black">
    <HeaderStyle BackColor="#AAAADD"></HeaderStyle>
    <Columns>
    <asp:EditCommandColumn ButtonType="LinkButton"
    UpdateText="Update" CancelText="Cancel" EditText="Edit">
    <HeaderStyle Wrap="False"></HeaderStyle>
    <ItemStyle Wrap="False"></ItemStyle>
    </asp:EditCommandColumn>
    <asp:BoundColumn DataField="TeamName" HeaderText="Team Name">
    <HeaderStyle Width="40%"></HeaderStyle>
    <ItemStyle Wrap="False"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="Password" HeaderText="Password">
    <HeaderStyle Width="15%"></HeaderStyle>
    <ItemStyle Wrap="False"></ItemStyle>
    </asp:BoundColumn>
    <asp:TemplateColumn HeaderText="Division">
    <HeaderStyle Width="5%"></HeaderStyle>
    <ItemStyle Wrap="False"></ItemStyle>
    <ItemTemplate>
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
    "DataItem.Division") %>' ID="lblDivision" >
    </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:DropDownList id="ddlDivision" DataSource="<%#
    BindDivision() %>" DataTextField="Division"
    DataValueField="DivisionID" runat="server">
    </asp:DropDownList>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Sponsor">
    <HeaderStyle Width="40%"></HeaderStyle>
    <ItemTemplate>
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
    "DataItem.Sponsor") %>' ID="Label2" NAME="Label2">
    </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox runat="server" Text='<%#
    DataBinder.Eval(Container, "DataItem.Sponsor") %>' ID="Textbox1"
    NAME="Textbox1">
    </asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn Visible="False"
    DataField="divisionid"></asp:BoundColumn>
    </Columns>
    </asp:datagrid></form>
    </body>
    </HTML>


    ------------------------------

    Sub MyDataGrid_Update(ByVal sender As Object, ByVal e As
    DataGridCommandEventArgs)

    Dim qtyText As TextBox = CType(e.Item.Cells(5).Controls(0),
    TextBox)
    End Sub
    -----------

    NOTE: - The text property of the qtyText control is an empty string
    when this executes.

    --------------------
     
    George, May 6, 2004
    #1
    1. Advertising

  2. George,
    You need to use the DataKeyField property of the DataGrid.
    After you make the DataBind() you should set that property to the Key
    Field.

    dgrGrid.DataKeyField = "KeyFieldName";

    Then when you need the id for any item, in the ItemDataBound will be
    something like :

    dgrGrid.DataKeys[e.Item.ItemIndex]

    Regards,

    --

    Leonardo C. Rodríguez
    Email at:
    Location : Bs.As., Argentina



    "George" <> wrote in message
    news:...
    > All,
    >
    > I have a data grid that uses template controls to support editing
    > data, a footer template to support adding new data, and a delete
    > button to delete a row. I store the unique id for the row from the
    > database in a hidden column and I need to use this value if the user
    > updates or deletes the row. (The HTML code from VS is below.. )
    >
    > The problem is that after I click the edit button, enter edit
    > mode, then click the update button, I cannot find a way to get the
    > unique id from the hidden field in that row so I can do the update.
    > Any suggestions?
    >
    > Thanks
    > George Ceaser
    >
    >
    >
    > --------------------------------------------------
    > html
    > --------------------------------------------------
    > <%@ Page Language="vb" AutoEventWireup="false"
    > Codebehind="testgrid1.aspx.vb" Inherits="OurTeamSite.testgrid1"%>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > <HTML>
    > <HEAD>
    > <title>testgrid1</title>
    > <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    > <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
    > <meta content="JavaScript" name="vs_defaultClientScript">
    > <meta content="http://schemas.microsoft.com/intellisense/ie5"
    > name="vs_targetSchema">
    > </HEAD>
    > <body style="FONT: 10pt verdana">
    > <form id="Form1" runat="server">
    > <h3><font face="Verdana">Using an Edit Command Column in
    > DataGrid</font></h3>
    > <asp:datagrid id="MyDataGrid" runat="server" Width="544px"
    > Font-Names="Verdana" AutoGenerateColumns="False"
    > OnUpdateCommand="MyDataGrid_Update"
    > OnCancelCommand="MyDataGrid_Cancel" OnEditCommand="MyDataGrid_Edit"
    > HeaderStyle-BackColor="#aaaadd" Font-Size="8pt"
    > Font-Name="Verdana" CellPadding="3" BorderWidth="1px"
    > BorderColor="Black">
    > <HeaderStyle BackColor="#AAAADD"></HeaderStyle>
    > <Columns>
    > <asp:EditCommandColumn ButtonType="LinkButton"
    > UpdateText="Update" CancelText="Cancel" EditText="Edit">
    > <HeaderStyle Wrap="False"></HeaderStyle>
    > <ItemStyle Wrap="False"></ItemStyle>
    > </asp:EditCommandColumn>
    > <asp:BoundColumn DataField="TeamName" HeaderText="Team Name">
    > <HeaderStyle Width="40%"></HeaderStyle>
    > <ItemStyle Wrap="False"></ItemStyle>
    > </asp:BoundColumn>
    > <asp:BoundColumn DataField="Password" HeaderText="Password">
    > <HeaderStyle Width="15%"></HeaderStyle>
    > <ItemStyle Wrap="False"></ItemStyle>
    > </asp:BoundColumn>
    > <asp:TemplateColumn HeaderText="Division">
    > <HeaderStyle Width="5%"></HeaderStyle>
    > <ItemStyle Wrap="False"></ItemStyle>
    > <ItemTemplate>
    > <asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
    > "DataItem.Division") %>' ID="lblDivision" >
    > </asp:Label>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:DropDownList id="ddlDivision" DataSource="<%#
    > BindDivision() %>" DataTextField="Division"
    > DataValueField="DivisionID" runat="server">
    > </asp:DropDownList>
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn HeaderText="Sponsor">
    > <HeaderStyle Width="40%"></HeaderStyle>
    > <ItemTemplate>
    > <asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
    > "DataItem.Sponsor") %>' ID="Label2" NAME="Label2">
    > </asp:Label>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:TextBox runat="server" Text='<%#
    > DataBinder.Eval(Container, "DataItem.Sponsor") %>' ID="Textbox1"
    > NAME="Textbox1">
    > </asp:TextBox>
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    > <asp:BoundColumn Visible="False"
    > DataField="divisionid"></asp:BoundColumn>
    > </Columns>
    > </asp:datagrid></form>
    > </body>
    > </HTML>
    >
    >
    > ------------------------------
    >
    > Sub MyDataGrid_Update(ByVal sender As Object, ByVal e As
    > DataGridCommandEventArgs)
    >
    > Dim qtyText As TextBox = CType(e.Item.Cells(5).Controls(0),
    > TextBox)
    > End Sub
    > -----------
    >
    > NOTE: - The text property of the qtyText control is an empty string
    > when this executes.
    >
    > --------------------
     
    Leonardo Rodríguez, May 6, 2004
    #2
    1. Advertising

  3. "After you make the DataBind() you should set that property to the Key"

    Sorry, Before!
    --

    Leonardo C. Rodríguez
    Email at:
    Location : Bs.As., Argentina



    "Leonardo Rodríguez" <> wrote in message
    news:%...
    > George,
    > You need to use the DataKeyField property of the DataGrid.
    > After you make the DataBind() you should set that property to the Key
    > Field.
    >
    > dgrGrid.DataKeyField = "KeyFieldName";
    >
    > Then when you need the id for any item, in the ItemDataBound will be
    > something like :
    >
    > dgrGrid.DataKeys[e.Item.ItemIndex]
    >
    > Regards,
    >
    > --
    >
    > Leonardo C. Rodríguez
    > Email at:
    > Location : Bs.As., Argentina
    >
    >
    >
    > "George" <> wrote in message
    > news:...
    > > All,
    > >
    > > I have a data grid that uses template controls to support editing
    > > data, a footer template to support adding new data, and a delete
    > > button to delete a row. I store the unique id for the row from the
    > > database in a hidden column and I need to use this value if the user
    > > updates or deletes the row. (The HTML code from VS is below.. )
    > >
    > > The problem is that after I click the edit button, enter edit
    > > mode, then click the update button, I cannot find a way to get the
    > > unique id from the hidden field in that row so I can do the update.
    > > Any suggestions?
    > >
    > > Thanks
    > > George Ceaser
    > >
    > >
    > >
    > > --------------------------------------------------
    > > html
    > > --------------------------------------------------
    > > <%@ Page Language="vb" AutoEventWireup="false"
    > > Codebehind="testgrid1.aspx.vb" Inherits="OurTeamSite.testgrid1"%>
    > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > > <HTML>
    > > <HEAD>
    > > <title>testgrid1</title>
    > > <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    > > <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
    > > <meta content="JavaScript" name="vs_defaultClientScript">
    > > <meta content="http://schemas.microsoft.com/intellisense/ie5"
    > > name="vs_targetSchema">
    > > </HEAD>
    > > <body style="FONT: 10pt verdana">
    > > <form id="Form1" runat="server">
    > > <h3><font face="Verdana">Using an Edit Command Column in
    > > DataGrid</font></h3>
    > > <asp:datagrid id="MyDataGrid" runat="server" Width="544px"
    > > Font-Names="Verdana" AutoGenerateColumns="False"
    > > OnUpdateCommand="MyDataGrid_Update"
    > > OnCancelCommand="MyDataGrid_Cancel" OnEditCommand="MyDataGrid_Edit"
    > > HeaderStyle-BackColor="#aaaadd" Font-Size="8pt"
    > > Font-Name="Verdana" CellPadding="3" BorderWidth="1px"
    > > BorderColor="Black">
    > > <HeaderStyle BackColor="#AAAADD"></HeaderStyle>
    > > <Columns>
    > > <asp:EditCommandColumn ButtonType="LinkButton"
    > > UpdateText="Update" CancelText="Cancel" EditText="Edit">
    > > <HeaderStyle Wrap="False"></HeaderStyle>
    > > <ItemStyle Wrap="False"></ItemStyle>
    > > </asp:EditCommandColumn>
    > > <asp:BoundColumn DataField="TeamName" HeaderText="Team Name">
    > > <HeaderStyle Width="40%"></HeaderStyle>
    > > <ItemStyle Wrap="False"></ItemStyle>
    > > </asp:BoundColumn>
    > > <asp:BoundColumn DataField="Password" HeaderText="Password">
    > > <HeaderStyle Width="15%"></HeaderStyle>
    > > <ItemStyle Wrap="False"></ItemStyle>
    > > </asp:BoundColumn>
    > > <asp:TemplateColumn HeaderText="Division">
    > > <HeaderStyle Width="5%"></HeaderStyle>
    > > <ItemStyle Wrap="False"></ItemStyle>
    > > <ItemTemplate>
    > > <asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
    > > "DataItem.Division") %>' ID="lblDivision" >
    > > </asp:Label>
    > > </ItemTemplate>
    > > <EditItemTemplate>
    > > <asp:DropDownList id="ddlDivision" DataSource="<%#
    > > BindDivision() %>" DataTextField="Division"
    > > DataValueField="DivisionID" runat="server">
    > > </asp:DropDownList>
    > > </EditItemTemplate>
    > > </asp:TemplateColumn>
    > > <asp:TemplateColumn HeaderText="Sponsor">
    > > <HeaderStyle Width="40%"></HeaderStyle>
    > > <ItemTemplate>
    > > <asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
    > > "DataItem.Sponsor") %>' ID="Label2" NAME="Label2">
    > > </asp:Label>
    > > </ItemTemplate>
    > > <EditItemTemplate>
    > > <asp:TextBox runat="server" Text='<%#
    > > DataBinder.Eval(Container, "DataItem.Sponsor") %>' ID="Textbox1"
    > > NAME="Textbox1">
    > > </asp:TextBox>
    > > </EditItemTemplate>
    > > </asp:TemplateColumn>
    > > <asp:BoundColumn Visible="False"
    > > DataField="divisionid"></asp:BoundColumn>
    > > </Columns>
    > > </asp:datagrid></form>
    > > </body>
    > > </HTML>
    > >
    > >
    > > ------------------------------
    > >
    > > Sub MyDataGrid_Update(ByVal sender As Object, ByVal e As
    > > DataGridCommandEventArgs)
    > >
    > > Dim qtyText As TextBox = CType(e.Item.Cells(5).Controls(0),
    > > TextBox)
    > > End Sub
    > > -----------
    > >
    > > NOTE: - The text property of the qtyText control is an empty string
    > > when this executes.
    > >
    > > --------------------

    >
    >
     
    Leonardo Rodríguez, May 6, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Terry Olsen
    Replies:
    3
    Views:
    663
    Eliyahu Goldin
    Jul 12, 2005
  2. Samuel
    Replies:
    1
    Views:
    288
    Samuel
    Jan 27, 2007
  3. Mike Gleason jr Couturier

    Knowing if a button has been clicked in Page_Load

    Mike Gleason jr Couturier, Jul 30, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    571
    Milosz Skalecki [MCAD]
    Jul 31, 2008
  4. Eric
    Replies:
    5
    Views:
    498
  5. Bob
    Replies:
    3
    Views:
    1,430
Loading...

Share This Page