?
Solved

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

Posted on 2007-12-06
8
Medium Priority
?
4,064 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 2000 total points
ID: 20424507
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
ID: 20431153
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 2000 total points
ID: 20431551
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:engg
ID: 20431925
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
 

Author Comment

by:engg
ID: 20431983
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 2000 total points
ID: 20432064
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
ID: 20432753
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
ID: 31413290
Thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Suggested Courses

801 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