Why?!

Discussion in 'ASP .Net' started by odwrotnie, Jul 30, 2006.

  1. odwrotnie

    odwrotnie Guest

    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.
     
    odwrotnie, Jul 30, 2006
    #1
    1. Advertising

  2. odwrotnie

    Teemu Keiski Guest

    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








    "odwrotnie" <> wrote in message
    news:eek:p.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.
     
    Teemu Keiski, Jul 30, 2006
    #2
    1. Advertising

  3. odwrotnie

    odwrotnie Guest

    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!

    On Sun, 30 Jul 2006 19:42:37 +0200, Teemu Keiski <>
    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.
     
    odwrotnie, Jul 30, 2006
    #3
  4. odwrotnie

    Teemu Keiski Guest

    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





    "odwrotnie" <> wrote in message
    news:eek:p.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("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 <>
    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.
     
    Teemu Keiski, Jul 31, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    Why, why, why???

    =?Utf-8?B?VGltOjouLg==?=, Jan 27, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    573
    Juan T. Llibre
    Jan 27, 2005
  2. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    464
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  3. Mr. SweatyFinger

    VWD why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    384
    =?Utf-8?B?Q2lhcmFuIE8nJycnRG9ubmVsbA==?=
    Dec 21, 2006
  4. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    912
    Mark Rae
    Dec 21, 2006
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,003
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page