【问题标题】:can't get generated controls through AJAX response无法通过 AJAX 响应获取生成的控件
【发布时间】:2010-08-15 13:34:31
【问题描述】:

我使用 ASP.NET C# 和 AJAX Professional (http://www.ajaxpro.info)

1) 我有一个带有 Panel 控件的 div 容器,Panel 控件应该包含将在代码隐藏函数中生成的 DropDownList:

<div id="divDDL" runat="server">
    <asp:Panel ID="Panel1" runat="server">
    </asp:Panel>
</div>

2) 我有一个 JS 脚本函数“getDDL”,它将数据发送到代码隐藏函数,然后它使用生成的 Panel 和 DropDownList 控件接收响应:

function getDDL(lng)
{
    MyCodebahindClass.GetDDL(0, lng, callbackDDL);
    //callbackDDL is a response function
}

function callbackDDL(response)
{
    //here the response with the generated DropDownList and Panel control comes to the div element
    document.getElementById('<%=divDDL.ClientID %>').innerHTML = response.value;
}

3) Codebehind 函数“GetDDL”必须返回 Panel 控件内生成的 DropDownList:

[Ajax.AjaxMethod]
public Panel GetDDL(int itemId, int lng)
{
     PanelID = Panel1.ID;
     DropDownList rubricDDL = new DropDownList();
     rubricDDL.ID = "Fashionable_Catheter";
     rubricDDL.DataTextField = "title";
     rubricDDL.DataValueField = "id";
     rubricDDL.DataSource = %LINQ STUFF%;
     rubricDDL.DataBind();

     panelID.Controls.Add(rubricDDL);
     return panelID;
}

4) 当我尝试通过 JS 响应获取生成的 Panel 和 DropDownList 时,我只收到文本“System.Web.UI.Design.Panel”或类似的内容,尝试仅生成 DropDownList - 出现类似的文本"System.Web.UI.Design.DropDownList"。

但是当我调用代码隐藏函数来获取这两个控件时,我看到它们没有任何问题。为什么我不能通过 JS 获取它们?我做的一切都很好,调试了一百万次,没有发现任何问题,我不知道 JavaScript 有什么问题? 非常感谢任何帮助。

【问题讨论】:

    标签: c# javascript asp.net ajax ajaxpro


    【解决方案1】:

    嗯,我认为您需要返回面板的渲染 html。所以你的方法应该返回字符串,你需要在你的方法中渲染 Panel Control 并返回渲染的 html。

    【讨论】:

      【解决方案2】:
      [Ajax.AjaxMethod]
      public string GetDDL(int itemId, int lng)
      {
          PanelID = Panel1.ID;
          DropDownList rubricDDL = new DropDownList();
          rubricDDL.ID = "Fashionable_Catheter";
          rubricDDL.DataTextField = "title";
          rubricDDL.DataValueField = "id";
          rubricDDL.DataSource = %LINQ STUFF%;
          rubricDDL.DataBind();
          panelID.Controls.Add(rubricDDL);
          StringBuilder sb = new StringBuilder();
          HtmlTextWriter htw = new HtmlTextWriter(new StringWriter(sb));
          panelID.RenderControl(htw);
          return sb.ToString(); 
      }
      

      在 ajax Response 中显示输出等任何 control.html

      (即)div1.html(ajaxresposeoutput)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-10
        • 1970-01-01
        相关资源
        最近更新 更多