【问题标题】:getting text into contentpage from textbox which is in masterpage in asp.net using c#使用 c# 从 asp.net 母版页中的文本框获取文本到内容页
【发布时间】:2012-09-26 10:24:13
【问题描述】:

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

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

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

【问题讨论】:

  • ...在内容页面...它是空的?请添加缺少的代码!
  • 请进一步明确您的查询

标签: c# asp.net


【解决方案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;
}

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

【讨论】:

  • thnx... 还有一个疑问院长.... 在按钮单击事件方法中,我想获取文本框中的文本。按钮和文本框都在母版页中。按钮单击方法在其 .cs 文件中... thnx dean提前
【解决方案2】:

//.aspx 页面

这是.cs页面 protected void Page_Load(object sender, EventArgs e) { for (int i = 1; i

    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!";
        }

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多