简体   繁体   English

从ASP.NET中另一个页面的链接按钮访问复选框的属性

[英]Accessing Properties of checkbox from link button of another page in ASP.NET

I am creating a web application using asp.net I'd like to know how to alter properties of a checkbox of one page from another page: Eg: my Page2.aspx page has a checkbox with id checkbox1 and by default its visibility is false. 我正在使用asp.net创建一个Web应用程序我想知道如何从另一个页面改变一个页面的复选框的属性:例如:我的Page2.aspx页面有一个id为checkbox1的复选框,默认情况下它的可见性为false 。 Now i need to set the visibility of checkbox1 to true from another page Page1.aspx with click event of a link button with id linkbutton1. 现在我需要将另一个页面Page1.aspx中checkbox1的可见性设置为true,并使用带有id linkbutton1的链接按钮的单击事件。 any help in this regards please? 请问有什么帮助吗?

Since the web is stateless, each page within a .NET website or web application loads independently of one another. 由于Web是无状态的,因此.NET网站或Web应用程序中的每个页面都彼此独立地加载。 Therefore, you're not able to directly control elements on one .aspx from another .aspx. 因此,您无法直接从另一个.aspx控制一个.aspx上的元素。

However, you would be able to store the desired settings for a control when Page1.aspx posts back and then use the settings saved from Page1.aspx to load the desired settings when Page2.aspx is loaded. 但是,当Page1.aspx发回时,您可以存储控件的所需设置,然后使用从Page1.aspx保存的设置加载Page2.aspx时加载所需的设置。

I'm not a huge fan of using Session management, but something like this would work: 我不是使用会话管理的忠实粉丝,但这样的东西会起作用:

The following event could exist on Page One. 第一页上可能存在以下事件。

protected void btnPageOne_Click(object sender, EventArgs e)
{
    Session["PageTwoIsChecked"] = true;
}

Then when Page Two is loaded, you could check the session information set in Page One. 然后,当加载第二页时,您可以检查第一页中设置的会话信息。

    protected void Page_Load(object sender, EventArgs e)
    {
       if (Session["PageTwoIsChecked"] != null && Convert.ToBoolean(Session["PageTwoIsChecked"]) == true)
       {
           chkPageTwo.Visible = true;
       }
    }

I hope this helps! 我希望这有帮助!

You can use this approach also 您也可以使用此方法

page1.aspx page1.aspx这个

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>

<body>
    <form id="form1" runat="server">
    <div>
    <asp:linkbutton ID="Linkbutton1" runat="server" onclick="Linkbutton1_Click">LinkButton</asp:linkbutton>
    </div>
    </form>
</body>
</html> 

page1.aspx.cs page1.aspx.cs

protected void Linkbutton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("page2.aspx?visible=1");//this is how to control the visibility 
    }

and page2.aspx.cs 和page2.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        //Request.QueryString["visible"].ToString() will be same

        if (Request.QueryString[0].ToString() != "1")
        {
            CheckBox1.Visible = false;
        }
        else
        {
            CheckBox1.Visible = true;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM