从另一个页面访问控件.ASP.Net

本文关键字:控件 ASP Net 访问 另一个 | 更新日期: 2023-09-27 18:31:36

嗯,情况就是这样...我在 Page1.aspx 中有一个元素 ( <h2 id="test"></h2>),我想从 Page2 更改它.aspx(用户的管理区域...),有点...

test.InnerText = "testText";

如何从第二页访问此控件?这可能吗?

像往常一样,谢谢你们...

从另一个页面访问控件.ASP.Net

您需要获取表单的实例。 请参阅下面的两个表单项目表格 1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Form2 form2;
        public Form1()
        {
            InitializeComponent();
            form2 = new Form2(this);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            form2.Show();
            string  results = form2.GetData();
        }
    }
}

表格 2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        Form1 form1;
        public Form2(Form1 nform1)
        {
            InitializeComponent();
            this.FormClosing +=  new FormClosingEventHandler(Form2_FormClosing);
            form1 = nform1;
            form1.Hide();
        }
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            //stops form from closing
            e.Cancel = true;
            this.Hide();
        }
        public string GetData()
        {
            return "The quick brown fox jumped over the lazy dog.";
        }
    }
}