简体   繁体   English

在后面的代码中调用OnClientClick按钮

[英]Invoke OnClientClick of button in code behind

I have an button in my aspx web page, which should be 'clicked' in my code behind c# code. 我的aspx网页上有一个按钮,应该在c#代码后面的代码中“点击”。 The OnClientClick method then call a JS confirm dialog box and so the btn_Click function only get executed when user click 'OK'... OnClientClick方法然后调用JS确认对话框,因此只有当用户单击“确定”时才执行btn_Click函数...

This is my code so far: Variante 1 这是我的代码到目前为止: Variante 1

aspx: ASPX:

<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" />

aspx.cs: aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
       // set OnClientClick 
       btnConfirm.OnClientClick = "confirm('Save in DB?');";

       // invoke method
       Type t = typeof(System.Web.UI.WebControls.Button);
       object[] p = new object[1];
       p[0] = EventArgs.Empty;
       MethodInfo m = t.GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);
       m.Invoke(btnConfirm, p);
}
protected void btnConfirm_Click(object sender, EventArgs e)
    {
        //save something in database
    }

Got code samples by: http://forums.asp.net/t/1046550.aspx?How+do+you+raise+a+button+click+event+from+outside+the+button+ 通过以下代码获取代码示例: http//forums.asp.net/t/1046550.aspx?How + do+you + laise + a +button + click&event + from+outside+ the+ button+

I want to call the OnClientClick method, but when I replace 'OnClick' with 'OnClientClick' this error appear: 我想调用OnClientClick方法,但当我用'OnClientClick'替换'OnClick'时,会出现以下错误:

System.NullReferenceException was unhandled by user code Message=Object reference not set to an instance of an object. 用户代码未处理System.NullReferenceException消息=未将对象引用设置为对象的实例。

Edit: Variante 2: 编辑: Variante 2:

I tried to rewrite my program like this: aspx: 我试着像这样重写我的程序:aspx:

<script language="javascript">
function invokeButtonClick() {
        document.getElementById("btnConfirm").click();
    }

    function Validate() {
        return confirm('Save in db?');
    }
</script>
<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" OnClientClick="return Validate();" />

aspx:cs: ASPX:CS:

protected void Page_Load(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, GetType(), "InvokeButton", "invokeButtonClick();", true);
}

protected void btnConfirm_Click(object sender, EventArgs e)
{
   // save something in database
}

But then the page postback before my btnConfirm_Click function get called... 但是在我的btnConfirm_Click函数被调用之前页面回发...

Thanks, 谢谢,

Michael 迈克尔

If OnClientClick has a return value of true then the codebehind OnClick event will fire. 如果OnClientClick的返回值为true ,则将触发代码隐藏的OnClick事件。 If OnClientClick is returned false then it won't. 如果OnClientClick返回false ,则不会。

If OnClientClick doesn't return anything the codebehind event will fire regardless. 如果OnClientClick没有返回任何内容,则无论如何都会触发codebehind事件。 So you want.. 所以你要..

<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" />

<script language="javascript">
    function Validate() {
        return confirm('Save in db?');
    }
</script>

Codebehind: 代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
   // set OnClientClick 
   btnConfirm.OnClientClick = "return Validate();";
}

protected void btnConfirm_Click(object sender, EventArgs e)
{
    //save something in database
}

UPDATE: 更新:

This post just came to my attention, don't know why I didn't include it in my original post but you can use 'OnClientClick' in the aspx tag: 这篇文章刚引起我的注意,不知道为什么我没有把它包含在原帖中,但是你可以在aspx标签中使用'OnClientClick':

<asp:Button runat="server" ID="btnConfirm" OnClientClick="return Validate();" Text="Confirm" OnClick="btnConfirm_Click" />

<script language="javascript">
    function Validate() {
        return confirm('Save in db?');
    }
</script>

You need to Add both the events on the button click. 您需要在按钮单击上添加两个事件。 OnClientClick handles the Client side script code and OnClick handles the server side event calling. OnClientClick处理客户端脚本代码, OnClick处理服务器端事件调用。
So add both events and there handlers. 所以添加两个事件和那些处理程序。 The NullReferenceException you are getting because of that. 因此而得到的NullReferenceException

<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" OnClientClick="return Validate();" />

and in your javascript code can be 并在您的javascript代码中可以

function Validate(){
     if (valied())
        return true;
    return false;
}

100% code behind confirmation! 确认后100%代码! Sometiems its so annyoing to Code behind or Server side confirmation in asp.net when you want to Delete or Update something, I got its solution after a long search as below: 当你想要删除或更新某些内容时,某些内容对于后面的代码或者asp.net中的服务器端确认是如此厌恶,我在长时间搜索之后得到了它的解决方案,如下所示:

<asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="return confirm('Are you sure to BLOCK this customer ?')" OnClick="btnSave_Click" />

Now whenever you will click this button it 'll ask to confirm, if you 'll select "NO" page will not post and if you select "Yes" then page will post and your button click event will be fire... like below 现在,无论何时单击此按钮,它都会要求确认,如果您选择“否”页面将不会发布,如果您选择“是”,则页面将发布,您的按钮点击事件将会触发...如下所示

Code behind: 代码背后:

protected void Page_Load(object sender, EventArgs e)
    {
    } 

 // Before coming here,,,there will be confirmation   
 protected void btnSave_Click(object sender, EventArgs e)
    {
      GetCustomerInfo();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM