HtmlInputHidden in a datagrid loses it's value after postback [modified]
-
I have a datagrid that contains an HtmlInputHidden, something like:
<asp:datagrid id="dgCurrency" runat="server" CssClass="grid" AllowPaging="False" CellPadding="0" GridLines="Vertical" AutoGenerateColumns="False" ShowFooter="false" > <Columns> <asp:TemplateColumn HeaderText="ISO kode" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="150px"> <ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle> <ItemTemplate> <asp:Label id="lblISOCode" runat="server" CssClass="tbStyle1"> <%#DataBinder.Eval(Container.DataItem, "strISOCode").ToString()%> </asp:Label> <input type="hidden" runat="server" id="inCurrencyID" value='<%#DataBinder.Eval(Container.DataItem, "lngCurrencyID").ToString()%>' /> </ItemTemplate> </asp:TemplateColumn> ...
Where the inCurrencyID is my HtmlInputHidden. At the first time my page loads, I add a DataView to the dgCurrency datasource. When I push a button the first time, the inCurrencyID has it's value, as in:foreach (DataGridItem dgi in dgCurrency.Items) { inCurrencyID = (HtmlInputHidden)dgi.FindControl("inCurrencyID"); int lngCurrencyID = int.Parse(inCurrencyID.Value); ...
After this I don't databind the dgCurrency to a new datasource, I just leave it as it is. Now the next time I push the same button, it has now lost it's value???? The same goes for the label lblISOCode. The strange thing is that I also have an TextBox in the DataGrid, and that control stil have it's Text. Dose anyone know why, or is there another hidden fild that I can use that dosen't lose it's value on a PostBack without a databind()? Thanks Thomasmodified on Thursday, May 15, 2008 9:47 AM
-
I have a datagrid that contains an HtmlInputHidden, something like:
<asp:datagrid id="dgCurrency" runat="server" CssClass="grid" AllowPaging="False" CellPadding="0" GridLines="Vertical" AutoGenerateColumns="False" ShowFooter="false" > <Columns> <asp:TemplateColumn HeaderText="ISO kode" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="150px"> <ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle> <ItemTemplate> <asp:Label id="lblISOCode" runat="server" CssClass="tbStyle1"> <%#DataBinder.Eval(Container.DataItem, "strISOCode").ToString()%> </asp:Label> <input type="hidden" runat="server" id="inCurrencyID" value='<%#DataBinder.Eval(Container.DataItem, "lngCurrencyID").ToString()%>' /> </ItemTemplate> </asp:TemplateColumn> ...
Where the inCurrencyID is my HtmlInputHidden. At the first time my page loads, I add a DataView to the dgCurrency datasource. When I push a button the first time, the inCurrencyID has it's value, as in:foreach (DataGridItem dgi in dgCurrency.Items) { inCurrencyID = (HtmlInputHidden)dgi.FindControl("inCurrencyID"); int lngCurrencyID = int.Parse(inCurrencyID.Value); ...
After this I don't databind the dgCurrency to a new datasource, I just leave it as it is. Now the next time I push the same button, it has now lost it's value???? The same goes for the label lblISOCode. The strange thing is that I also have an TextBox in the DataGrid, and that control stil have it's Text. Dose anyone know why, or is there another hidden fild that I can use that dosen't lose it's value on a PostBack without a databind()? Thanks Thomasmodified on Thursday, May 15, 2008 9:47 AM
Possibly enableviewstate flag needs to be set to true. You are using a standard html input item, rather than a dotnet control. This is ok since you set the runat server attribute. You may also want to set the enableviewstate="true" attribute as well. Not sure if this solves it, but worth a try. An alternative would be to use an 'asp:label' tag and set it's visibility to false. This gives you a fully qualified dotnet control, yet is not displayed to the user.
Steve