将jquery模态中的html文本框值传递给asp代码隐藏

本文关键字:值传 asp 隐藏 代码 文本 模态 jquery html | 更新日期: 2023-09-27 18:20:12

此代码显示一个Jquery对话框,其中包含一个html文本框,该文本框在用户单击asp:按钮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"];
    }

当用户点击对话框中的新按钮时,我想从文本框中获取名称,并在我的代码中使用它来处理asp新按钮点击事件。

将jquery模态中的html文本框值传递给asp代码隐藏

我真的不确定这是否是您想要的,但您可以将Name值传递给服务器端的Web方法。通过在New按钮的函数中使用Jquery的Ajax。

在代码隐藏页的服务器端,您可以通过添加来创建Web方法

using System.Web.Services;

到你的页面顶部,然后在你的代码后面创建一个web方法,就像这个

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

Ajax调用将取代

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

您当前在"新建按钮"单击事件中具有的类似内容。

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

}

因为已经准备好了一个输入控件,并且你发布了一个post,所以通常你可以通过使用Request.Form来获取值,在你的情况下,这个参数会在代码后面获取你的值。

Request.Form["Name"]

请注意,从对话中删除<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>')