Wednesday, April 20, 2011

How to get textbox value from client side and create query string from it?

I'm using jquery for modal dialogs. I want to open model dialog from one page and to send some additional query string to modal dialog page. something like this:

 <asp:HyperLink ID="hypClientSearch" runat="server" NavigateUrl="~/SomePage.aspx?KeepThis=true&additionalQS='<%= txtBox.Text %>'&TB_iframe=true&height=650&width=800&modal=true" CssClass="thickbox" >

This example doesn't work. Does anyone know the solution?

From stackoverflow
  • Try this in the modal dialog when it opens (this is client side javascript):

    var textBoxValue = window.opener.document.getElementById("txtBoxId").value;
    

    You then use Javascript to insert the additional info into the correct place in the dialog, for example using JQuery.

  • In addition to Helgi's answer.
    If you want to get the value of the textbox using jQuery (for when you need to use other selectors then the id) you can use:

    var textBoxValue = $(textBoxSelector, window.opener.document).val();
    

    Edit
    Oh I just noticed that your using modal. Then the page is opened in an iFrame, you can get the value from within the iFrame using:

    var textBoxValue = $(textBoxSelector, window.parent.document).val();
    

    Also if you need to send it to the server at the iFrame request try editing the href attribite of the link on click:

    $('#hypClientSearch').click( function() {
     var textBoxContent = $(textBoxSelector).val();
     $(this).attr('href', 'somepage.aspx?textbox='+textBoxContent+'&otherVarsForModal=foo');
     //we let the event bubble for the modal plugin, so ne returning false here
    });
    

0 comments:

Post a Comment