开发者

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

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 :

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

There may be some scenario where you may want to explicitly postback to the server using some clientside 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:

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

"


one way would be to use an actual asp:Button and utilize the OnClientClick event...

<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.

here is a link to more details on the OnClientClick event.


It looks like you want to use ajax to call this server method. You can use ajax.net to do this. Obviously as a result it will not be postback.

Have a look here for examples


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜