Results 1 to 6 of 6

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

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Location
    Barcelona
    Posts
    70

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

    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)

    Client side code (javascript)
    Code:
    function check_State(eventTarget, eventArgument) {  
    
          if (document.form_newOffer.states.value != "0") {
                  var theform;
    
                theform = document.form_newOffer;
    
            }
    
            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

  2. #2
    All you really need is a way for ASP.NET to know what to do once the form is "posted back" to the server...

    Easiest way is probably to either use a query string or write a value to a cookie...

    QUERY STRING METHOD
    First, make sure your form postback routine from javascript works (I don't have a sample one handy, but you can find them from lots of places)...

    You can change the postback routine in Javascript so that it posts the page with a querystring appended to it depending on which sub you want to run:
    www.yourpage.com?rs=sub1

    Then in the Page Load event of the page you can check the value of the query string as such:

    If Page.IsPostBack then
    Select Case Request.QueryString
    Case "sub1"
    Sub1() 'Run sub procedure1

    Case "sub2"
    Sub2() 'Run sub procedure2

    End Select
    End If



    You can also do a similar thing by using javascript to write to a value in a cookie, then on the Page.IsPostBack check the cookie for the name of the sub to run.

    You can also use hidden fields that are HTML controls set to "Run as Server". However I have had some weird issues with doing it this way.

    If you want to try a more real-time solution and your customers are all using IE 5.5 or later, you can actually embed a Web Service directly into the CLIENT SIDE part of the page and tie them together in real-time. This is probably a little more then you need to do though.

    Hope this helps...
    Developer Web Hosting - ASP.NET & PHP
    www.datahostingcenter.com
    15% OFF HOSTING Coupon for Forum Members - Enter:
    vbforums

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Location
    Barcelona
    Posts
    70
    I mean something much more simple. I only have to call a javascript function from my dropdownlist to check if a value is selected in another form control. If it is selected, return 'true' in order to make a postback and trigger my subroutine, if it is not selected return 'false' and not to make any postback. Something like this:

    Javascript function:
    Code:
    function check_State(eventTarget, eventArgument) {  
    
          if (document.form_newOffer.states.value != "0") {
                
            return true;
        
          } else {
             
                return false; }
    
    }
    The ddl control:
    Code:
    <asp:dropdownlist ID="product" onChange="if (check_State())" runat="server" CssClass="letter1" OnSelectedIndexChanged="Certificates" Autopostback="true"></asp:dropdownlist>
    And the Sub I want to trigger:
    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
      ...
    If I put this code, the postback happens when the control value 'states.value' is different to '0', so, correct. But I don' t know why the Certificates Sub isn' t triggered. What is wrong?

    Thanks

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Location
    Barcelona
    Posts
    70
    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..?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Location
    Barcelona
    Posts
    70
    ..that was 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?

  6. #6
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

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

    use your script on a list with AutoPostback=false(or an html list) and use this to fire your postback when it is needed

    http://vbforums.com/showthread.php?t=329597
    Magiaus

    If I helped give me some points.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width