Solved

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

Posted on 2007-12-06
8
4,061 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

726 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