1

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?

Wompguinea
  • 378
  • 3
  • 19
  • 3
    where is your event binding to linkbutton ? – Hakunamatata Sep 22 '15 at 23:32
  • On Page_Load the page will check the boolean variable in my User.vb class file, and it should display the appropriate link button while hiding the other (shown in the Page_Load event), and each linkbutton has a click event which should change the boolean and then refresh the page. I'm not sure where I would need to add extra binding to that? – Wompguinea Sep 22 '15 at 23:35
  • 1
    I think what @Hakunamatata is saying is that he doesn't see an OnClick="lbtnFull_Click" in your markup, which is usually where the event binding is declared. Double check this by putting a break point in your event handlers and then see if it breaks when you click the button. – Al W Sep 23 '15 at 01:47
  • Have you tried to check any breakpoints during those event handlers to make sure whether the its being called? – choz Sep 23 '15 at 01:57

4 Answers4

4

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.

Al W
  • 7,539
  • 1
  • 19
  • 40
  • Yep, that seems to be exactly what's happening now. The viewchange is working, but it requires an extra postback to actually perform the change. Which part of the Page Life Cycle should I be checking the viewchange in? – Wompguinea Sep 23 '15 at 23:23
  • 1
    Perhaps try putting it in the PreRender. – Al W Sep 23 '15 at 23:29
1

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.

Serif Emek
  • 674
  • 5
  • 13
1

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.

Suprabhat Biswal
  • 3,033
  • 1
  • 21
  • 29
  • Thanks, I've updated my linkbuttons to reference the OnClick events. Since they change a variable in a separate class file (that should stay changed regardless of postback status) do I still need to use the !ispostback section? – Wompguinea Sep 23 '15 at 04:36
  • 1
    Sure, Initial data should always be wrapped inside **!IsPostback**. – Suprabhat Biswal Sep 23 '15 at 04:43
0

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.

Tathagat Verma
  • 549
  • 7
  • 12