如何在ASP.NET中从JavaScript触发按钮单击事件

时间:2022-08-29 03:35:21

How do I fire a server side button click event from JavaScript?

如何从JavaScript启动服务器端按钮单击事件?

I tried like this:

我试着像这样:

document.getElementById("<%= ButtonID.ClientID %>").click();

But no use. How can I do it?

但是没有使用。我怎么做呢?

9 个解决方案

#1


35  

You can just place this line in a JavaScript function:

您可以将这一行放在JavaScript函数中:

__doPostBack('btnSubmit','OnClick');

Or do something like this:

或者做这样的事情:

$('#btnSubmit').trigger('click');

#2


15  

I used the below JavaScript code and it works...

我使用了下面的JavaScript代码,它可以工作……

var clickButton = document.getElementById("<%= btnClearSession.ClientID %>");
clickButton.click();

#3


15  

var clickButton = document.getElementById("<%= btnClearSession.ClientID %>");
clickButton.click();

That solution works for me, but remember it wont work if your asp button has

这个解决方案对我来说是有效的,但是请记住,如果您的asp按钮有的话,它就不能工作

Visible="False"

可见= " False "

To hide button that should be triggered with that script you should hide it with <div hidden></div>

要隐藏应该由该脚本触发的按钮,您应该将其隐藏在

中。

#4


4  

None of the solutions posted here would work for me, this was my eventual solution to the problem.

在这里发布的所有解决方案对我都不起作用,这是我对这个问题的最终解决方案。

// In Server Side code
protected void Page_Load(object sender, EventArgs e)
{
    Page.GetPostBackEventReference(hiddenButton);
}

// Javascript
function SetSaved() {
    __doPostBack("<%= hiddenButton.UniqueID %>", "OnClick");
}

// ASP
 <asp:Button ID="hiddenButton" runat="server" OnClick="btnSaveGroup_Click" Visible="false"/>

#5


2  

I lived this problem in two days and suddenly I realized it that I am using this click method(for asp button) in a submit button(in html submit button) javascript method...

我在两天内解决了这个问题,突然我意识到我在一个提交按钮(在html提交按钮)javascript方法中使用了这个click方法(for asp按钮)……

I mean ->

我的意思是- >

I have an html submit button and an asp button like these:

我有一个html提交按钮和一个asp按钮如下:

<input type="submit" value="Siparişi Gönder" onclick="SendEmail()" />
<asp:Button ID="sendEmailButton" runat="server" Text="Gönder" OnClick="SendToEmail" Visible="True"></asp:Button>

SendToEmail() is a server side method in Default.aspx SendEmail() is a javascript method like this:

SendToEmail()是默认的服务器端方法。aspx SendEmail()是这样一种javascript方法:

<script type="text/javascript" lang="javascript">
   function SendEmail() {
            document.getElementById('<%= sendEmailButton.UniqueID %>').click();
            alert("Your message is sending...");
        }
</script>

And this "document.getElementById('<%= sendEmailButton.UniqueID %>').click();" method did not work in just Crome. It was working in IE and Firefox.

和这个“文档。getElementById(“< % = sendEmailButton。单击();“方法在Crome中不起作用。它在IE和Firefox中工作。

Then I tried and tried a lot of ways for executing "SendToEmail()" method in Crome.

然后我尝试了很多方法来在Crome中执行“SendToEmail()”方法。

Then suddenly I changed html submit button --> just html button like this and now it is working:

然后我突然改变了html提交按钮——>就是这样的html按钮,现在可以工作了:

<input type="button" value="Siparişi Gönder" onclick="SendEmail()" />

Have a nice days...

有个美好的一天…

#6


0  

You can fill a hidden field from your JavaScript code and do an explicit postback from JavaScript. Then from the server side, check that hiddenfield and do whatever necessary.

您可以从JavaScript代码中填充隐藏字段,并从JavaScript执行显式回发。然后,从服务器端检查这个隐藏字段,并执行任何必要的操作。

#7


0  

document.FormName.btnSubmit.click(); 

works for me. Enjoy.

为我工作。享受。

#8


0  

$("#"+document.getElementById("<%= ButtonID.ClientID %>")).trigger("click");

#9


0  

I can make things work this way:

我可以这样做:

inside javascript junction that is executed by the html button:

在由html按钮执行的javascript连接内部:

document.getElementById("<%= Button2.ClientID %>").click();

文档。getElementById(“< % = Button2。ClientID % >”).click();

ASP button inside div:

ASP按钮div内:

<div id="submitBtn" style="display: none;">
   <asp:Button ID="Button2" runat="server" Text="Submit" ValidationGroup="AllValidators" OnClick="Button2_Click" />
</div>

Everything runs from the .cs file except that the code below doesn't execute. There is no message box and redirect to the same page (refresh all boxes):

除了下面的代码没有执行之外,所有东西都是从.cs文件中运行的。没有消息框并重定向到同一页(刷新所有框):

 int count = cmd.ExecuteNonQuery();
 if (count > 0)
 {
   cmd2.CommandText = insertSuperRoster;
   cmd2.Connection = con;
   cmd2.ExecuteNonQuery();
   string url = "VaccineRefusal.aspx";
   ClientScript.RegisterStartupScript(this.GetType(), "callfunction", "alert('Data Inserted Successfully!');window.location.href = '" + url + "';", true);
  }

Any ideas why these lines won't execute?

你知道为什么这些行不能执行吗?

#1


35  

You can just place this line in a JavaScript function:

您可以将这一行放在JavaScript函数中:

__doPostBack('btnSubmit','OnClick');

Or do something like this:

或者做这样的事情:

$('#btnSubmit').trigger('click');

#2


15  

I used the below JavaScript code and it works...

我使用了下面的JavaScript代码,它可以工作……

var clickButton = document.getElementById("<%= btnClearSession.ClientID %>");
clickButton.click();

#3


15  

var clickButton = document.getElementById("<%= btnClearSession.ClientID %>");
clickButton.click();

That solution works for me, but remember it wont work if your asp button has

这个解决方案对我来说是有效的,但是请记住,如果您的asp按钮有的话,它就不能工作

Visible="False"

可见= " False "

To hide button that should be triggered with that script you should hide it with <div hidden></div>

要隐藏应该由该脚本触发的按钮,您应该将其隐藏在

中。

#4


4  

None of the solutions posted here would work for me, this was my eventual solution to the problem.

在这里发布的所有解决方案对我都不起作用,这是我对这个问题的最终解决方案。

// In Server Side code
protected void Page_Load(object sender, EventArgs e)
{
    Page.GetPostBackEventReference(hiddenButton);
}

// Javascript
function SetSaved() {
    __doPostBack("<%= hiddenButton.UniqueID %>", "OnClick");
}

// ASP
 <asp:Button ID="hiddenButton" runat="server" OnClick="btnSaveGroup_Click" Visible="false"/>

#5


2  

I lived this problem in two days and suddenly I realized it that I am using this click method(for asp button) in a submit button(in html submit button) javascript method...

我在两天内解决了这个问题,突然我意识到我在一个提交按钮(在html提交按钮)javascript方法中使用了这个click方法(for asp按钮)……

I mean ->

我的意思是- >

I have an html submit button and an asp button like these:

我有一个html提交按钮和一个asp按钮如下:

<input type="submit" value="Siparişi Gönder" onclick="SendEmail()" />
<asp:Button ID="sendEmailButton" runat="server" Text="Gönder" OnClick="SendToEmail" Visible="True"></asp:Button>

SendToEmail() is a server side method in Default.aspx SendEmail() is a javascript method like this:

SendToEmail()是默认的服务器端方法。aspx SendEmail()是这样一种javascript方法:

<script type="text/javascript" lang="javascript">
   function SendEmail() {
            document.getElementById('<%= sendEmailButton.UniqueID %>').click();
            alert("Your message is sending...");
        }
</script>

And this "document.getElementById('<%= sendEmailButton.UniqueID %>').click();" method did not work in just Crome. It was working in IE and Firefox.

和这个“文档。getElementById(“< % = sendEmailButton。单击();“方法在Crome中不起作用。它在IE和Firefox中工作。

Then I tried and tried a lot of ways for executing "SendToEmail()" method in Crome.

然后我尝试了很多方法来在Crome中执行“SendToEmail()”方法。

Then suddenly I changed html submit button --> just html button like this and now it is working:

然后我突然改变了html提交按钮——>就是这样的html按钮,现在可以工作了:

<input type="button" value="Siparişi Gönder" onclick="SendEmail()" />

Have a nice days...

有个美好的一天…

#6


0  

You can fill a hidden field from your JavaScript code and do an explicit postback from JavaScript. Then from the server side, check that hiddenfield and do whatever necessary.

您可以从JavaScript代码中填充隐藏字段,并从JavaScript执行显式回发。然后,从服务器端检查这个隐藏字段,并执行任何必要的操作。

#7


0  

document.FormName.btnSubmit.click(); 

works for me. Enjoy.

为我工作。享受。

#8


0  

$("#"+document.getElementById("<%= ButtonID.ClientID %>")).trigger("click");

#9


0  

I can make things work this way:

我可以这样做:

inside javascript junction that is executed by the html button:

在由html按钮执行的javascript连接内部:

document.getElementById("<%= Button2.ClientID %>").click();

文档。getElementById(“< % = Button2。ClientID % >”).click();

ASP button inside div:

ASP按钮div内:

<div id="submitBtn" style="display: none;">
   <asp:Button ID="Button2" runat="server" Text="Submit" ValidationGroup="AllValidators" OnClick="Button2_Click" />
</div>

Everything runs from the .cs file except that the code below doesn't execute. There is no message box and redirect to the same page (refresh all boxes):

除了下面的代码没有执行之外,所有东西都是从.cs文件中运行的。没有消息框并重定向到同一页(刷新所有框):

 int count = cmd.ExecuteNonQuery();
 if (count > 0)
 {
   cmd2.CommandText = insertSuperRoster;
   cmd2.Connection = con;
   cmd2.ExecuteNonQuery();
   string url = "VaccineRefusal.aspx";
   ClientScript.RegisterStartupScript(this.GetType(), "callfunction", "alert('Data Inserted Successfully!');window.location.href = '" + url + "';", true);
  }

Any ideas why these lines won't execute?

你知道为什么这些行不能执行吗?