Solved

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

Posted on 2007-12-06
8
4,063 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

688 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