Reputation: 2004
I have a form and a GridView on the same page. When the user saves the form, it creates an ID. I need to set the id to a HiddenField. I then need that HiddenField for the GridView. But as soon as the code finishes the save method of the form, the HiddenField gets reset to 0.
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSaveMaintenance" />
</Triggers>
</asp:UpdatePanel>
<tr>
<td>
<asp:LinkButton ID="btnSaveMaintenance" OnClientClick="return ValidateSaveWithoutPieces()" OnClick="btnSaveMaintenance_Click" runat="server" style="float:left" CssClass="btnSaveSmall" ></asp:LinkButton>
</td>
I put the save button in an update panel to stop the page from refreshing and closing (Normally once the save button is clicked it will close the form but I needed it to stay open).
At the top of the page I put the HiddenField:
<asp:HiddenField ID="hfNewID" runat="server" />
Then in the btnSaveMaintenance_Click
method I set the value of the HiddenField
protected void btnSaveMaintenance_Click(object sender, EventArgs e)
{
int tmpParentID = tmpID;
if (ParentID.HasValue)
tmpParentID = ParentID.Value;
Maintenance newMain = new Maintenance
{
ID = tmpID,
Company_ID = Company.Current.CompanyID,
VehicleTrailer = tmpType,
LinkedID = (long)tmpParentID,
DBRowStatus = JobPiece.RowStatus.ToCreate
};
main.Add(newMain);;
hfNewID.Value = tmpID.ToString(); //set value to hiddenfield
if (ParentID.HasValue)
{
Save(Reg, ParentID.Value);
List<Maintenance> Newmain = Maintenance.GetMainteneceItemsByParentID(Company.Current.CompanyID,
ParentID.Value,
PageType,
"Active");
ViewState["Maintenance"] = Newmain;
gvMaintenance.DataSource = Newmain;
gvMaintenance.DataBind();
udpMain.Update();
}
}
But when I try call the HiddenField in another function is keeping returning blank. How do I get the HiddenField to keep the value?
Upvotes: 0
Views: 486
Reputation: 892
Please try this: Put your hidden field inside your updatePanel, under contentTemplate, and not on top of page
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
// you have to put your hidden in this place
<asp:HiddenField ID="hfNewID" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSaveMaintenance" />
</Triggers>
</asp:UpdatePanel>
Upvotes: 2