开发者

Button click doesn't call function even when button attribute OnClick is set to that function

开发者 https://www.devze.com 2022-12-23 06:35 出处:网络
I have an aspx page with two buttons, one of the buttons has an OnClick attribute to a function that should be run when clicked.When the other button is clicked there is an if statement checking if th

I have an aspx page with two buttons, one of the buttons has an OnClick attribute to a function that should be run when clicked. When the other button is clicked there is an if statement checking if the page is a postback, if it is then I run some statements that need to be run when that button is clicked. That postback 开发者_运维百科button works fine. However, the other button, when it's clicked the function it's supposed to call never executes, and the statements inside if (Page.IsPostBack) get executed instead. What can I do to fix this? Is there a way to make the button that calls a function not do a Post back?


[snip - not part of the question]

EDIT: Here's an example of calling a button's OnClick function in an asych way using an UpdatePanel:

.aspx

<form id="form1" runat="server">
    <asp:ScriptManager runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="upTest" runat="server">
        <ContentTemplate>
            <asp:Button ID="btnTest" Text="Test" OnClick="Delete_Click"  runat="server" />
        </ContentTemplate>
    </asp:UpdatePanel>
</form>

.aspx.cs

protected void Delete_Click(object sender, EventArgs e)
{
    // Whatever you want to do.
}

EDIT 2: Here's an answer I gave recently that figures out what control has caused a postback.

0

精彩评论

暂无评论...
验证码 换一张
取 消