Solved

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

Posted on 2007-12-06
8
4,059 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
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 500 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…

948 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

21 Experts available now in Live!

Get 1:1 Help Now