繁体   English   中英

FindControl不适用于动态创建的控件

[英]FindControl not working with dynamically created controls

我有一个使用母版页的asp页。 我在页面后面的代码中使用AJAX调用[Webmethod]在页面上创建了一些面板。 这是页面本身的结构...

<form runat="server" class="form-horizontal" id="frm_estRequest">
   <div id="mainContent" runat="server" >
    <div id="contentInner" runat="server>
           <asp:Panel  id="comp_list" runat="server"></asp:Panel>
        </div>
   </div>
</form>

我通过web方法将动态创建的控件添加到“ comp_list”面板中。 这是在webmethod中创建的html ...

Panel component = new Panel();
component.ID = "comp" + compNumber;
component.Controls.Add(new LiteralControl("<input type='hidden' value='" + 
compType + "' ID='compType_" + compNumber + "'/>"));

所以我想做的就是从Html Hiddenfield中获取价值。 这是我必须要做的(由于某种原因而无法使用)。

ContentPlaceHolder cph =(ContentPlaceHolder)this.Master.FindControl("placeholder_content");
Control compList = (Control)cph.FindControl("frm_estRequest").FindControl("mainContent").FindControl("contentInner").FindControl("comp_list");
Control ct = (Control) compList.FindControl("comp" + i.ToString());
HiddenField compType = (HiddenField)ct.FindControl("compType_" + i.ToString());

我可以获取“ comp_list”控件,但到目前为止,我可以得到。 一直说ct为空。 仅供参考,我只是一个计数器,用于跟踪已添加到页面的控件。 我唯一的想法是,也许我应该使用clientID。 任何帮助,将不胜感激。

控件呈现为html,然后通过ajax返回到页面,然后像这样添加到div ...

System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter tw = new System.IO.StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
component.RenderControl(hw); 
return sb.ToString();

var req = $.ajax({ 
                    type: "POST", 
                    url: "EstRequest.aspx/createComponent", 
                    data: params,
                    contentType: "application/json; charset=utf-8", 
                    dataType: "json", 
                    success: function (data) { 
                            $('#<%= comp_list.ClientID%>').append(data.d) 
                            document.getElementById('<%= compNumber.ClientID %>').value = comp + 1;

                            $("#<%= compNumber.ClientID %>").live("click", function() {
                            $(this).next().slideToggle("fast")}); 

                            }, 
                    error: function (response) { debugger; alert("Error: " + params); }, 
                    failure: function (response) { 
                        alert(response.d); 
                    } 
                }) 

`Panel组件= new Panel(); component.ID =“ comp” + compNumber; component.Controls.Add(new LiteralControl(“”));

//在这里添加一行

comp_list.Add(component);`

现在尝试,它应该可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM