Solved

how to call ASP.NET function with an input parameter, from Javascript

Posted on 2007-12-06
8
4,058 Views
Last Modified: 2013-11-07
I need to call an ASP.NET function which takes one input parameter, from Javascript.

I have been able to call an ASP.NET function function1() with no input parameter, from Javascript like this.  
"javaScript:myJS(" + Button1.ClientID + ");"
But the function1() is supposed to have an input parameter - function1(string arg0). How do I call function1(string arg0) then because I can't send the parameter 'arg0' to the button click event handler? Thanks.

<script type="text/javascript">
         function myJS(myVar)
         {
            document.getElementById(myVar).click();
            return false;
         }
</script>

protected void Button1_Click(object sender, EventArgs e)
{
   function1();
}
0
Comment
Question by:engg
  • 5
  • 3
8 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
Comment Utility
Add a hidden fields to the form and put the value in the hidden field in the Javascript code. The value is sent along to the server when the form is posted, so that you can pick up the value in the server side method.
0
 

Author Comment

by:engg
Comment Utility
Thanks. It works.

I added asp:TextBox control TextBox1 to the page and put this statement in the code behind page.
TextBox1.Text = " tbtext";
and changed Button1_Click like this.
protected void Button1_Click(object sender, EventArgs e)
{
   function1(TextBox1.Text);
}

I will get back soon.


0
 
LVL 29

Assisted Solution

by:Göran Andersson
Göran Andersson earned 500 total points
Comment Utility
I suppose that you don't want the textbox, but rather a hidden field:

<input type="hidden" id="somename" name="somename" />

Put the value in the field:

document.getElemetById('somename').value = myVar;

Pick it up on the server side:

function1(Request.Form["somename"]);
0
 

Author Comment

by:engg
Comment Utility
Thanks. This will work.

I need to cause the page postback and ultimately call function1(string val)  through Javascript.  

I could do this by adding a asp:button to the form and doing this in Javascript.
document.getElementById(buttonid).click();

The problem of passing the value to the function1 will be solved by adding a hidden field.

The button click through Javascript worked, when the button was visible. I can't have this button visible.
If I make this button invisible, the button click using Javascript doesn't work.

How do I cause postback through Javascript?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:engg
Comment Utility
On the design source (markup) page, I need to have a function like this.

<script type="text/javascript">
         function myJS(myVar1)
         {
            document.getElemetById('somename').value = myVar;
            //cause a postback  ---- button click didn't work if the button was hidden
         }
</script>
0
 
LVL 29

Assisted Solution

by:Göran Andersson
Göran Andersson earned 500 total points
Comment Utility
How did you hide the button? If you use the Visible property it will not be rendered on the page at all.

If you hide it using css, you can still use it:

<asp:Button id="buttonid" runat="server" style="display:none;" />
0
 

Author Comment

by:engg
Comment Utility
great.
Yes, I used the visible property to hide the button.
I will do it using css, the first thing Monday morning.
Thanks a lot.
0
 

Author Closing Comment

by:engg
Comment Utility
Thanks.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now