繁体   English   中英

使用C#将文本从asp.net母版页中的文本框中输入到contentpage中

[英]getting text into contentpage from textbox which is in masterpage in asp.net using c#

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

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

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");
    }
}
// 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;
}

试试这个,把它放在代码后面的按钮上

//.aspx页

这是受.cs页保护的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!";
        }

        }

暂无
暂无

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

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