如何从aspx.cs页面获取隐藏字段值到aspx.cs页面

本文关键字:cs aspx 字段 页面 隐藏 获取 | 更新日期: 2023-09-27 18:10:06

my ascx code:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Demo.ascx.cs" Inherits="Demo" %>
<asp:HiddenField ID="hidden1" runat="server" Value=""/>
<asp:Label ID="lbl1" runat="server" Text="Hiii"></asp:Label>

my asx .cs code:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lbl1.Text = "Mahendra Kamble";
            hidden1.Value = "123";
        }   

    }

my aspx code:

<%@ Register TagName="TopBar" TagPrefix="CRS" Src="~/Demo.ascx" %>
---
--
<div>
        <CRS:TopBar ID="Hide" runat="server" Visible="false"/>
</div>

my aspx.cs code

 if (!IsPostBack)
        {

            //Label lb = (Label)Hide.FindControl("lbl1");
            // Response.Write("---" + lb.Text);
            //HiddenField hf = (HiddenField)Hide.FindControl("hidden1");
            //Response.Write("---" + hf.Value);
            // UserControl control = (UserControl)LoadControl("Demo.ascx");
            Demo uc = (Demo)Page.LoadControl("Demo.ascx");
            // HiddenField hf1 = (HiddenField)control.FindControl("hidden1");
            HiddenField hide = (HiddenField)uc.FindControl("hidden1");
            Response.Write("--- " + hide.Value + " Value ");
            form1.Controls.Add(uc);
        }

现在我应该如何得到隐藏字段的值,即隐藏1。值="123"在这个aspx.cs页面?

如何从aspx.cs页面获取隐藏字段值到aspx.cs页面

您可以将隐藏字段的值传递给viewstate属性。因此,您可以设置或获取隐藏字段的值。