1

我有一个母版页和一个内容页。在我的母版页中,我有一个文本框、一个按钮和一个内容占位符。目的是使用用户在母版页中的文本框中输入的文本更改内容页中的文本(不分配到任何字段,只是内容页的文本/内容)。这是问题所在

即使我更改了文本框中的文本并单击了按钮,上一个文本也已更新,并且内容页面中没有任何更改...这是我在母版页中的代码

public partial class example : System.Web.UI.MasterPage
{
    FileInfo fil = new FileInfo("c:/documents and settings/administrator/my documents/visual studio 2010/Projects/WebApplication1/WebApplication1/contentpage.aspx");
    protected void Page_Load(object sender, EventArgs e)
    {
        contenttext.Text = File.ReadAllText(fil.ToString());
    }

    protected void clicked_Click(object sender, EventArgs e)
    {
        File.WriteAllText(fil.ToString(),this.contenttext.Text);
        Response.Redirect("contentpage.aspx");
    }
}
4

2 回答 2

1
// Gets a reference to a TextBox control that not in 
// a ContentPlaceHolder
Textbox txt = (Textbox) Master.FindControl("masterPageLabel");
if(txt != null)
{
    Textbox1.Text = Textbox2.Text;
}

试试这个,然后点击后面代码中的按钮

于 2012-09-26T11:06:22.867 回答
0

//.aspx页面

这是 .cs page protected void Page_Load(object sender, EventArgs e) { for (int i = 1; i <= 5; i++) { TextBox tb = new TextBox(); tb.ID = "文本框" + i.ToString(); tb.Attributes.Add("runat", "server"); MyPanel.Controls.Add(tb); } }

    protected void btnReadTextBoxValue_Click(object sender, EventArgs e)
    {
        for (int i = 1; i <=5; i++)
        {
//Append the master page content place holder id with textbox then it find value and      work
            string str ="ctl00$ContentPlaceHolder3$"+"textbox" + i.ToString();
            TextBox retrievedTextBox = FindControl(str) as TextBox;
        if (retrievedTextBox != null)
        {
            lblResult.Text = ((TextBox)retrievedTextBox).Text;
            break;
        }
        else
        {
            lblResult.Text = "No text box has been created!";
        }

        }
于 2014-04-05T19:26:33.767 回答