Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Why?!

Reply
Thread Tools

Why?!

 
 
odwrotnie
Guest
Posts: n/a
 
      07-30-2006
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 .

--
Best regards,
Odwrotnie.
 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      07-30-2006
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("Tex tBox1");

//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" <(E-Mail Removed)> wrote in message
newsp.tdh8njzvdvqlmf@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 .

--
Best regards,
Odwrotnie.


 
Reply With Quote
 
 
 
 
odwrotnie
Guest
Posts: n/a
 
      07-30-2006
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("Tex tBox1");

//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 <(E-Mail Removed)>
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("Tex tBox1");
> //Get the Label from the current page
> Label Label1 = (Label)Page.Master.FindControl("Label1");
> //Set the Text
> Label1.Text = tb.Text;
> }


--
Best regards,
Odwrotnie.
 
Reply With Quote
 
Teemu Keiski
Guest
Posts: n/a
 
      07-31-2006
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("Tex tBox1");

//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" <(E-Mail Removed)> wrote in message
newsp.tdimugo1dvqlmf@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("Tex tBox1");

//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 <(E-Mail Removed)>
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("Tex tBox1");
> //Get the Label from the current page
> Label Label1 = (Label)Page.Master.FindControl("Label1");
> //Set the Text
> Label1.Text = tb.Text;
> }


--
Best regards,
Odwrotnie.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments