Search code examples
asp.netc#-4.0webformsasp.net-4.0

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" />

Solution

  • 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"