Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Send value to server side

Send value to server side

Scheduled Pinned Locked Moved ASP.NET
helpjavascriptdatabasesysadmintutorial
18 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Elham M
    wrote on last edited by
    #1

    Hi I have an Interdependent select menus allow you to populate one select menu based on a choice made in another select menu in javascript. For example, a menu of cities might change based on the state that was selected in another menu .my problem is I can't access the value of menu in server side because I want to insert this value in database but when I write runat="server" in select tag it doesen't work correctly what can I do to get the value of this menu in asp please help me?:confused: Regards

    N S 2 Replies Last reply
    0
    • E Elham M

      Hi I have an Interdependent select menus allow you to populate one select menu based on a choice made in another select menu in javascript. For example, a menu of cities might change based on the state that was selected in another menu .my problem is I can't access the value of menu in server side because I want to insert this value in database but when I write runat="server" in select tag it doesen't work correctly what can I do to get the value of this menu in asp please help me?:confused: Regards

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Elham M wrote:

      it doesen't work correctly

      We need a little more than this. What is the observed behavior? What doesn't work? You could gather the data on the client side (JavaScript) and call and ajax method to push it to the server. Or you could, using JavaScript again, store the selected menu values in a hidden field.


      I know the language. I've read a book. - _Madmatt

      E 1 Reply Last reply
      0
      • E Elham M

        Hi I have an Interdependent select menus allow you to populate one select menu based on a choice made in another select menu in javascript. For example, a menu of cities might change based on the state that was selected in another menu .my problem is I can't access the value of menu in server side because I want to insert this value in database but when I write runat="server" in select tag it doesen't work correctly what can I do to get the value of this menu in asp please help me?:confused: Regards

        S Offline
        S Offline
        shankysharma86
        wrote on last edited by
        #3

        Your query is not clear still.... Keep autopostback property of your combo box true and on SelectIndexChanged() event of ComboBox you can take the value Selected by user.

        N 1 Reply Last reply
        0
        • S shankysharma86

          Your query is not clear still.... Keep autopostback property of your combo box true and on SelectIndexChanged() event of ComboBox you can take the value Selected by user.

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          And where did the OP mention anything about a combobox?


          I know the language. I've read a book. - _Madmatt

          1 Reply Last reply
          0
          • N Not Active

            Elham M wrote:

            it doesen't work correctly

            We need a little more than this. What is the observed behavior? What doesn't work? You could gather the data on the client side (JavaScript) and call and ajax method to push it to the server. Or you could, using JavaScript again, store the selected menu values in a hidden field.


            I know the language. I've read a book. - _Madmatt

            E Offline
            E Offline
            Elham M
            wrote on last edited by
            #5

            I've didn't work with ajax method How can I call it to push to the server? this is my code: var MENU = []; MENU[0] = []; MENU[1] = []; MENU[0][0] = new Option("New York", "NY"); MENU[0][1] = new Option("New York City", "NYC"); MENU[0][2] = new Option("Syracuse", "SYR"); MENU[1][0] = new Option("California", "CA"); MENU[1][1] = new Option("Los Angeles", "LAN"); MENU[1][2] = new Option("San Diego", "SDI"); function populateMain(mainSel, subSel) { var mainMenu = mainSel; var subMenu = subSel; mainMenu.options.length = 0; for (var i = 0; i < MENU.length; i++) { mainMenu.options[i] = MENU[i][0]; } populateSub(mainMenu, subMenu); } function populateSub(mainSel, subSel) { var mainMenu = mainSel; var subMenu = subSel; var optMainMenu; subMenu.options.length = 1; optMainMenu = mainMenu.selectedIndex; for (var i = 1; i < MENU[optMainMenu].length; i++) { subMenu.options[i] = MENU[optMainMenu][i]; } } --Please Choose-- ////////////On server side protected void Page_Load(object sender, EventArgs e) { const string someScript = "alertMe"; if (!ClientScript.IsStartupScriptRegistered(this.GetType(), someScript)) { ClientScript.RegisterStartupScript(this.GetType(), someScript, "populateMain(document.getElementById('State'), document.getElementById('City'));", true); } } I can't get the value of menu in server side!I wan't to save them in database so what's your sugestion?

            N 1 Reply Last reply
            0
            • E Elham M

              I've didn't work with ajax method How can I call it to push to the server? this is my code: var MENU = []; MENU[0] = []; MENU[1] = []; MENU[0][0] = new Option("New York", "NY"); MENU[0][1] = new Option("New York City", "NYC"); MENU[0][2] = new Option("Syracuse", "SYR"); MENU[1][0] = new Option("California", "CA"); MENU[1][1] = new Option("Los Angeles", "LAN"); MENU[1][2] = new Option("San Diego", "SDI"); function populateMain(mainSel, subSel) { var mainMenu = mainSel; var subMenu = subSel; mainMenu.options.length = 0; for (var i = 0; i < MENU.length; i++) { mainMenu.options[i] = MENU[i][0]; } populateSub(mainMenu, subMenu); } function populateSub(mainSel, subSel) { var mainMenu = mainSel; var subMenu = subSel; var optMainMenu; subMenu.options.length = 1; optMainMenu = mainMenu.selectedIndex; for (var i = 1; i < MENU[optMainMenu].length; i++) { subMenu.options[i] = MENU[optMainMenu][i]; } } --Please Choose-- ////////////On server side protected void Page_Load(object sender, EventArgs e) { const string someScript = "alertMe"; if (!ClientScript.IsStartupScriptRegistered(this.GetType(), someScript)) { ClientScript.RegisterStartupScript(this.GetType(), someScript, "populateMain(document.getElementById('State'), document.getElementById('City'));", true); } } I can't get the value of menu in server side!I wan't to save them in database so what's your sugestion?

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              Now we can see what you are doing. Of course you can't get the value in the code behind because you are setting the values on the client side, the server knows nothing about this. You would be better off binding the combobox to a data source, it doesn't have to be a data table or dataset, any collection will do. What you appear to be attempting to do is called a cascading drop down and there are multiple sources available if you look.


              I know the language. I've read a book. - _Madmatt

              E 1 Reply Last reply
              0
              • N Not Active

                Now we can see what you are doing. Of course you can't get the value in the code behind because you are setting the values on the client side, the server knows nothing about this. You would be better off binding the combobox to a data source, it doesn't have to be a data table or dataset, any collection will do. What you appear to be attempting to do is called a cascading drop down and there are multiple sources available if you look.


                I know the language. I've read a book. - _Madmatt

                E Offline
                E Offline
                Elham M
                wrote on last edited by
                #7

                But how can I bind this combobox to a data source!!? :omg: Do you see my code? my combobox isn't a component

                N 1 Reply Last reply
                0
                • E Elham M

                  But how can I bind this combobox to a data source!!? :omg: Do you see my code? my combobox isn't a component

                  N Offline
                  N Offline
                  Not Active
                  wrote on last edited by
                  #8

                  Then make it so! Why are you making this more difficult on yourself?


                  I know the language. I've read a book. - _Madmatt

                  E 1 Reply Last reply
                  0
                  • N Not Active

                    Then make it so! Why are you making this more difficult on yourself?


                    I know the language. I've read a book. - _Madmatt

                    E Offline
                    E Offline
                    Elham M
                    wrote on last edited by
                    #9

                    Because my combobox is not a server control, it's a html control so how can I bind data source to html control?!please guide me.

                    N 1 Reply Last reply
                    0
                    • E Elham M

                      Because my combobox is not a server control, it's a html control so how can I bind data source to html control?!please guide me.

                      N Offline
                      N Offline
                      Not Active
                      wrote on last edited by
                      #10

                      Once again. Why must it be an html control? Why are you making it difficult on yourself, and us, by not using ASP.NET controls in an ASP.NET application?


                      I know the language. I've read a book. - _Madmatt

                      E 1 Reply Last reply
                      0
                      • N Not Active

                        Once again. Why must it be an html control? Why are you making it difficult on yourself, and us, by not using ASP.NET controls in an ASP.NET application?


                        I know the language. I've read a book. - _Madmatt

                        E Offline
                        E Offline
                        Elham M
                        wrote on last edited by
                        #11

                        Because I want to do this in client side Not in server side client side is faster than server side Do you have any suggestion or not?

                        N 1 Reply Last reply
                        0
                        • E Elham M

                          Because I want to do this in client side Not in server side client side is faster than server side Do you have any suggestion or not?

                          N Offline
                          N Offline
                          Not Active
                          wrote on last edited by
                          #12

                          Elham M wrote:

                          client side is faster than server side

                          Not necessarily. There are many factors to consider. By hard coding values into the JavaScript you have reduced the maintainability and extensibility of the code and application making it more difficult on yourself and those that may follow. Knowing the proper tools and when to use them is the mark of a real developer not just a hobbyist or hacker. Which are you? You can easily get the values using JQuery

                          var menuVal = $("#menu").val();
                          var subMenuVal = $("#submenu").val();

                          One way of passing them to a server method

                          Code-behind

                          [WebMethod]
                          public static void Foo(string menu, string subMenu)
                          {
                          ...
                          }

                          JavaScript
                          PageMethods.Foo( menuVal, subMenuVal);


                          I know the language. I've read a book. - _Madmatt

                          M E 2 Replies Last reply
                          0
                          • N Not Active

                            Elham M wrote:

                            client side is faster than server side

                            Not necessarily. There are many factors to consider. By hard coding values into the JavaScript you have reduced the maintainability and extensibility of the code and application making it more difficult on yourself and those that may follow. Knowing the proper tools and when to use them is the mark of a real developer not just a hobbyist or hacker. Which are you? You can easily get the values using JQuery

                            var menuVal = $("#menu").val();
                            var subMenuVal = $("#submenu").val();

                            One way of passing them to a server method

                            Code-behind

                            [WebMethod]
                            public static void Foo(string menu, string subMenu)
                            {
                            ...
                            }

                            JavaScript
                            PageMethods.Foo( menuVal, subMenuVal);


                            I know the language. I've read a book. - _Madmatt

                            M Offline
                            M Offline
                            Morgs Morgan
                            wrote on last edited by
                            #13

                            I hope this will help u, if not ask if u need further clarification (there is code here!). Otherwise use ajax to send you newely populated data back to the server.

                            E N 2 Replies Last reply
                            0
                            • N Not Active

                              Elham M wrote:

                              client side is faster than server side

                              Not necessarily. There are many factors to consider. By hard coding values into the JavaScript you have reduced the maintainability and extensibility of the code and application making it more difficult on yourself and those that may follow. Knowing the proper tools and when to use them is the mark of a real developer not just a hobbyist or hacker. Which are you? You can easily get the values using JQuery

                              var menuVal = $("#menu").val();
                              var subMenuVal = $("#submenu").val();

                              One way of passing them to a server method

                              Code-behind

                              [WebMethod]
                              public static void Foo(string menu, string subMenu)
                              {
                              ...
                              }

                              JavaScript
                              PageMethods.Foo( menuVal, subMenuVal);


                              I know the language. I've read a book. - _Madmatt

                              E Offline
                              E Offline
                              Elham M
                              wrote on last edited by
                              #14

                              Thaks alot Thanks...

                              1 Reply Last reply
                              0
                              • M Morgs Morgan

                                I hope this will help u, if not ask if u need further clarification (there is code here!). Otherwise use ajax to send you newely populated data back to the server.

                                E Offline
                                E Offline
                                Elham M
                                wrote on last edited by
                                #15

                                I think ajax is good suggestion But where is your code?!

                                M 1 Reply Last reply
                                0
                                • M Morgs Morgan

                                  I hope this will help u, if not ask if u need further clarification (there is code here!). Otherwise use ajax to send you newely populated data back to the server.

                                  N Offline
                                  N Offline
                                  Not Active
                                  wrote on last edited by
                                  #16

                                  Who were you responding to?

                                  MorgSim wrote:

                                  I hope this will help u

                                  Hope what helps?

                                  MorgSim wrote:

                                  use ajax to send you newely populated data back to the server.

                                  Brilliant! Exactly what I have suggested days ago.


                                  I know the language. I've read a book. - _Madmatt

                                  M 1 Reply Last reply
                                  0
                                  • E Elham M

                                    I think ajax is good suggestion But where is your code?!

                                    M Offline
                                    M Offline
                                    Morgs Morgan
                                    wrote on last edited by
                                    #17

                                    Try this: 1. Default.aspx

                                    <select id="sel1"><option>came here first!</option></select>
                                    <select id="sel2"><option>came here second!</option></select>

                                    //anywhere in your default.aspx insert this:
                                    <asp:ScriptManager ID="test" runat="server">
                                    <Services>
                                    <asp:ServiceReference Path="test.asmx" />
                                    </Services>
                                    </asp:ScriptManager>

                                    2. Your JS/SQuery

                                    $(function(){
                                         if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
                                         $('#send').click(function(){
                                               $sel1\_value = $('#sel1').val();
                                               $sel2\_value = $('#sel2').val();
                                               test.SendData($sel1\_value, $sel2\_value, SendSuccess, SendFailed);
                                          });
                                          function SendSuccess(result, eventArgs)
                                          {
                                              alert(result);
                                          }
                                          function SendFailed(error)
                                          {
                                              alert(error);//server side exception
                                          }
                                     });
                                    

                                    3. Webservice (test.asmx) - Create a new webservice called test.asmx - Create a new [WebMethod] called SendData in this class like so:

                                    //uncomment whatever you are asked to uncomment in this class
                                    \[WebMethod\]
                                    public string SendDate(string sel1, string sel2)
                                    {
                                        //process your received data here using the passed parameters
                                        //care to return response if need be!
                                        return "...holla! i got here last!!!";
                                    }
                                    

                                    Oops!!! don't forget to download and reference the latest JQuery Library in your project! Goodluck

                                    1 Reply Last reply
                                    0
                                    • N Not Active

                                      Who were you responding to?

                                      MorgSim wrote:

                                      I hope this will help u

                                      Hope what helps?

                                      MorgSim wrote:

                                      use ajax to send you newely populated data back to the server.

                                      Brilliant! Exactly what I have suggested days ago.


                                      I know the language. I've read a book. - _Madmatt

                                      M Offline
                                      M Offline
                                      Morgs Morgan
                                      wrote on last edited by
                                      #18

                                      Mark Nischalke wrote:

                                      Hope what helps?

                                      Your suggestion

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups