John Doe
John Doe

Reputation: 149

Pass html textbox value from a jquery modal to asp code behind

This code displays a Jquery dialog with an html textbox that is shown after the user clicks on an asp:button btnNewGroup

<script type="text/javascript" language="javascript">
function pageLoad(sender, args) {
    var $dialog = $('<div><fieldset><label for="name">Name</label><input type="text" name="Name" id="name" class="text ui-widget-content ui-corner-all" /></div>')
        .dialog({
            modal: true,
            autoOpen: false,
            title: 'Enter New Group Name',
            buttons: {
                'New': function () {
                    $(this).dialog('close');
                },
                Cancel: function () {
                    $(this).dialog('close');
                }
            }
        });


        $("#<%=btnNewGroup.ClientID %>").click(function () {
            $dialog.dialog('open');
            return true;
        });
}
</script>

<asp:Button ID="btnNewGroup" runat="server" Height="26px" 
onclick="btnNewGroup_Click" Style="margin-bottom: 0px" Text="New Group" 
ClientIDMode="Static" />

protected void btnNewGroup_Click(object sender, EventArgs e)
    {
        String t = Request.Form["Name"];
    }

When the user clicks the new button in the dialogue I want to take the name from the textbox and use it in my code behinds asp new button click event.

Upvotes: 0

Views: 4017

Answers (2)

Jive Boogie
Jive Boogie

Reputation: 1265

I'm not really sure if this is what you are looking for but you coud pass the Name value to a Server side Web Method. by using Jquery's Ajax with in the function for the New button.

On the server side in your code behind page you create a Web Method by adding a

using System.Web.Services;

to the top of your page and then creating a web method in your code behind like this

[WebMethod]
public static void DoSomething(object name)
{
   //do something intersting
}

The Ajax call would replace the

 $(this).dialog('close');

That you currently have in your New Button click event with something like this.

var valFromNameBox = $("#name").val();
var jsonObj = '{name: "' + valFromNameBox + '"}';
var res;
$.ajax({
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    data: jsonObj,
    dataType: 'json',
    url: 'YourPage.aspx/DoSomething',
    success: function (result) {
    },
    error: function (err) {
        alert(err.toString());
    }
});

}

Upvotes: 1

Aristos
Aristos

Reputation: 66641

Because is all ready an input control, and you make a post, normally you can get the value by using the Request.Form, in your case this parameter gets your value on code behind.

Request.Form["Name"]

One note, remove the <form></form> from your dialogue, because you break the asp.net form and probably not work.

var $dialog = $('<div><fieldset><label for="name">Name</label>
<input type="text" name="Name" id="name" 
   class="text ui-widget-content ui-corner-all" /></fieldset></div>')

Upvotes: 0

Related Questions