How to avoid Page_Load() on button click?

2019-06-19 12:00发布

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

6条回答
Ridiculous、
2楼-- · 2019-06-19 12:24

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"
查看更多
Evening l夕情丶
3楼-- · 2019-06-19 12:26

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.

查看更多
beautiful°
4楼-- · 2019-06-19 12:28

Try adding this to the buttons properties in the aspx page.

OnClientClick="return false;"
查看更多
时光不老,我们不散
5楼-- · 2019-06-19 12:33

I had the same problem and the solution above of "CausesValidation="False"" and even adding "UseSubmitBehavior="False"" DID NOT work - it still called "Page_Load" method.

What worked for me was adding the following line up front in Page_Load method.

if (IsPostBack) return; 

I am mentioning this if it helps someone (I meant to comment above but StackOverflow did not allow me to comment because I am a new user - hence a new reply).

查看更多
手持菜刀,她持情操
6楼-- · 2019-06-19 12:39

you can put your code in

Page_Init()
{
   //put your code here
}

instead of

Page_Load()
{
   //code
}
查看更多
孤傲高冷的网名
7楼-- · 2019-06-19 12:41
form1.Action = Request.RawUrl; 

Write this code on page load then page is not post back on button click

查看更多
登录 后发表回答