【问题标题】:Pass html textbox value from a jquery modal to asp code behind将 html 文本框值从 jquery modal 传递到 asp 代码后面
【发布时间】:2012-01-14 08:49:55
【问题描述】:

此代码显示一个带有 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"];
    }

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

【问题讨论】:

    标签: c# jquery asp.net


    【解决方案1】:

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

    在后台代码的服务器端,您可以通过添加一个

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

    }

    【讨论】:

      【解决方案2】:

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

      Request.Form["Name"]
      

      请注意,请从对话中删除 &lt;form&gt;&lt;/form&gt;,因为您破坏了 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>')
      

      【讨论】:

      • 好的,删除了表单,当我尝试访问 Request.Form["Name"] 时,值为 null。
      • @JohnDoe 然后不会在 asp.net 的形式中呈现。确保在那里渲染它。
      • 现在它是一个弹出对话框我如何以asps形式呈现它?
      猜你喜欢
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 1970-01-01
      • 2015-01-06
      • 1970-01-01
      相关资源
      最近更新 更多