开发者

ASP.NET: rendering content after master is already rendered

I have a button on a content page which is suppose to modify the value of a textbox on the master page. The setup is simple enough.

Content Page - Code Behind - Button Click Handler:

protected void but_mybutton_Click(object sender, EventArgs e)
{
    string new_value = "!!";
    Master.textbox_value(new_value);
}

The prob开发者_StackOverflow社区lem is that by the time it get to that function, the master's page is already rendered. To get the new value to display, I would need to re-fresh the page. Is there anyway to explicitly tell a page to re-render because I have changed some values on it's controls without having to re-fresh the page?


The master page renders indeed like it's a control on the page. This blogpost shows you the full page life cycle including the events of the master page.

http://weblogs.asp.net/ricardoperes/archive/2009/03/08/asp-net-page-events-lifecycle.aspx

From the code you have provided this should work as the OnClick handler of the button gets executed before the OnPreRender of the masterpage. Can you show us the code of what

Master.textbox_value(new_value);

does?

I just tested this with the most simple setup. On the masterpage 1 label and a method that sets the text of the label.

public partial class SiteMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e) {}

    public void SetLabelText(string text) {
        this.Label1.Text = text;
    }
}

On the content page a button. The onclick handler of the button calls the method on the masterpage.

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) {}

    protected void Button1_Click(object sender, EventArgs e)
    {
        ((SiteMaster)this.Master).SetLabelText("foo");
    }
}

This sets the text of the label in the same postback as expected.


You will need to review the ASP.NET Page Life Cycle to get a better understanding of how the events are firing.

Your best bet is to try setting the value on the page OnInit or LoadViewstate events.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜