beginner quesion about IsPostBack

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default beginner quesion about IsPostBack

    hi,

    i have an aspx page with a button. if i step through my code when i click
    the button then it calls the button_Click event which is what i want.

    but i need to redirect the page to another aspx page when this button is
    clicked so i dont want to waste time rendering the same page before
    redirecting, so i put the following code in, but now the button_click event
    doesn't get called :(

    what am i doing wrong? how can i render a page the first time its called,
    but if the button is clicked , then immediately call the button_Click event.

    private void Page_Load(object sender, System.EventArgs e)

    {

    if (!(Page.IsPostBack))

    {

    //perform main rendering of the page here.

    }

    }

    by the way, here is my click event code:

    private void button_Click(object sender, EventArgs e)

    {

    Response.Redirect("anotherpage.aspx";

    }


    suzy Guest

  2. Similar Questions and Discussions

    1. Not IsPostBack
      I have a problem. I have a function Load_Info where I am generating dymanic table. On a first load is it fine however after the refresh because it...
    2. IsPostBack ??
      Hi, how can I initialize data kept in the Session-object only the first time a WS is invoked and not everytime the WS is invoked. I tried : ...
    3. IsPostBack issues??
      I have a datagrid in my development environment and it works just fine. I am able to edit and so forth. I moved it to my production environment...
    4. Composite Controls and Me.Page.IsPostback Problem
      Hi all, In my Composite Control's Sub New Function I am unable to check if Me.Page.IsPostback. Me.Page is Nothing (so i get a...
    5. Manually get the text that a certain control posted when in IsPostBack mode...
      I have a page with EnableViewState="false". That page contains a DropDownList with dynamic items and a submit Button. I need to get the...
  3. #2

    Default Re: beginner quesion about IsPostBack

    sorry that was just a typo.

    there is no error, it just renders the page, then calls the click event.


    "David Wier" <dwier@nospamASPNet101.com> wrote in message
    news:u5oZTQrWDHA.536@TK2MSFTNGP10.phx.gbl...
    > Do you get an error?
    > Try:
    > Response.Redirect("anotherpage.aspx");
    >
    > David Wier
    > [url]http://aspnet101.com[/url]
    > [url]http://aspexpress.com[/url]
    >
    >
    > "suzy" <suzy@nospam.com> wrote in message
    > news:OWDWeLrWDHA.2268@TK2MSFTNGP11.phx.gbl...
    > > hi,
    > >
    > > i have an aspx page with a button. if i step through my code when i
    click
    > > the button then it calls the button_Click event which is what i want.
    > >
    > > but i need to redirect the page to another aspx page when this button is
    > > clicked so i dont want to waste time rendering the same page before
    > > redirecting, so i put the following code in, but now the button_click
    > event
    > > doesn't get called :(
    > >
    > > what am i doing wrong? how can i render a page the first time its
    called,
    > > but if the button is clicked , then immediately call the button_Click
    > event.
    > >
    > > private void Page_Load(object sender, System.EventArgs e)
    > >
    > > {
    > >
    > > if (!(Page.IsPostBack))
    > >
    > > {
    > >
    > > //perform main rendering of the page here.
    > >
    > > }
    > >
    > > }
    > >
    > > by the way, here is my click event code:
    > >
    > > private void button_Click(object sender, EventArgs e)
    > >
    > > {
    > >
    > > Response.Redirect("anotherpage.aspx";
    > >
    > > }
    > >
    > >
    >
    >

    suzy Guest

  4. #3

    Default beginner quesion about IsPostBack

    If you just want to redirect to a different page and not
    do anything else you can use hyperlink instead of the
    button.

    If you are going to be doing something before redirecting
    then you have the code for rendering inside a if not
    ispostback event so you are not wasting time rendering it
    again. Because this will be skipped when the user clicks
    on the button.

    You can see for yourself by setting break points and
    stepping through the code if you are using an IDE.

    >-----Original Message-----
    >hi,
    >
    >i have an aspx page with a button. if i step through my
    code when i click
    >the button then it calls the button_Click event which is
    what i want.
    >
    >but i need to redirect the page to another aspx page when
    this button is
    >clicked so i dont want to waste time rendering the same
    page before
    >redirecting, so i put the following code in, but now the
    button_click event
    >doesn't get called :(
    >
    >what am i doing wrong? how can i render a page the first
    time its called,
    >but if the button is clicked , then immediately call the
    button_Click event.
    >
    >private void Page_Load(object sender, System.EventArgs e)
    >
    >{
    >
    > if (!(Page.IsPostBack))
    >
    > {
    >
    > //perform main rendering of the page here.
    >
    > }
    >
    >}
    >
    >by the way, here is my click event code:
    >
    >private void button_Click(object sender, EventArgs e)
    >
    >{
    >
    > Response.Redirect("anotherpage.aspx";
    >
    >}
    >
    >
    >.
    >
    makthar Guest

  5. #4

    Default Re: beginner quesion about IsPostBack

    You might want to consider adding to the onclick attribute for the redirect.
    That way it would say the whole postback/redirect.

    in the page load

    button.Attributes["OnClick"] = "javascript : self.location.href =
    'anotherpage.aspx';"

    HTH,

    bill

    "suzy" <suzy@nospam.com> wrote in message
    news:OWDWeLrWDHA.2268@TK2MSFTNGP11.phx.gbl...
    > hi,
    >
    > i have an aspx page with a button. if i step through my code when i click
    > the button then it calls the button_Click event which is what i want.
    >
    > but i need to redirect the page to another aspx page when this button is
    > clicked so i dont want to waste time rendering the same page before
    > redirecting, so i put the following code in, but now the button_click
    event
    > doesn't get called :(
    >
    > what am i doing wrong? how can i render a page the first time its called,
    > but if the button is clicked , then immediately call the button_Click
    event.
    >
    > private void Page_Load(object sender, System.EventArgs e)
    >
    > {
    >
    > if (!(Page.IsPostBack))
    >
    > {
    >
    > //perform main rendering of the page here.
    >
    > }
    >
    > }
    >
    > by the way, here is my click event code:
    >
    > private void button_Click(object sender, EventArgs e)
    >
    > {
    >
    > Response.Redirect("anotherpage.aspx";
    >
    > }
    >
    >

    William F. Robertson, Jr. Guest

  6. #5

    Default Re: beginner quesion about IsPostBack

    yes i am dynamically creating the button.

    i didnt realise this would make a difference , because the click event gets
    called if the "if" statement is not there. the downside to this is that it
    performs unnecessary actions by rendering the current page before
    redirecting.


    "Marina" <zlatkinam@nospam.hotmail.com> wrote in message
    news:equ$dXrWDHA.652@TK2MSFTNGP10.phx.gbl...
    > Are you dynamically creating the button? In which case, you must recreate
    it
    > on postback.
    >
    > "suzy" <suzy@nospam.com> wrote in message
    > news:OWDWeLrWDHA.2268@TK2MSFTNGP11.phx.gbl...
    > > hi,
    > >
    > > i have an aspx page with a button. if i step through my code when i
    click
    > > the button then it calls the button_Click event which is what i want.
    > >
    > > but i need to redirect the page to another aspx page when this button is
    > > clicked so i dont want to waste time rendering the same page before
    > > redirecting, so i put the following code in, but now the button_click
    > event
    > > doesn't get called :(
    > >
    > > what am i doing wrong? how can i render a page the first time its
    called,
    > > but if the button is clicked , then immediately call the button_Click
    > event.
    > >
    > > private void Page_Load(object sender, System.EventArgs e)
    > >
    > > {
    > >
    > > if (!(Page.IsPostBack))
    > >
    > > {
    > >
    > > //perform main rendering of the page here.
    > >
    > > }
    > >
    > > }
    > >
    > > by the way, here is my click event code:
    > >
    > > private void button_Click(object sender, EventArgs e)
    > >
    > > {
    > >
    > > Response.Redirect("anotherpage.aspx";
    > >
    > > }
    > >
    > >
    >
    >

    suzy Guest

  7. #6

    Default Re: beginner quesion about IsPostBack

    There is a difference. You must recreate the button, otherwise your event
    will not get called.

    "suzy" <suzy@nospam.com> wrote in message
    news:%238sA7wrWDHA.3924@tk2msftngp13.phx.gbl...
    > yes i am dynamically creating the button.
    >
    > i didnt realise this would make a difference , because the click event
    gets
    > called if the "if" statement is not there. the downside to this is that
    it
    > performs unnecessary actions by rendering the current page before
    > redirecting.
    >
    >
    > "Marina" <zlatkinam@nospam.hotmail.com> wrote in message
    > news:equ$dXrWDHA.652@TK2MSFTNGP10.phx.gbl...
    > > Are you dynamically creating the button? In which case, you must
    recreate
    > it
    > > on postback.
    > >
    > > "suzy" <suzy@nospam.com> wrote in message
    > > news:OWDWeLrWDHA.2268@TK2MSFTNGP11.phx.gbl...
    > > > hi,
    > > >
    > > > i have an aspx page with a button. if i step through my code when i
    > click
    > > > the button then it calls the button_Click event which is what i want.
    > > >
    > > > but i need to redirect the page to another aspx page when this button
    is
    > > > clicked so i dont want to waste time rendering the same page before
    > > > redirecting, so i put the following code in, but now the button_click
    > > event
    > > > doesn't get called :(
    > > >
    > > > what am i doing wrong? how can i render a page the first time its
    > called,
    > > > but if the button is clicked , then immediately call the button_Click
    > > event.
    > > >
    > > > private void Page_Load(object sender, System.EventArgs e)
    > > >
    > > > {
    > > >
    > > > if (!(Page.IsPostBack))
    > > >
    > > > {
    > > >
    > > > //perform main rendering of the page here.
    > > >
    > > > }
    > > >
    > > > }
    > > >
    > > > by the way, here is my click event code:
    > > >
    > > > private void button_Click(object sender, EventArgs e)
    > > >
    > > > {
    > > >
    > > > Response.Redirect("anotherpage.aspx";
    > > >
    > > > }
    > > >
    > > >
    > >
    > >
    >
    >

    Marina Guest

  8. #7

    Default Re: beginner quesion about IsPostBack

    when you say it worked, do you mean it DIDNT render the main page before
    redirecting???

    that is what i am trying to prevent... the unnecessary rendering before the
    redirection.

    "David Wier" <dwier@nospamASPNet101.com> wrote in message
    news:uc0kmyrWDHA.1900@TK2MSFTNGP10.phx.gbl...
    > That's really strange - - - I just copied your code to a new file, ran it
    > and it worked just fine - - I even added other code...
    > Here's the way I did it - - -
    >
    > <html>
    > <head>
    > <script language="C#" Runat="server">
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > if (!(Page.IsPostBack))
    > {
    > //perform main rendering of the page here.
    > ddl.Items.Add("Red");
    > ddl.Items.Add("Blue");
    > ddl.Items.Add("Green");
    > ddl.Items.Add("Purple");
    > }
    > }
    > private void button_Click(object sender, EventArgs e)
    > {
    > Response.Redirect([url]http://microsoft.com);[/url]
    > }
    > </script>
    > </head>
    > <body>
    > <form id="form1" Runat="server">
    > My text is here<asp:DropDownList id="ddl" Runat="server">
    > </asp:DropDownList><p>
    > <asp:Button id="button1" Text="click here" onclick="button_Click"
    > Runat="server" />
    > </form>
    > </body>
    > </html>
    >
    > David Wier
    > [url]http://aspnet101.com[/url]
    > [url]http://aspexpress.com[/url]
    >
    >
    > "suzy" <suzy@nospam.com> wrote in message
    > news:uL2GUVrWDHA.2204@TK2MSFTNGP12.phx.gbl...
    > > sorry that was just a typo.
    > >
    > > there is no error, it just renders the page, then calls the click event.
    > >
    > >
    > > "David Wier" <dwier@nospamASPNet101.com> wrote in message
    > > news:u5oZTQrWDHA.536@TK2MSFTNGP10.phx.gbl...
    > > > Do you get an error?
    > > > Try:
    > > > Response.Redirect("anotherpage.aspx");
    > > >
    > > > David Wier
    > > > [url]http://aspnet101.com[/url]
    > > > [url]http://aspexpress.com[/url]
    > > >
    > > >
    > > > "suzy" <suzy@nospam.com> wrote in message
    > > > news:OWDWeLrWDHA.2268@TK2MSFTNGP11.phx.gbl...
    > > > > hi,
    > > > >
    > > > > i have an aspx page with a button. if i step through my code when i
    > > click
    > > > > the button then it calls the button_Click event which is what i
    want.
    > > > >
    > > > > but i need to redirect the page to another aspx page when this
    button
    > is
    > > > > clicked so i dont want to waste time rendering the same page before
    > > > > redirecting, so i put the following code in, but now the
    button_click
    > > > event
    > > > > doesn't get called :(
    > > > >
    > > > > what am i doing wrong? how can i render a page the first time its
    > > called,
    > > > > but if the button is clicked , then immediately call the
    button_Click
    > > > event.
    > > > >
    > > > > private void Page_Load(object sender, System.EventArgs e)
    > > > >
    > > > > {
    > > > >
    > > > > if (!(Page.IsPostBack))
    > > > >
    > > > > {
    > > > >
    > > > > //perform main rendering of the page here.
    > > > >
    > > > > }
    > > > >
    > > > > }
    > > > >
    > > > > by the way, here is my click event code:
    > > > >
    > > > > private void button_Click(object sender, EventArgs e)
    > > > >
    > > > > {
    > > > >
    > > > > Response.Redirect("anotherpage.aspx";
    > > > >
    > > > > }
    > > > >
    > > > >
    > > >
    > > >
    > >
    > >
    >
    >

    suzy Guest

  9. #8

    Default Re: beginner quesion about IsPostBack

    There is no 'rendering'. The original page never makes it back to the
    browser.

    What does happen, is that you are creating a bunch of objects - there is
    some cost associated with that (though not much). Also, if you have database
    access, there is cost associated with that as well.

    "suzy" <suzy@nospam.com> wrote in message
    news:eyWchQsWDHA.2360@TK2MSFTNGP12.phx.gbl...
    > when you say it worked, do you mean it DIDNT render the main page before
    > redirecting???
    >
    > that is what i am trying to prevent... the unnecessary rendering before
    the
    > redirection.
    >
    > "David Wier" <dwier@nospamASPNet101.com> wrote in message
    > news:uc0kmyrWDHA.1900@TK2MSFTNGP10.phx.gbl...
    > > That's really strange - - - I just copied your code to a new file, ran
    it
    > > and it worked just fine - - I even added other code...
    > > Here's the way I did it - - -
    > >
    > > <html>
    > > <head>
    > > <script language="C#" Runat="server">
    > > private void Page_Load(object sender, System.EventArgs e)
    > > {
    > > if (!(Page.IsPostBack))
    > > {
    > > //perform main rendering of the page here.
    > > ddl.Items.Add("Red");
    > > ddl.Items.Add("Blue");
    > > ddl.Items.Add("Green");
    > > ddl.Items.Add("Purple");
    > > }
    > > }
    > > private void button_Click(object sender, EventArgs e)
    > > {
    > > Response.Redirect([url]http://microsoft.com);[/url]
    > > }
    > > </script>
    > > </head>
    > > <body>
    > > <form id="form1" Runat="server">
    > > My text is here<asp:DropDownList id="ddl" Runat="server">
    > > </asp:DropDownList><p>
    > > <asp:Button id="button1" Text="click here" onclick="button_Click"
    > > Runat="server" />
    > > </form>
    > > </body>
    > > </html>
    > >
    > > David Wier
    > > [url]http://aspnet101.com[/url]
    > > [url]http://aspexpress.com[/url]
    > >
    > >
    > > "suzy" <suzy@nospam.com> wrote in message
    > > news:uL2GUVrWDHA.2204@TK2MSFTNGP12.phx.gbl...
    > > > sorry that was just a typo.
    > > >
    > > > there is no error, it just renders the page, then calls the click
    event.
    > > >
    > > >
    > > > "David Wier" <dwier@nospamASPNet101.com> wrote in message
    > > > news:u5oZTQrWDHA.536@TK2MSFTNGP10.phx.gbl...
    > > > > Do you get an error?
    > > > > Try:
    > > > > Response.Redirect("anotherpage.aspx");
    > > > >
    > > > > David Wier
    > > > > [url]http://aspnet101.com[/url]
    > > > > [url]http://aspexpress.com[/url]
    > > > >
    > > > >
    > > > > "suzy" <suzy@nospam.com> wrote in message
    > > > > news:OWDWeLrWDHA.2268@TK2MSFTNGP11.phx.gbl...
    > > > > > hi,
    > > > > >
    > > > > > i have an aspx page with a button. if i step through my code when
    i
    > > > click
    > > > > > the button then it calls the button_Click event which is what i
    > want.
    > > > > >
    > > > > > but i need to redirect the page to another aspx page when this
    > button
    > > is
    > > > > > clicked so i dont want to waste time rendering the same page
    before
    > > > > > redirecting, so i put the following code in, but now the
    > button_click
    > > > > event
    > > > > > doesn't get called :(
    > > > > >
    > > > > > what am i doing wrong? how can i render a page the first time its
    > > > called,
    > > > > > but if the button is clicked , then immediately call the
    > button_Click
    > > > > event.
    > > > > >
    > > > > > private void Page_Load(object sender, System.EventArgs e)
    > > > > >
    > > > > > {
    > > > > >
    > > > > > if (!(Page.IsPostBack))
    > > > > >
    > > > > > {
    > > > > >
    > > > > > //perform main rendering of the page here.
    > > > > >
    > > > > > }
    > > > > >
    > > > > > }
    > > > > >
    > > > > > by the way, here is my click event code:
    > > > > >
    > > > > > private void button_Click(object sender, EventArgs e)
    > > > > >
    > > > > > {
    > > > > >
    > > > > > Response.Redirect("anotherpage.aspx";
    > > > > >
    > > > > > }
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >
    > >
    > >
    >
    >

    Marina Guest

  10. #9

    Default Re: beginner quesion about IsPostBack

    marina,

    you are absolutely right! i just tried it again but this time i added ONLY
    the button before the "if" statement in the page_load event. now if i click
    the button the button_click event fires without rendering the entire page.

    however, it has left my page looking untidy (and in the wrong order too!) as
    the button is now at the top of the page, rather than at the bottom!

    is there a neat way to get around my problem? it just seems a bit messy :(

    thanks again!


    "Marina" <zlatkinam@nospam.hotmail.com> wrote in message
    news:eWU983rWDHA.2376@TK2MSFTNGP11.phx.gbl...
    > There is a difference. You must recreate the button, otherwise your event
    > will not get called.
    >
    > "suzy" <suzy@nospam.com> wrote in message
    > news:%238sA7wrWDHA.3924@tk2msftngp13.phx.gbl...
    > > yes i am dynamically creating the button.
    > >
    > > i didnt realise this would make a difference , because the click event
    > gets
    > > called if the "if" statement is not there. the downside to this is that
    > it
    > > performs unnecessary actions by rendering the current page before
    > > redirecting.
    > >
    > >
    > > "Marina" <zlatkinam@nospam.hotmail.com> wrote in message
    > > news:equ$dXrWDHA.652@TK2MSFTNGP10.phx.gbl...
    > > > Are you dynamically creating the button? In which case, you must
    > recreate
    > > it
    > > > on postback.
    > > >
    > > > "suzy" <suzy@nospam.com> wrote in message
    > > > news:OWDWeLrWDHA.2268@TK2MSFTNGP11.phx.gbl...
    > > > > hi,
    > > > >
    > > > > i have an aspx page with a button. if i step through my code when i
    > > click
    > > > > the button then it calls the button_Click event which is what i
    want.
    > > > >
    > > > > but i need to redirect the page to another aspx page when this
    button
    > is
    > > > > clicked so i dont want to waste time rendering the same page before
    > > > > redirecting, so i put the following code in, but now the
    button_click
    > > > event
    > > > > doesn't get called :(
    > > > >
    > > > > what am i doing wrong? how can i render a page the first time its
    > > called,
    > > > > but if the button is clicked , then immediately call the
    button_Click
    > > > event.
    > > > >
    > > > > private void Page_Load(object sender, System.EventArgs e)
    > > > >
    > > > > {
    > > > >
    > > > > if (!(Page.IsPostBack))
    > > > >
    > > > > {
    > > > >
    > > > > //perform main rendering of the page here.
    > > > >
    > > > > }
    > > > >
    > > > > }
    > > > >
    > > > > by the way, here is my click event code:
    > > > >
    > > > > private void button_Click(object sender, EventArgs e)
    > > > >
    > > > > {
    > > > >
    > > > > Response.Redirect("anotherpage.aspx";
    > > > >
    > > > > }
    > > > >
    > > > >
    > > >
    > > >
    > >
    > >
    >
    >

    suzy Guest

  11. #10

    Default Re: beginner quesion about IsPostBack

    yes i have database calls during the rendering process, which is why i am
    trying to prevent it.


    :)

    "Marina" <zlatkinam@nospam.hotmail.com> wrote in message
    news:ejwmsSsWDHA.1512@TK2MSFTNGP11.phx.gbl...
    > There is no 'rendering'. The original page never makes it back to the
    > browser.
    >
    > What does happen, is that you are creating a bunch of objects - there is
    > some cost associated with that (though not much). Also, if you have
    database
    > access, there is cost associated with that as well.
    >
    > "suzy" <suzy@nospam.com> wrote in message
    > news:eyWchQsWDHA.2360@TK2MSFTNGP12.phx.gbl...
    > > when you say it worked, do you mean it DIDNT render the main page before
    > > redirecting???
    > >
    > > that is what i am trying to prevent... the unnecessary rendering before
    > the
    > > redirection.
    > >
    > > "David Wier" <dwier@nospamASPNet101.com> wrote in message
    > > news:uc0kmyrWDHA.1900@TK2MSFTNGP10.phx.gbl...
    > > > That's really strange - - - I just copied your code to a new file, ran
    > it
    > > > and it worked just fine - - I even added other code...
    > > > Here's the way I did it - - -
    > > >
    > > > <html>
    > > > <head>
    > > > <script language="C#" Runat="server">
    > > > private void Page_Load(object sender, System.EventArgs e)
    > > > {
    > > > if (!(Page.IsPostBack))
    > > > {
    > > > //perform main rendering of the page here.
    > > > ddl.Items.Add("Red");
    > > > ddl.Items.Add("Blue");
    > > > ddl.Items.Add("Green");
    > > > ddl.Items.Add("Purple");
    > > > }
    > > > }
    > > > private void button_Click(object sender, EventArgs e)
    > > > {
    > > > Response.Redirect([url]http://microsoft.com);[/url]
    > > > }
    > > > </script>
    > > > </head>
    > > > <body>
    > > > <form id="form1" Runat="server">
    > > > My text is here<asp:DropDownList id="ddl" Runat="server">
    > > > </asp:DropDownList><p>
    > > > <asp:Button id="button1" Text="click here" onclick="button_Click"
    > > > Runat="server" />
    > > > </form>
    > > > </body>
    > > > </html>
    > > >
    > > > David Wier
    > > > [url]http://aspnet101.com[/url]
    > > > [url]http://aspexpress.com[/url]
    > > >
    > > >
    > > > "suzy" <suzy@nospam.com> wrote in message
    > > > news:uL2GUVrWDHA.2204@TK2MSFTNGP12.phx.gbl...
    > > > > sorry that was just a typo.
    > > > >
    > > > > there is no error, it just renders the page, then calls the click
    > event.
    > > > >
    > > > >
    > > > > "David Wier" <dwier@nospamASPNet101.com> wrote in message
    > > > > news:u5oZTQrWDHA.536@TK2MSFTNGP10.phx.gbl...
    > > > > > Do you get an error?
    > > > > > Try:
    > > > > > Response.Redirect("anotherpage.aspx");
    > > > > >
    > > > > > David Wier
    > > > > > [url]http://aspnet101.com[/url]
    > > > > > [url]http://aspexpress.com[/url]
    > > > > >
    > > > > >
    > > > > > "suzy" <suzy@nospam.com> wrote in message
    > > > > > news:OWDWeLrWDHA.2268@TK2MSFTNGP11.phx.gbl...
    > > > > > > hi,
    > > > > > >
    > > > > > > i have an aspx page with a button. if i step through my code
    when
    > i
    > > > > click
    > > > > > > the button then it calls the button_Click event which is what i
    > > want.
    > > > > > >
    > > > > > > but i need to redirect the page to another aspx page when this
    > > button
    > > > is
    > > > > > > clicked so i dont want to waste time rendering the same page
    > before
    > > > > > > redirecting, so i put the following code in, but now the
    > > button_click
    > > > > > event
    > > > > > > doesn't get called :(
    > > > > > >
    > > > > > > what am i doing wrong? how can i render a page the first time
    its
    > > > > called,
    > > > > > > but if the button is clicked , then immediately call the
    > > button_Click
    > > > > > event.
    > > > > > >
    > > > > > > private void Page_Load(object sender, System.EventArgs e)
    > > > > > >
    > > > > > > {
    > > > > > >
    > > > > > > if (!(Page.IsPostBack))
    > > > > > >
    > > > > > > {
    > > > > > >
    > > > > > > //perform main rendering of the page here.
    > > > > > >
    > > > > > > }
    > > > > > >
    > > > > > > }
    > > > > > >
    > > > > > > by the way, here is my click event code:
    > > > > > >
    > > > > > > private void button_Click(object sender, EventArgs e)
    > > > > > >
    > > > > > > {
    > > > > > >
    > > > > > > Response.Redirect("anotherpage.aspx";
    > > > > > >
    > > > > > > }
    > > > > > >
    > > > > > >
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >
    > >
    > >
    >
    >

    suzy Guest

  12. #11

    Default Re: beginner quesion about IsPostBack

    judging by what marina said in another post in this thread, i think your
    page might have worked as you aren't dynamically creating your button.



    "David Wier" <dwier@nospamASPNet101.com> wrote in message
    news:uc0kmyrWDHA.1900@TK2MSFTNGP10.phx.gbl...
    > That's really strange - - - I just copied your code to a new file, ran it
    > and it worked just fine - - I even added other code...
    > Here's the way I did it - - -
    >
    > <html>
    > <head>
    > <script language="C#" Runat="server">
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > if (!(Page.IsPostBack))
    > {
    > //perform main rendering of the page here.
    > ddl.Items.Add("Red");
    > ddl.Items.Add("Blue");
    > ddl.Items.Add("Green");
    > ddl.Items.Add("Purple");
    > }
    > }
    > private void button_Click(object sender, EventArgs e)
    > {
    > Response.Redirect([url]http://microsoft.com);[/url]
    > }
    > </script>
    > </head>
    > <body>
    > <form id="form1" Runat="server">
    > My text is here<asp:DropDownList id="ddl" Runat="server">
    > </asp:DropDownList><p>
    > <asp:Button id="button1" Text="click here" onclick="button_Click"
    > Runat="server" />
    > </form>
    > </body>
    > </html>
    >
    > David Wier
    > [url]http://aspnet101.com[/url]
    > [url]http://aspexpress.com[/url]
    >
    >
    > "suzy" <suzy@nospam.com> wrote in message
    > news:uL2GUVrWDHA.2204@TK2MSFTNGP12.phx.gbl...
    > > sorry that was just a typo.
    > >
    > > there is no error, it just renders the page, then calls the click event.
    > >
    > >
    > > "David Wier" <dwier@nospamASPNet101.com> wrote in message
    > > news:u5oZTQrWDHA.536@TK2MSFTNGP10.phx.gbl...
    > > > Do you get an error?
    > > > Try:
    > > > Response.Redirect("anotherpage.aspx");
    > > >
    > > > David Wier
    > > > [url]http://aspnet101.com[/url]
    > > > [url]http://aspexpress.com[/url]
    > > >
    > > >
    > > > "suzy" <suzy@nospam.com> wrote in message
    > > > news:OWDWeLrWDHA.2268@TK2MSFTNGP11.phx.gbl...
    > > > > hi,
    > > > >
    > > > > i have an aspx page with a button. if i step through my code when i
    > > click
    > > > > the button then it calls the button_Click event which is what i
    want.
    > > > >
    > > > > but i need to redirect the page to another aspx page when this
    button
    > is
    > > > > clicked so i dont want to waste time rendering the same page before
    > > > > redirecting, so i put the following code in, but now the
    button_click
    > > > event
    > > > > doesn't get called :(
    > > > >
    > > > > what am i doing wrong? how can i render a page the first time its
    > > called,
    > > > > but if the button is clicked , then immediately call the
    button_Click
    > > > event.
    > > > >
    > > > > private void Page_Load(object sender, System.EventArgs e)
    > > > >
    > > > > {
    > > > >
    > > > > if (!(Page.IsPostBack))
    > > > >
    > > > > {
    > > > >
    > > > > //perform main rendering of the page here.
    > > > >
    > > > > }
    > > > >
    > > > > }
    > > > >
    > > > > by the way, here is my click event code:
    > > > >
    > > > > private void button_Click(object sender, EventArgs e)
    > > > >
    > > > > {
    > > > >
    > > > > Response.Redirect("anotherpage.aspx";
    > > > >
    > > > > }
    > > > >
    > > > >
    > > >
    > > >
    > >
    > >
    >
    >

    suzy Guest

  13. #12

    Default Re: beginner quesion about IsPostBack

    Solution for ur problems is based on ur requirement:

    1. If you need to do some processing on the server when the button is clicked before transfering the control to another page, then u dont have any option ur page will get build and ......... and after that ur button click event will be fired. (pl. go thru asp.net page life cycle).

    2. If u dont want to do any processing on the server when the button is clicked the just add OnClick attribute as said by william.
    button.Attributes["OnClick"] = "javascript : self.location.href ='anotherpage.aspx';"

    rajeev

    "suzy" <suzy@nospam.com> wrote in message news:uS90l34WDHA.1268@TK2MSFTNGP11.phx.gbl...
    > anyone have a solution to this?
    >
    > "suzy" <suzy@nospam.com> wrote in message
    > news:Op%23t8TsWDHA.536@TK2MSFTNGP10.phx.gbl...
    > > marina,
    > >
    > > you are absolutely right! i just tried it again but this time i added
    > ONLY
    > > the button before the "if" statement in the page_load event. now if i
    > click
    > > the button the button_click event fires without rendering the entire page.
    > >
    > > however, it has left my page looking untidy (and in the wrong order too!)
    > as
    > > the button is now at the top of the page, rather than at the bottom!
    > >
    > > is there a neat way to get around my problem? it just seems a bit messy
    > :(
    > >
    > > thanks again!
    > >
    > >
    > > "Marina" <zlatkinam@nospam.hotmail.com> wrote in message
    > > news:eWU983rWDHA.2376@TK2MSFTNGP11.phx.gbl...
    > > > There is a difference. You must recreate the button, otherwise your
    > event
    > > > will not get called.
    > > >
    > > > "suzy" <suzy@nospam.com> wrote in message
    > > > news:%238sA7wrWDHA.3924@tk2msftngp13.phx.gbl...
    > > > > yes i am dynamically creating the button.
    > > > >
    > > > > i didnt realise this would make a difference , because the click event
    > > > gets
    > > > > called if the "if" statement is not there. the downside to this is
    > that
    > > > it
    > > > > performs unnecessary actions by rendering the current page before
    > > > > redirecting.
    > > > >
    > > > >
    > > > > "Marina" <zlatkinam@nospam.hotmail.com> wrote in message
    > > > > news:equ$dXrWDHA.652@TK2MSFTNGP10.phx.gbl...
    > > > > > Are you dynamically creating the button? In which case, you must
    > > > recreate
    > > > > it
    > > > > > on postback.
    > > > > >
    > > > > > "suzy" <suzy@nospam.com> wrote in message
    > > > > > news:OWDWeLrWDHA.2268@TK2MSFTNGP11.phx.gbl...
    > > > > > > hi,
    > > > > > >
    > > > > > > i have an aspx page with a button. if i step through my code when
    > i
    > > > > click
    > > > > > > the button then it calls the button_Click event which is what i
    > > want.
    > > > > > >
    > > > > > > but i need to redirect the page to another aspx page when this
    > > button
    > > > is
    > > > > > > clicked so i dont want to waste time rendering the same page
    > before
    > > > > > > redirecting, so i put the following code in, but now the
    > > button_click
    > > > > > event
    > > > > > > doesn't get called :(
    > > > > > >
    > > > > > > what am i doing wrong? how can i render a page the first time its
    > > > > called,
    > > > > > > but if the button is clicked , then immediately call the
    > > button_Click
    > > > > > event.
    > > > > > >
    > > > > > > private void Page_Load(object sender, System.EventArgs e)
    > > > > > >
    > > > > > > {
    > > > > > >
    > > > > > > if (!(Page.IsPostBack))
    > > > > > >
    > > > > > > {
    > > > > > >
    > > > > > > //perform main rendering of the page here.
    > > > > > >
    > > > > > > }
    > > > > > >
    > > > > > > }
    > > > > > >
    > > > > > > by the way, here is my click event code:
    > > > > > >
    > > > > > > private void button_Click(object sender, EventArgs e)
    > > > > > >
    > > > > > > {
    > > > > > >
    > > > > > > Response.Redirect("anotherpage.aspx";
    > > > > > >
    > > > > > > }
    > > > > > >
    > > > > > >
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >
    > >
    > >
    >
    >
    Rajeev Soni Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139