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

G

George

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.

--------------------
 
L

Leonardo Rodríguez

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: (e-mail address removed)
Location : Bs.As., Argentina
 
L

Leonardo Rodríguez

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

Sorry, Before!
--

Leonardo C. Rodríguez
Email at: (e-mail address removed)
Location : Bs.As., Argentina



Leonardo Rodríguez said:
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: (e-mail address removed)
Location : Bs.As., Argentina



George said:
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.

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top