【问题标题】:jquery serialize and .net controlsjquery 序列化和 .net 控件
【发布时间】:2011-12-29 18:18:51
【问题描述】:

我将 jquery serialize() 与 asp.net 服务器控件一起使用。

        <asp:DropDownList ID="ddFirmaAd" runat="server" Width="180" Height="25">
        </asp:DropDownList>

当我打印数组时

ajaxRequest("AjaxServices/Insert.aspx", $("#aspnetForm").serialize(), $('#returnMessage'), 0, $(this)); 

 function ajaxRequest(pageURL, queryString, putArea, timeout, disabledCtrl) {
        if (disabledCtrl != null)
            disabledCtrl.attr("disabled", "true");
        $.ajax({
            async: true,
            timeout: timeout,
            cache: false,
            url: pageURL + "?" + queryString,  


alert($("#aspnetForm").find("input,textarea,select,hidden").not("#__VIEWSTATE,#__EVENTVALIDATION").serialize());

它在 window ="ct100%24ct100%24ContentPlaceHolder1%24ContentPlaceHolder1%24ddFirmaAd = 2" 上打印 ddFirmaAd 客户端名称

我想在后面的代码中获取 ddFirmaAd.selectedValue

string value = request.queryString("ddFirmaAd");

但控件名称 = ct100%24ct100%24ContentPlaceHolder1%24ContentPlaceHolder1%24ddFirmaAd

如何使用 serialize() 和 .net 控件?

【问题讨论】:

  • @Robotsushi:ASP.NET 自动将元素的name 属性设置为完全限定的控件层次结构名称,使得它们几乎不能在 ASP.NET 服务器控件生态系统之外的代码中使用。他正在使用 jQuery 进行序列化,它使用 name 属性来构建表单值(因为 HTTP POST 使用 name 属性来识别 form 元素)。大概是将它们发送到不同的页面(不是具有控件的可后发页面)。因此,很难查询字符串值。
  • 为什么要序列化 ​​.net 控件? (不是批评只是对您的用例感到好奇)
  • 我有动态控件列出数据库中的数据。

标签: asp.net .net jquery webforms


【解决方案1】:

string value = request.QueryString(ddFirmaAd.ClientID);怎么样

但如果你将这些值传递到不同的页面,你可以像这样破解它:

string value = GetValueById("ddFirmaAd");

private string GetValueById(string endsWith)
    {
        var qs = HttpContext.Current.Request.QueryString;
        foreach(var key in qs)
        {
            string skey = key.ToString();
            if(skey.EndsWith(endsWith))
            {
                return qs[skey];
            }

        }
        return string.Empty;
    }

并不是说它是正确的。根据当前提供的问题描述,根本无法提出更好的建议。

您可以将 ClientIDMode 配置为静态,但随后维护唯一 ID 成为您的责任,并且可能导致难以找到客户端错误。

【讨论】:

  • 它不能。我使用 serialize() 方法将数据发布到另一个文件中
【解决方案2】:

如果您使用的是 Asp.Net 4.0,您可以使用 ClientIdMode = "Static" 以让客户端 id 与 id 相同。更多信息,请查看http://beyondrelational.com/blogs/hima/archive/2010/07/16/all-about-client-id-mode-in-asp-net-4.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    • 1970-01-01
    • 2013-08-31
    • 2010-12-15
    相关资源
    最近更新 更多