Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call an ASP.NET Subroutine from a javascript function 1

Status
Not open for further replies.

cesark

Programmer
Dec 20, 2003
621
0
0
I want to call a subroutine from a javascript function. How can I do it? I am trying to do it thus: (But it doesn’ t work)
Code:
function check_State(eventTarget, eventArgument) {  

	  if (document.form_newOffer.states.value != "0") {
	  		var theform;

		if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {

			theform = document.forms["form_newOffer"];

		} else {

			theform = document.form_newOffer;

		}

		theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
		theform.__EVENTARGUMENT.value = eventArgument;

		theform.submit();

	  }

}

The form control that triggers the js function:
Code:
<asp:dropdownlist ID="product" onChange="check_State(' Certificates ','')" runat="server" CssClass="letter1"></asp:dropdownlist>

And a piece of the Subroutine:
Code:
Sub Certificates(sender As Object, E As EventArgs)
  If states.SelectedItem.Value <> "0" Then 

     Dim CmdCert AS New SqlCommand("sel_CertAuto", strConnection)
     CmdCert.CommandType = CommandType.StoredProcedure

...

Thank you,
Cesar
 
I think that the problem is pure ASP.NET. I think that I am calling a subroutine from a form control that not passes the correct parameters needed for the subroutine. Because if I put the Certificates Sub in ‘Page_Load’ section it works fine. The mistake must be here:
Code:
<asp:dropdownlist ID="product" runat="server" CssClass="letter1" onSelectedIndexChanged="Certificates" Autopostback="true"></asp:dropdownlist>



Sub Certificates(sender As Object, E As EventArgs)
  If states.SelectedItem.Value <> "0" Then 

     Dim CmdCert AS New SqlCommand("sel_CertAuto", strConnection)
     CmdCert.CommandType = CommandType.StoredProcedure

          CmdCert.Parameters.Add(New SqlParameter("@Product_id", SqlDbType.smallint, 2, "Product_id"))
     CmdCert.Parameters("@Product_id").Value = product.SelectedItem.Value
   
     CmdCert.Parameters.Add(New SqlParameter("@State_id", SqlDbType.smallint, 2, "State_id"))
     CmdCert.Parameters("@State_id").Value = states.SelectedItem.Value
   	 
	 strConnection.open()
	    Dim Certif_Aut As SqlDataReader = CmdCert.ExecuteReader(CommandBehavior.CloseConnection)

      certAut.DataSource = Certif_Aut
      certAut.DataBind()
	  

	 Dim Cert_Aut As New ListItem
     Cert_Aut.Text = "(Select)"
     Cert_Aut.Value = "0"  
     certAut.Items.Insert(0, Cert_Aut)
	 certAut.SelectedIndex = 0
	    
	test.Text() = Certif_Aut.GetValue(1)	

  End If
End Sub

Somebody knows why the Certificates Subroutine works in ‘Page_Load’ or in ‘If Page.IsPostBack’ section, but not when I am calling it from a dropdownlist? When the page is postback from a ddl it seems that the Certificates Sub must be have all the needed information from the form to make the query..? Isn’ t it..? And I am calling the Sub from the ddl in the correct way..
 
..this is all correct. I think that the problem is:

The ddl 'product' is an ASP.NET server control, but it is populated with items on client side instead of on server side. I mean with a javascript function, so, I suppose for that reason, when the page is postback and tries to find the operation that this ddl have to do, (OnSelectedIndexChanged="Certificates"), the ddl server control doesn' t find the 'OnSelectedIndexChanged' property because it hasn' t items on it on server side. It can be possible? If so, what is the solution?
 
cesark,
I was wondering if you ever found the solution cause I am facing the similar problem.
I have a list box that I add items to it using java script, but when I use any server controls on the page (like calendar) the value go away!
I need to invoke a C# function using java script so that I can add the options using C# instead of java script.

Any idea?
 
I need to invoke a C# function using java script so that I can add the options using C# instead of java script.

Could I see a sample of your javascript function from you want to 'call' the C# function? And also your server control from you call the javascript function?
 
Folks,

You cannot "call" a C#/ASP.NET method from JavaScript. Those methods only run on the server.

What you can do is use your JavaScript to:

1) add/change properties of the various controls, including, for example, setting hidden form variables and then,

2) submitting the form with the JavaScript form.submit() method.

Then, when you are processing the form server-side, you can look at the form values (see what the JavaScript did) and then call any server-side method you need to in response.



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Hi,
You cannot "call" a C#/ASP.NET method from JavaScript. Those methods only run on the server

Yes it's true, but you can cause the page postback from a javascript function for a specified condition, and then, during the postback you can know which form control caused the postback and trigger/call a C# or VB.NET subroutine or function, and make whatever operation.

 
That it's very interesting..
 
BoulderBum, I am trying to test the examples you provided with that msdn page but I have a problem:

I am trying to test the example ‘Using a hidden variable for capturing client-side changes’, from this page: . To test the example exposed in that page, I created a virtual directory in IIS named ‘Samples’, and a subdirectory named ‘bin’ under that virtual directory. Then, to test the example I tried to execute a command from the ‘Samples’ virtual directory so that I can test the example, that command generates an assembly. But I don’ t know how to execute that command, I tried: (from ‘Start/execute/cmd’ and then enter)

C:\Inetpub\ vbc /t:library /out:./bin/CustomControls.dll /r:System.dll /r:System.web.dll /r:System.drawing.dll /r:System.Data.dll *.vb

But it doesn’ work, an error appears saying:
“vbc” is unknown as an internal or external command,..

How can I do it?
The instructions to build samples are here:
And with the example mentioned above () I tried to create two files in the ‘Samples’ directory, the first one with .vb extension and the second one with .aspx extension. Is it correct?

Thank you in advance,
Cesar
 
Do you have the Framework SDK installed? Apparently it can't find the compiler.

All they're doing with vbc is compiling a type library with a bunch of references. If you use VS.NET or Web Matrix or something it will be a lot easier for you. Do you have either?
 
Yes I have the Framework SDK installed.

I do not use VS.NET nor Web Matrix, I programing all my web site typing by hand. Do you think that I would have to use some tool? If so, which you recommend me? And why?

Cheers


 
You don't HAVE to have an IDE, it's just a lot more productive and easy to use. I use Visual Studio .NET 2003 which features powerful visual designers, a robust debugger, Intellisense (can't live without it), code templates, and a host of other features. It will also help you avoid headaches like the one you're experiencing because you can compile class library projects with the click of a button and not have to worry about reference switches and stuff because the IDE does it for you (after you add a reference to the project).

Microsoft ocassionally has promotions where you can get a standard edition of the software for free. In fact, you can now get the software AND a book for watching 3 webcasts. Check it out:


Web Matrix has always been free (but I don't like it):


and you can download the "Express Edition" betas of VS 2005 here (Web Dev 2005 Express is the ASP.NET thingie):




That said you should still be able to compile the library without any IDE. Maybe they're assuming you're using the "Visual Studio Command Prompt" or something. If you try to locate vbc.exe, browse to that directory, then include the full file paths in the arguments I think it should work. I rarely ever have to use a command-prompt compiler though, so I probably won't be able to offer as detailed info as you'd like, unfortunately.
 
Ok, thank you very much for your explanation! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top