0

我将 ASP.NET C# 与 AJAX Professional ( http://www.ajaxpro.info )一起使用

1)我有一个带有面板控件的 div 容器,面板控件应该包含将在代码隐藏函数中生成的 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 有什么问题?非常感谢任何帮助。

4

2 回答 2

0

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

于 2010-10-11T10:00:49.870 回答
0
[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 响应中显示输出,如任何 control.html

(即) div1.html(ajaxresposeoutput)

于 2013-06-05T07:22:39.213 回答