简体   繁体   English

将html文本框值从jquery模态传递到背后的asp代码

[英]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 此代码显示一个带有html文本框的Jquery对话框,该对话框在用户单击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. 当用户在对话框中单击新按钮时,我想从文本框中获取名称,并在asp new button click事件背后的代码中使用它。

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. 我不太确定这是否是您要查找的内容,但是您可以将Name值传递给服务器端Web方法。 by using Jquery's Ajax with in the function for the New button. 通过在New按钮的函数中使用Jquery的Ajax与。

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 到页面顶部,然后在后面的代码中像这样创建一个Web方法

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

The Ajax call would replace the Ajax调用将取代

 $(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());
    }
});

} }

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来获取值,在这种情况下,此参数可以在后面的代码中获取值。

Request.Form["Name"]

One note , remove the <form></form> from your dialogue, because you break the asp.net form and probably not work. 请注意 ,从对话框中删除<form></form> ,因为您破坏了asp.net表单并且可能无法正常工作。

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>')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM