[SOLVED] Postback issue with button click

Issue

I am using Visual Studio webforms with asp.net and C#. I need to have my radiobutton dynamically change on the page but not run a function on a button click. How can I fix this postback issue?

On my page, I have a radio button to dynamically change the page from English or French language

<asp:RadioButtonList ID="RadioBtnLanguage" runat="server" AutoPostBack="True" ViewStateMode="Enabled" Width="227px" RepeatDirection="Horizontal">
                <asp:ListItem value="English" Selected="True"> English</asp:ListItem>
                <asp:ListItem Value="French"> Fran├žais</asp:ListItem>
                </asp:RadioButtonList>

I change the language as such:

  protected void Page_Load(object sender, EventArgs e)
    {
switch (RadioBtnLanguage.SelectedValue.ToString())
        {
            case "English":
                LblTitle.Text = "InfoHere"
                changeAttendType();
                break;
            case "French":
                LblTitle.Text = "InfoHere";
                changeAttendType();
                break;
        }
    }

That works fine. However I have a function changeAttendType(); that I don’t want to run when my submit button is clicked

<asp:Button ID="ButtonEditEvent" runat="server" Text="Submit" 
                onclick="ButtonEditEvent_Click" CssClass="formbutton"/>

How can I not run changeAttendType(); on a button click but let it run when someone changes the language radiobutton from english to french or french or english?

Solution

Ok, in 99% of my page load – every page for "many" years?

If you going to drop in buttons, combo boxes, even text boxes that cause a post-back and event trigger? Well, then you have to make a "design" choice.

That "choice" means that setup code, or just about ANY thing you run on first page load has to go inside of a if (!IsPostBack) block of code. In fact, if you leave this out?

Then all kinds of issues crop up. You might say load up some combo boxes on page load. Then user selects a combo value, but on page-load you trigger that loading again. Same goes for GridView, and just about everything else.
And if you re-load the Grid, or re-load the combo box? Then you blow out and lose the users choice they just made!!!

So, as a number one rule? To control this, and not have "nasty" surprises?

Then use the !IsPostback for the REAL first page load.

So, in the case of your radio button, then you do this:

    protected void Page_Load(object sender, EventArgs e)
    {
        {
            if (!IsPostBack)
            LoadLang();
        }
    }

    void LoadLang()
    {
        switch (RadioBtnLanguage.SelectedValue.ToString())
        {
            case "English":
                LblTitle.Text = "InfoHere";
                changeAttendType();
                break;
            case "French":
                LblTitle.Text = "InfoHere";
                changeAttendType();
                break;
        }
    }

And if you REALLY do need the radio button to work, and allow user to change the value? then your radio button can have a auto-post back = true, and you simple wire up the event for the RB

Say like this:

        <asp:RadioButtonList ID="RadioBtnLanguage" runat="server" 
            AutoPostBack="True" OnSelectedIndexChanged="RadioBtnLanguage_SelectedIndexChanged">
            <asp:ListItem>English</asp:ListItem>
            <asp:ListItem>French</asp:ListItem>
        </asp:RadioButtonList>

So, we have auto-post back – true, and a even for if user changes that RB.

the even code stub then becomes this:

    protected void RadioBtnLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadLang();
    }

As you can see? NEVER use the page.load event to re-run code each time, or run ALL the time, since page load ALWAYS runs for ANY button click, any post-back, any dropdown list, or even in above – the auto post-back and the RB "changed" event.

In effect, by "introducing" the above design pattern? You can freely add controls, buttons, events, or whatever.

Thus, no code runs un-expected. Thus over time, as you add more buttons, more events, or whatever? You not care one bit, and you not have some stray code running each time on page load (such as your RB button code).

That RB button code does not, and should not run on EVERY post-back. But as we know, page.load fires each and every time, and all the time, and for every post-back. So, put your setup, or the code to run one time in page load, but ALSO inside of that !IsPostBack stub.

99% or more, in fact every webform page I have ever created has the !IsPostBack stub in it.

If you break the above rule? then you quite much loose developer control over what code runs when a simple button, or anything that triggers a post-back – and that’s just about everything that will occur on such a page.

So, just adopt the if (!IsPostBack) code stub in all your web pages, and then you can just code without any thought (or worry) that code will run each time without you wanting it to, or often such code runs each time, which is not only a waste of resources, but often means you running code over and over when not required.

Answered By – Albert D. Kallal

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *