希登菲尔德失去了价值

时间:2016-02-04 12:34:21

标签: javascript c# jquery asp.net hidden-field

我在同一页面上有一个表单和一个GridView。当用户保存表单时,它会创建一个ID。我需要将id设置为HiddenField。然后我需要GridView的HiddenField。但是一旦代码完成表单的save方法,HiddenField就会重置为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>

我将保存按钮放在更新面板中以阻止页面刷新和关闭(通常一旦单击保存按钮,它将关闭表单,但我需要它保持打开状态。)

在页面顶部我放了HiddenField:

<asp:HiddenField ID="hfNewID" runat="server" />

然后在btnSaveMaintenance_Click方法中设置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();
        }
}

但是当我尝试在另一个函数中调用HiddenField时,保持返回空白。如何让HiddenField保持值?

1 个答案:

答案 0 :(得分:2)

请试试这个: 将隐藏字段放在updatePanel中,在contentTemplate下,而不是在页面顶部

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