Wompguinea
Wompguinea

Reputation: 378

linkbuttons postback before executing click event code?

I am trying to add a basic switch to my site in order to switch between static and responsive layouts.

I have two linkbuttons at the bottom of my page:

<div id="toggleView">

    <asp:linkbutton ID="lbtnMobile" runat="server" Visible="false">Switch to Mobile site</asp:linkbutton>

    <asp:linkbutton ID="lbtnFull" runat="server" >Switch to Full site</asp:linkbutton>

</div>

They both have a very similar OnClick event.

protected void lbtnFull_Click(object sender, EventArgs e)
    {
        c.ViewChange = true;
        Session["Customer"] = c;
    }
    protected void lbtnMobile_Click(object sender, EventArgs e)
    {
        c.ViewChange = false;
        Session["Customer"] = c;
    }

The events should set a boolean in a class file (User.vb) between true or false and then save the session, on postback the Page_Load event is supposed to read this boolean and use it to adjust the Viewport meta tag:

protected void Page_Load(object sender, System.EventArgs e)
    {
//Other Stuff in here, irrelevant to current question

HtmlMeta view = new HtmlMeta();
                view.Name = "viewport";
                if (c.ViewChange = false)
                {
                    view.Content = "width=device-width, initial-scale=1";
                    lbtnFull.Visible = true;
                    lbtnMobile.Visible = false;

                }
                else
                {
                    view.Content = "width=1040px, initial-scale=1";
                    lbtnFull.Visible = false;
                    lbtnMobile.Visible = true;
                }
                MetaPlaceHolder.Controls.Add(view);
}

However, when I click on the "Switch to Full Site" linkbutton, the page will postback but nothing will have changed. Does the postback get triggered too early somehow?

Upvotes: 1

Views: 1199

Answers (4)

Al W
Al W

Reputation: 7713

The page load event will happen BEFORE your click event. Reference this here.

This means your check for the ViewChange will happen before you set it in the OnClick handler.

Upvotes: 4

Tathagat Verma
Tathagat Verma

Reputation: 548

Firstly your implementation in the Page_Load isn't very clear.

Nevertheless this is what I recommend, from what I've understod:

  • As the page load will get executed before the post-back event like the buton or link click, you need persist the value of the class object
  • Make a protected property of type of your class (where you store/manage the ViewChange atribute)
  • The property should internally (in the get & set), hold/persist the value in session/viewstate (similar to what you've written)
  • The setting and reading should only be by referring the property directly (and not how you've done the click-event)
  • On clicking of the button and post setting the new value, you will have to redirect to the same page, as only then the Page_Load event will get the new boolean value that you've just changed in the click-event; (Page_Load occurs befoer the any other post-back event)
  • An alternative to the fresh redirection is that, you could make a function that has the view changing logic (as depicted in your Page_Load code), and this function should be called on your button/link click event (post boolean value change) and also in the Page_Load event, but within the "!IsPostBack" block

Hope this helps you.

Upvotes: 0

Suprabhat Biswal
Suprabhat Biswal

Reputation: 3216

When ever you postback the Page_Load always get called. So, the code mentioned inside Page_Load would always get executed.

protected void Page_Load(object sender, System.EventArgs e)
{
   ... All your mentioned code will be executed.
} 

Therefore, you won't find any change in your HTML page currently viewed in a browser because at postback initial content also got executed. You need to wrap your content inside !IsPostBack to make it work properly.

Thus, modify you code in following way.

protected void Page_Load(object sender, System.EventArgs e)
{    
    if(!IsPostback)
    {
       ... All your mentioned code will be executed during normal load.
    }
} 

Also, you need to add some extra code in LinkButton click event i.e. what to show and what to hide.

Upvotes: 1

Serif Emek
Serif Emek

Reputation: 674

You should change

if (c.ViewChange = false)

to

if (c.ViewChange == false)

for something to happen. But I think it won't be what you expect. Because page_load is executed before click event. You may move some code from page_load to click event handlers.

Upvotes: 1

Related Questions