Why?!

O

odwrotnie

Hi,

I have a problem...

My website has a masterpage and 2 pages (Start.aspx and Button.aspx)
connected with this masterpage. On my masterpage there are textbox, button
and label.

This button has postbackurl set to Button.aspx and

protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = TextBox1.Text;
}

Why Label1.Text stays unchanged?

When button postbackurl is nothing it works fine :|.
 
T

Teemu Keiski

Hi,

involving cross-page postback changes things a bit since it basically
involves two pages. Even though they share same master-page, it does not
mean that it would be same instance. E.g pages are different instances.
Therefore having code like this either on Master's Page_Load or
Button.aspx's Page_Load would solve it

if (Page.PreviousPage != null)
{
//Get the TextBox from the previous page (source of cross-page
post)
TextBox tb =
(TextBox)Page.PreviousPage.Master.FindControl("TextBox1");

//Get the Label from the current page
Label Label1 = (Label)Page.Master.FindControl("Label1");

//Set the Text
Label1.Text = tb.Text;
}


--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke








Hi,

I have a problem...

My website has a masterpage and 2 pages (Start.aspx and Button.aspx)
connected with this masterpage. On my masterpage there are textbox, button
and label.

This button has postbackurl set to Button.aspx and

protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = TextBox1.Text;
}

Why Label1.Text stays unchanged?

When button postbackurl is nothing it works fine :|.
 
O

odwrotnie

In this example Page.PreviousPage is always null :|.

Also strange thing for me is that when I click the button for the first
time it doesnt run Button1_Click method. Only when clicked second time
(when it shows Button.aspx it runs the Button1_Click :/.

Some code from MasterPage.aspx:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Button" PostBackUrl="~/Button.aspx" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

.... and MasterPage.cs:

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

}
protected void Button1_Click(object sender, EventArgs e)
{
if (Page.PreviousPage != null)
{
//Get the TextBox from the previous page (source of cross-page
post)
TextBox tb =
(TextBox)Page.PreviousPage.Master.FindControl("TextBox1");

//Get the Label from the current page
Label lb = (Label)Page.Master.FindControl("Label1");

//Set the Text
lb.Text = tb.Text;
}
}
}

Thanks!
 
T

Teemu Keiski

Hi,

The code I gave would end into Page_Load of the master page not into Button
click. I built exact repro of your scenario and the code is from there.

protected void Page_Load(object sender, EventArgs e)
{
if (Page.PreviousPage != null)
{
//Get the TextBox from the previous page
TextBox tb =
(TextBox)Page.PreviousPage.Master.FindControl("TextBox1");

//Get the Label from the current page
Label Label1 = (Label)Page.Master.FindControl("Label1");

//Set the Text
Label1.Text = tb.Text;
}
}


--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke





In this example Page.PreviousPage is always null :|.

Also strange thing for me is that when I click the button for the first
time it doesnt run Button1_Click method. Only when clicked second time
(when it shows Button.aspx it runs the Button1_Click :/.

Some code from MasterPage.aspx:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Button" PostBackUrl="~/Button.aspx" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

.... and MasterPage.cs:

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

}
protected void Button1_Click(object sender, EventArgs e)
{
if (Page.PreviousPage != null)
{
//Get the TextBox from the previous page (source of cross-page
post)
TextBox tb =
(TextBox)Page.PreviousPage.Master.FindControl("TextBox1");

//Get the Label from the current page
Label lb = (Label)Page.Master.FindControl("Label1");

//Set the Text
lb.Text = tb.Text;
}
}
}

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top