Home > Backend Development > C++ > How Can I Access and Modify Controls on One ASP.NET Page from Another?

How Can I Access and Modify Controls on One ASP.NET Page from Another?

Mary-Kate Olsen
Release: 2025-02-01 15:31:47
Original
487 people have browsed it

How Can I Access and Modify Controls on One ASP.NET Page from Another?

In the asp.net across page access control

Assume that you have an element in Page1.aspx, such as

& gt; label, you want to modify it from another page (Page2.aspx). Can you access and operate this control remotely?

<h2 Solution

In ASP.NET, you can access the control by obtaining a form instance. For this, please consider the following technologies:

Step 1: Create a new project

Create a new ASP.NET Web window application in Visual Studio. Step 2: Design page1.aspx

In Page1.aspx, add a

& gt; label:

Step 3: Design Page2.aspx

<h2

In Page2.aspx (you want to modify the position of the control), add the following code to the code hidden file (.spx.cs):
<h2></h2>
Copy after login

<明> Explanation:

<索> Examples of the previous page (PAGE1.ASPX).
using System.Web.UI.HtmlControls;

public partial class Page2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // 获取Page1.aspx的实例
        Page1 page1 = (Page1)Page.GetPreviousPage();

        // 将其转换为HTMLControl
        HtmlGenericControl h2 = (HtmlGenericControl)page1.FindControl("test");

        // 修改InnerText属性
        h2.InnerText = "已修改的文本";
    }
}
Copy after login

convert to allow you to access the attribute of & gt; label.

    Modify to change the text displayed in
  • & gt; tags (from Page2.aspx) on Page1.aspx. Page.GetPreviousPage()
  • Additional description: HtmlGenericControl <h2 InnerText
  • This technology can be applied to any type of server control.
  • InnerText To access the control control from multiple levels, you may need to change the previous page multiple times. <h2
  • Using this method, you can maintain the status between different pages in web applications.

The above is the detailed content of How Can I Access and Modify Controls on One ASP.NET Page from Another?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template