Jquery value to Asp.net Textbox

Hi , am trying to get the Selected value from a menu that is created with pure Jquery and CSS . I somehow tried to put the value in a html textbox , but when i make it runat="Server" , it's not putting any value in there. Please help me in this.

Here's the Part of a jquery code

this.chooseItem = function (item) {
          menu.kill();
          // edit this for your own custom function/callback:
            $('#selectmenu').val($(item).text());
     
          location.href = $(item).attr('href');
      };

Here's the html input box
<input type="text" id="selectmenu"
    />

Right now this code is working fine, but i want to get the value in the .net code behind so that i can pass it to a stored procedure.  Please let me know how can i pass the value to asp.net textbox.

I already tried  the below code for an asp.net textbox.  but it's not working.
<asp:TextBox ID="Test1" runat="server"></asp:TextBox>
 $('#<%= Test1.ClientID %>').val($(item).text());

I would appreciate your help.
gladstonesheebaAsked:
Who is Participating?
 
leakim971PluritechnicianCommented:
I can't find the following in sourcefile.docx

this.chooseItem = function (item) {
      menu.kill();
      // just to be sure it found the .net control :
      if( $('#<%= Test1.ClientID %>').length > 0 ) alert("found");
      // edit this for your own custom function/callback:
      $('#<%= Test1.ClientID %>').val( $(item).text() );
      //location.href = $(item).attr('href');
};

Open in new window

0
 
leakim971PluritechnicianCommented:
Modify the code to set a class to the selected value :

this.chooseItem = function (item) {
// remove the class for the previous selected item
         $(".selectedItem").removeClass(".selectedItem");
// add the class for the new one
         $(item).addClass(".selectedItem");
          menu.kill();
          // edit this for your own custom function/callback:
            $('#selectmenu').val($(item).text());
      
          location.href = $(item).attr('href');
      };

Open in new window


and use :

$("form").submit(function() {
  $('#<%= Test1.ClientID %>').val( $(".selectedItem").text() );
});

Open in new window

0
 
gladstonesheebaAuthor Commented:
Hi,
 Am sorry Little bit confusing to me.  what is that remove the class for the previous selected item and you've added the same thing again in the add class. Please let me know.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
leakim971PluritechnicianCommented:
when you click/choose a new item you may have previous item with the class.
the goal is to find the item having the class selected before postback
so we remove the class by default and set it ONLY for the new item
0
 
gladstonesheebaAuthor Commented:
how does that help in setting the textbox text value to the selected item. I tried it but still it's not assigning the text to the asp.net textbox control.

Where do i put the form.submit function()?.
0
 
leakim971PluritechnicianCommented:
Don't forget I don't see your page.

>here do i put the form.submit function()?.

in

$(document).ready(function() {
   // here
});
0
 
gladstonesheebaAuthor Commented:
Attached is the Jquery file for the menu selection. It uses Jquery-1.3.2.min.js plugin  . Am totally new to jquery , and am just using this code that  i got from an internet website. Please let me know how can i modify this so that i need to get the selected value to the .net code behind.
JqueryMenu.docx
0
 
leakim971PluritechnicianCommented:
let the code as you find it.
if it's a plugin, I do the job you want it do or search an other one :)

I would like to see the way you use it and the place you get it. thanks
0
 
gladstonesheebaAuthor Commented:
Hi,
 This is the place where i get those code.
http://filamentgroup.com/lab/jquery_ipod_style_and_flyout_menus/

0
 
gladstonesheebaAuthor Commented:
I want the same  flyout menu , but i want it to be able to get the selected item in the  .net code behind.

Please help me on this.

Thanks
0
 
leakim971PluritechnicianCommented:
the code you produce should work (so forget my code). Try without the redirection to see it, so like this :
this.chooseItem = function (item) {
      menu.kill();
      // just to be sure it found the .net control :
      if( $('#<%= Test1.ClientID %>').length > 0 ) alert("found");
      // edit this for your own custom function/callback:
      $('#<%= Test1.ClientID %>').val( $(item).text() );
      //location.href = $(item).attr('href');
};

Open in new window

0
 
gladstonesheebaAuthor Commented:
Hi,
 I don't think its finding the .Net control . Is there anyother way to do that?
0
 
leakim971PluritechnicianCommented:
could you provide a live link ? or right click on the page and choose view source ?

> Is there anyother way to do that?
this is the good way to do that

you may have an error blocking javascript
0
 
gladstonesheebaAuthor Commented:
Please find the attached file for  the source code and the image of the aspx page.
If i select the menu, the value is in Html input box, but  not in the asp.net Textbox.
sourcefile.docx
0
 
gladstonesheebaAuthor Commented:
ok, i finally tried and i can get the selected value in to the asp.net textbox control . But when i view the source by right clicking on the page , and i see there is no text assigned to the Textbox controls. and i couldn't get the value from the asp.net Textbox. Please see the attached image.
menu.PNG
0
 
leakim971PluritechnicianCommented:
>ok, i finally tried and i can get the selected value in to the asp.net textbox control
good

>But when i view the source by right clicking on the page , and i see there is no text assigned to the Textbox controls. and i couldn't get the value from the asp.net Textbox.
by viewing the source with Internet Explorer you can't see the value

0
 
gladstonesheebaAuthor Commented:

ok , but when i click the asp.net button to get the value from the Textbox control , the value disappears . how to retain that selected value everytime  in the Textbox so that i can get it in the button_click event.
0
 
leakim971PluritechnicianCommented:
>ok , but when i click the asp.net button to get the value from the Textbox control

put return false; at the OnClientClick attribute of your button
0
 
gladstonesheebaAuthor Commented:
Like this?.
<asp:Button id="btnTest" runat="server" onClientClick="return false;" OnClick="btnTest_Cllick" />
0
 
leakim971PluritechnicianCommented:
you mean currently you button is like this :

<asp:Button id="btnTest" runat="server" OnClick="btnTest_Cllick" />

Open in new window


please post your page
0
 
gladstonesheebaAuthor Commented:
yes that's correct .

Protected void  btnTest_Click(object sender,EventArgs e)
{
string selectedvalue =Test1.Text;

}
0
 
leakim971PluritechnicianCommented:
with this code you do a postback to the server and you have some code clearng your textbox

0
 
gladstonesheebaAuthor Commented:
That's what i want to know how to retain the value for the textbox so that i can get it in button_click event.Please let me know.
0
 
leakim971PluritechnicianCommented:
post the code of your page
0
 
gladstonesheebaAuthor Commented:
protected void Page_Load(object sender, EventArgs e)
        {

           
            HtmlLink objLink = new HtmlLink();

            objLink.Attributes["rel"] = "Stylesheet";
            objLink.Attributes["type"] = "text/css";
            // objLink.Attributes["media"] = "screen";
            objLink.Href = "~/Scripts/menu.css";
            Page.Header.Controls.Add(objLink);

            HtmlLink controlcss = new HtmlLink();
            controlcss.Attributes["rel"] = "Stylesheet";
            controlcss.Attributes["type"] = "text/css";
            //controlcss.Attributes["media"] = "screen";
            controlcss.Href = "~/Scripts/theme1/ui.all.css";
            Page.Header.Controls.Add(controlcss);
            menustring.Text = Test1.Text;


            if (!IsPostBack)
            {
               
                GetDatafortheMenu();
            }
        }

protected void btnTest_Click(object sender, EventArgs e)
        {
            string value = Test1.Text;
           
           

        }
0
 
gladstonesheebaAuthor Commented:
For the jquery
in ASPX page
<script type="text/javascript">
        var ids = {
            txtmenu: "<%= Test1.ClientID %>"
           
        }
</script>

in External file Menu.js

this.chooseItem = function (item) {

          menu.kill();
         // to set the Selected value  text in asp.net textbox control
          $("#" + ids.txtmenu).val($(item).text());
      

          location.href = $(item).attr('href');
      };
0
 
leakim971PluritechnicianCommented:
sorry I was away.

You should remove this : location.href = $(item).attr('href');
because it ask to left the page

I understand your need to save and restore the selected item but I'm not sure you choose the good way to pass value between cthe lient side and the server, especially to save/restore state of .net control

check this articles to see if you found good info :
http://msdn.microsoft.com/en-us/library/ms972976.aspx
http://www.codeproject.com/KB/viewstate/retainingstate.aspx
http://www.telerik.com/community/forums/aspnet/docking/again-save-state-fo-dynamic-added-controls.aspx

I need to sleep a bit, see you later

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.