0

I have use the jquery click event for open popup like this.

$("#ctl00_login1").click(function () {
    $("#EmailPwd").addClass("Emailhide");
    $("#Email").removeClass("Emailhide");
    //centering with css
   centerPopup();
    //load popup
    loadPopup();
});

I want to call this click event using C# Code in asp.net to open popup like this.

protected void Page_Load(object sender, EventArgs e)
    {
call code here..
}
3
  • C# code that is located where? Commented May 1, 2014 at 6:21
  • Which framework? ASP.NET or MVC. it's difficult to diagnoise problem in currect form Commented May 1, 2014 at 6:21
  • 1
    The oversimplified answer is you can't. C# Code is executed on the server while javascript/jquery is executed client side in a web browser. Also what is the trigger for calling this event, what are your wanting to respond to? Commented May 1, 2014 at 6:26

3 Answers 3

1

see http://msdn.microsoft.com/de-de/library/z9h4dk8y.aspx

try this

string jquery = "$("#target" ).click();"

 ClientScript.RegisterStartupScript(typeof(Page), "a key", 
         "<script type=\"text/javascript\">"+ jquery +"</script>"
                   );
Sign up to request clarification or add additional context in comments.

Comments

0

Mahmoude Elghandour's answer is a good one but I would not actully fire the click event, but abstract out the code you want to fire. Then fire this code off in a document.ready event.

javascript

<script type="text/javascript">
   function doPop(){
      $("#EmailPwd").addClass("Emailhide");
      $("#Email").removeClass("Emailhide");
      //centering with css
      centerPopup();
      //load popup
     loadPopup();
   }

   $(document).ready(function(){
      $("#ctl00_login1").click(doPop());
   });
</script>

C#

protected void Page_Load(object sender, EventArgs e)
{
   ClientScript.RegisterStartupScript(typeof(Page), "popIt", "$(document).ready(function(){doPop();});", true);
}

1 Comment

Thanx Jon for the perfect solution.
0

JQuery is a client side framework. C# is probably your server's codes.

Now your server cannot call your client functions since then run on various client machines.

What you can do is render the page with c# conditions that will affect the client, something like this:

// the if is C# code
<%if(num == 1){%>
    alert( "Some code written here for..." );
<%}%>

if num is really 1, this will translate into this js code that will run:

alert( "Some code written here for..." );

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.