javascript 如何从javascript调用c#按钮单击方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10481811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call c# button click method from javascript?
提问by Abhishek
i am trying to call a server side button click method from javascript function but it's not working, can i know where i did wrong?
我正在尝试从 javascript 函数调用服务器端按钮单击方法,但它不起作用,我能知道我哪里做错了吗?
aspx code:
aspx代码:
<asp:Button ID="ButtonFns" Name="ButtonFns" runat="server" Text="Finish"
OnClientClick ="CountDownTick()" class="finish" onclick="ButtonFns_Click" />
Javascript code:
Javascript代码:
function CountDownTick() {
if (_currentSeconds <= 0) {
document.getElementById('<%= ButtonFns.ClientID %>').click();
return;
}
SetCountdownText(_currentSeconds-1);
window.setTimeout("CountDownTick()", 1000);
}
C# button click:
C#按钮点击:
protected void ButtonFns_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Examination.mdf;Integrated Security=True;User Instance=True");
try
{
con.Open();
string snm;
string Em;
string Sx;
string uname = Session["status"].ToString();
string qry = "SELECT SName,Email,Sex FROM Students WHERE Uname=@uname";
SqlCommand cm = new SqlCommand(qry, con);
cm.Parameters.AddWithValue("@uname", uname);
SqlDataReader reader = cm.ExecuteReader();
while (reader.Read())
{
snm = reader["SName"].ToString();
Em = reader["Email"].ToString();
Sx = reader["Sex"].ToString();
if (snm != null && Em != null && Sx != null)
{
Session["snm"] = snm.ToString();
Session["em"] = Em.ToString();
Session["sx"] = Sx.ToString();
Session["uname"] = uname;
Session["RAns"] = LabelRitAns.Text;
Session["Tatt"] = LabelTotAtt.Text;
Server.Transfer("YourScore.aspx");
break;
}
}
}
catch (Exception emsg)
{
LabelErr.Text= emsg.Message.ToString();
}
finally
{
con.Close();
}
}
Any help would be appreciated..
任何帮助,将不胜感激..
回答by Chris Sinclair
Use the __doPostBack method exposed for JavaScript. If you pass in the ID of the button and event name, it essentially performs the same postback to the server as though the user clicked the button.
使用为 JavaScript 公开的 __doPostBack 方法。如果您传入按钮的 ID 和事件名称,它本质上执行与用户单击按钮相同的回发到服务器。
ASP.NET postback with JavaScript(this uses VB code-behind, but otherwise does what you need I think)
使用 JavaScript 的 ASP.NET 回发(这使用 VB 代码隐藏,但除此之外,我认为可以满足您的需求)
EDIT: Just to clarify, instead of calling the button's "click" method, use __doPostBack instead.
编辑:只是为了澄清,而不是调用按钮的“点击”方法,而是使用 __doPostBack 。
EDITx2: Also, make sure your OnClientClick return value is true. If it's false, I think its equivalent to saying it's not valid and should not do a postback.
EDITx2:另外,请确保您的 OnClientClick 返回值为 true。如果它是错误的,我认为这相当于说它无效并且不应该进行回发。
回答by Eugene Trofimenko
ButtonFns.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(ButtonFns, "Your postback argument"));
GetPostBackEventReference allows you to get that piece of JavaScript, so that you can trigger that postback from elsewhere.
GetPostBackEventReference 允许您获取那段 JavaScript,以便您可以从其他地方触发该回发。
This article will help you to resolve this issue
这篇文章将帮助您解决这个问题
回答by Zaheer Ahmed
You can make a POST request from your client side firstly make your click event to method as:
您可以从客户端发出 POST 请求,首先将单击事件设置为方法:
[WebMethods]
public static void ClickEvent()
{
//do you stuff
}
and now make an ajax call from your client side let your web page name is Default.aspx:
现在从您的客户端进行 ajax 调用,让您的网页名称为 Default.aspx:
$.ajax({
type: "POST",
url: "Default.aspx/ClickEvent",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Do something interesting here.
}
});
回答by Alper
In your case you can do it like that,
在你的情况下,你可以这样做,
<script type="text/javascript">
function ImitatePressButton() {
__doPostBack('ButtonFns', '');
}
</script>
good luck.
祝你好运。
回答by javasocute
If you want to call a button from javascript, here is another way how.
如果你想从 javascript 调用一个按钮,这里是另一种方法。
var test = document.getElementbyId("YourButtonName");
test.onclick = function() {
//your code