无法通过AJAX响应获取生成的控件

发布于 2024-09-15 02:40:53 字数 1536 浏览 3 评论 0原文

我使用 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)代码隐藏函数“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 出了什么问题? 非常感谢任何帮助。

I use ASP.NET C# with AJAX Professional (http://www.ajaxpro.info)

1) I have a div container with Panel control, Panel control should hold DropDownList that will be generated in codebehind function:

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

2) I have a JS script function "getDDL" that sends data to codebehind function and then it receives response with generated Panel and DropDownList controls:

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 function "GetDDL" must return generated DropDownList inside the Panel control:

[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) When I try to get the generated Panel and DropDownList through the JS response I receive only the text "System.Web.UI.Design.Panel" or something like that, tried to generate only DropDownList - similar text shows up "System.Web.UI.Design.DropDownList".

But when I call a codebehind function to get these two controls I see them without any problems. Why I can't get them through JS? I do everything fine, debugged million times and didn't see any problems, I can't figure out what's wrong with JavaScript?
Any help much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆之渊 2024-09-22 02:40:53

嗯,我认为你需要返回面板的渲染 html。因此,您的方法应该返回字符串,并且您需要在您的方法中渲染面板控件并返回渲染的 html。

Hmm, I think you need to return rendered html of the panel. So your method should return string and you need to render Panel Control in your method and return rendered html.

听风念你 2024-09-22 02:40:53
[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)

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

in ajax Response display output as such that any of the control.html

(i.e) div1.html(ajaxresposeoutput)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文