[英]calling javascript from codebehind or dopastback in javascript
i have a button1_click() function which runs on page load,,now i want to call this function from javascript,,for that purpose i need to do dopostback in javascript,,can nyone tell how can i do that..as u can see my pageload function that button1_click() runs on postback 我有一个button1_click()函数在页面加载时运行,现在我想从javascript调用此函数,为此,我需要使用javascript做dopostback,没人能告诉我该怎么做。看到button1_click()在回发时运行的页面加载函数
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int l = files.Length;
Button1.Attributes.Add("onclick", " alertMe("+ l.ToString() +");");
}
Button1_Click();
}
my javascript code : 我的JavaScript代码:
function alertMe(len)
{
if(len>3)
//do postback(post back will run Button1_click function)
else
alert('Hello');
}
This is a helpful link 这是一个有用的链接
From the article: " Calling postback event from Javascript 来自文章:“从Javascript调用回发事件
There may be some scenario where you may want to explicitly postback to the server using some clientside javascript. 在某些情况下,您可能想使用某些客户端JavaScript将其显式回发到服务器。 It is pretty simple to do this.
做到这一点很简单。
ASP.NET already creates a client side javascript method as shown below to support Postbacks for the web controls: ASP.NET已经创建了如下所示的客户端javascript方法,以支持Web控件的回发:
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
" ”
Possibly this: 可能是这样的:
function alertMe(len)
{
if(len>3)
//do postback(post back will run Button1_click function)
alertMe(len);
else
alert('Hello');
}
I would always try to avoid inline js though 我总是会尽量避免内联js
one way would be to use an actual asp:Button and utilize the OnClientClick event... 一种方法是使用实际的asp:Button并利用OnClientClick事件...
<asp:Button id="myButton" runat="Server" OnClick="Button1_Click" OnClientClick="alertMe();" />
function alertMe()
{
if (this.len>3)
{
return true;
}
else
{
return false;
}
}
if alertMe returns true, the the postback to the server will occur, if it returns false, it won't. 如果alertMe返回true,则将发生向服务器的回发,如果返回false,则不会发生。
here is a link to more details on the OnClientClick event. 这是有关OnClientClick事件的更多详细信息的链接 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.