2012-07-06 2 views
0

마스터 페이지를 사용하는 ASP 페이지가 있습니다. 내 코드 숨김 페이지의 [Webmethod]에 대한 AJAX 호출을 사용하여 페이지에 몇 개의 패널을 만듭니다. 다음 페이지 자체의 구조 ... FindControl이 동적으로 생성 된 컨트롤과 함께 작동하지 않습니다.

<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> 

내가의 WebMethod를 통해 "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가 null이라는 것을 계속 말합니다. 참고로, 나는 페이지에 추가 된 컨트롤을 추적하는 데 사용되는 카운터입니다. 내 유일한 생각은 아마 내가 clientID를 사용해야한다는 것입니다. 이것에 대한 도움은 크게 감사 할 것입니다.

컨트롤을 ...

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); 
        } 
       }) 
+3

포스트 백에서 컨트롤을 찾으려고합니까? 검색하기 전에 필요에 따라 다시 추가 했습니까? – Oded

+0

어떤 이벤트에서 동적 컨트롤을 추가합니까? 그것은 page_load보다 먼저 수행되어야하며 Oded가 말했듯이, 포스트 백에 다시 추가해야합니다. – TheGeekYouNeed

+0

캐스트가 작동하지 않습니다 : HiddenField. LiteralControl을 만들고 HiddenField로 찾으려하는 이유는 무엇입니까? HiddenField는 < 입력 유형 = "숨김">으로 렌더링되지만 캐스트 자체는 작동하지 않습니다. 새로운 hiddenfield 컨트롤을 추가 한 다음 findControl을 수행하십시오. 또한 페이지 수준에 대해 "추적"을 설정하고 렌더링 된 ID 등을 찾기 위해 컨트롤 트리를 볼 수 있습니다 (리터럴 컨트롤의 ClientID 렌더링이 정적으로 설정되지 않았을 수 있음) – Richthofen

답변

0

`패널 성분 = 새 패널을 HTML로 렌더링하고 페이지에 AJAX로 돌아와 다음과 같이 사업부에 추가된다(); component.ID = "comp"+ compNumber; component.Controls.Add (새 LiteralControl (""));

// 여기에 한 줄

comp_list.Add (구성 요소)를 추가,`

가 작동합니다 지금 시도한다.

+0

나는 여기 새로워서 사용할 수있는 서식 지정 옵션이 많지 않다. – Sameer

+0

webmethod에서 작성하기 때문에이 방법을 사용할 수 없다. – kjaenick

+0

"comp_list"패널에 컨트롤을 추가하는 위치가 표시되지 않습니다. 나는 그것을위한 부호를 보지 않는다. 또한 webmethods 정적 그래서 당신이 asp.net 컨트롤을 추가 할 수 있다고 생각하지 않습니다. 스크립트를 사용하여 시도해야합니다. – Sameer

관련 문제