0

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');
 }

4 Answers 4

1

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();
    }
}

"

Sign up to request clarification or add additional context in comments.

Comments

0

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.

4 Comments

ok bt in that case how will i retrieve the len ,,like earlier i was retrieving it from code behind file
can you add the definition of the files element?
if u see my code i posted, u will see i m trying to calculate the length of files arrary nd passing it to alertMe function so i jst need that length as an argument in my javascript function
right, i see that. but is it an html element, a global variable or something else? depending on how files is defined, you should be able to either evaluate files.length directly in alertMe, or pass files.Length to alert me using an eval expression from OnClientClick. But it's going to depend on how files is defined. Is it a global variable defined in your code behind file?
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.