Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid Page_Load() on button click?

I have two buttons, preview and Save. With help of preview button user can view the data based on the format and then can save.

But when preview is clicked, one textbox attached to ajaxcontrol (Calender) becomes empty and user have to fill the date before saving. How to handle this? On preview click i get the details to show the data in layout.

<asp:TextBox ID="txtDate" ReadOnly="true" runat="server"></asp:TextBox>
                    <div style="float: right;">
                        <asp:ImageButton ID="imgcalender1" runat="server" ImageUrl="~/images/calendar.png"
                            ImageAlign="Bottom" />
                        <asp:CalendarExtender ID="ajCal" TargetControlID="txtpublishDate" PopupButtonID="imgcalender1"
                            runat="server">
                        </asp:CalendarExtender>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2"  ValidationGroup="group1" runat="server" ControlToValidate="txtDate"
                            ForeColor="Red" Font-Bold="true" ErrorMessage="*"></asp:RequiredFieldValidator>
                    </div>

<asp:Button ID="btnPreview" runat="server" Text="Preview" OnClick="btnPreview_Click" />
                    <asp:Button ID="btnsubmit" runat="server" ValidationGroup="group1" Text="Save" OnClick="btnsubmit_Click" />
like image 908
Abhishek Ranjan Avatar asked Mar 28 '13 19:03

Abhishek Ranjan


People also ask

How to avoid page load on button click in asp net?

Set AutoPostback = false for insert button, and this will do the trick for you.

What is Page_Load?

Your web form has a method called Page_Load. This method is called each time your page is loaded. This includes: 1. The first time the page is loaded, e.g., when a user enters the url to it or clicks on a link to it.

Why we use page load in asp net?

The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page. This is the first place in the page lifecycle that all values are restored.


2 Answers

Use Page.IsPostback() in your aspx code (server-side). Like this:

private void Page_Load()
{
    if (!IsPostBack)
    {
        // the code that only needs to run once goes here
    }
}

This code will only run the first time the page is loaded and avoids stepping on user-entered changes to the form.

like image 84
tgolisch Avatar answered Nov 08 '22 07:11

tgolisch


From what I am understanding the preview button is causing a postback and you do not want that, try this on your preview button:

<asp:button runat="server".... OnClientClick="return false;" />

similarly this also works:

YourButton.Attributes.Add("onclick", return false");

Edit:

it seems the answer to the user's question was simple change in the HTML mark up of the preview button

CausesValidation="False"
like image 38
Prakash Chennupati Avatar answered Nov 08 '22 06:11

Prakash Chennupati