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 :|.
--
Best regards,
Odwrotnie.
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
"odwrotnie" <kru...@gazeta.pl> wrote in message
news:op.tdh8njzvdvqlmf@odwrotnie...
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!
On Sun, 30 Jul 2006 19:42:37 +0200, Teemu Keiski <jot...@aspalliance.com>
wrote:
> 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;
> }
--
Best regards,
Odwrotnie.
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
"odwrotnie" <kru...@gazeta.pl> wrote in message
news:op.tdimugo1dvqlmf@odwrotnie...