如何在按钮单击时避免Page_Load()?

时间:2022-12-04 20:42:16

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.

但是,当单击预览时,附加到ajaxcontrol(日历)的一个文本框变为空,用户必须在保存之前填写日期。怎么办呢?在预览单击我获取详细信息以显示布局中的数据。

<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 个解决方案

#1


8  

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

似乎用户问题的答案是在预览按钮的HTML标记中进行了简单的更改

CausesValidation="False"

#2


13  

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

在aspx代码(服务器端)中使用Page.IsPostback()。喜欢这个:

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.

此代码将只运行在第一次加载页面,并避免踩着用户输入到表格的变化。

#3


3  

you can put your code in

你可以把代码放进去

Page_Init()
{
   //put your code here
}

instead of

Page_Load()
{
   //code
}

#4


1  

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

尝试将此添加到aspx页面中的按钮属性。

OnClientClick="return false;"

#5


1  

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.

我有同样的问题和上面的解决方案“CausesValidation =”False“”甚至添加“UseSubmitBehavior =”False“”DID不工作 - 它仍然称为“Page_Load”方法。

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

对我有用的是在Page_Load方法中添加以下行。

if (IsPostBack) return; 

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

我提到这个,如果它有帮助的人(我的意思是上面评论但*不允许我评论,因为我是一个新用户 - 因此一个新的答复)。

#6


-1  

form1.Action = Request.RawUrl; 

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

在页面加载时写下此代码,然后页面不会在按钮单击时回发

#1


8  

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

似乎用户问题的答案是在预览按钮的HTML标记中进行了简单的更改

CausesValidation="False"

#2


13  

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

在aspx代码(服务器端)中使用Page.IsPostback()。喜欢这个:

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.

此代码将只运行在第一次加载页面,并避免踩着用户输入到表格的变化。

#3


3  

you can put your code in

你可以把代码放进去

Page_Init()
{
   //put your code here
}

instead of

Page_Load()
{
   //code
}

#4


1  

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

尝试将此添加到aspx页面中的按钮属性。

OnClientClick="return false;"

#5


1  

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.

我有同样的问题和上面的解决方案“CausesValidation =”False“”甚至添加“UseSubmitBehavior =”False“”DID不工作 - 它仍然称为“Page_Load”方法。

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

对我有用的是在Page_Load方法中添加以下行。

if (IsPostBack) return; 

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

我提到这个,如果它有帮助的人(我的意思是上面评论但*不允许我评论,因为我是一个新用户 - 因此一个新的答复)。

#6


-1  

form1.Action = Request.RawUrl; 

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

在页面加载时写下此代码,然后页面不会在按钮单击时回发