2017-03-16 1 views
0

포스트 백 이벤트에서 모든 입력 컨트롤을 가져오고 싶습니다.Request.Form의 특성을 가진 모든 컨트롤을 가져 옵니까?

이 샘플 컨트롤은 내가 가지고 있습니다 :

<input name="ctl00$ContentBody$dt_62f6f44864ec4c4892ac074da0209ff4 
type="text" value="11.06.2014" 
id="ContentBody_dt_62f6f44864ec4c4892ac074da0209ff4" 
class="m-wrap span12 date form_datepicker form-control" 
data-pagetype="main" 
data-groupname="group_DATE" 
data-rowindex="0" data-objtype="Datepicker" 
data-columnname="DATE_FROM" style="width:50px;"> 

핸들 모든 키

public Collection<ActionContainer.RequestFormParameter> GetFormParameters() 
{ 
    System.Collections.IEnumerator e2 = Request.Form.GetEnumerator(); 

    while (e2.MoveNext()) 
    { 
     ActionContainer.RequestFormParameter params_; 
     String xkey = (String)e2.Current; // output "ContentBody_dt_62f6f44864ec4c4892ac074da0209ff4" 
     String xval = Request.Form.Get(xkey); // output "11.06.2014" 
     String AttrCollection = ?? 

     // I try to find control by id but it didn't work for me 
    } 
} 
+0

동적으로 클라이언트 측에서 생성 된 입력 태그입니다하거나 텍스트 상자 서버 컨트롤로 ASPX 페이지에 이미 ... 그것은 그러나 매우 복잡한 방법, 수행 할 수 있습니다 볼 수 있듯이 디자인 타임에? – Win

+0

'name'을 보면 이미 페이지에있는 것처럼 보입니다. 내 대답이 많이 도움이되지 않는다면 ... – VDWWD

답변

0

속성이는 Request.Form 컬렉션에 포함되지 않습니다. 따라서 FindControl을 사용하여 컨트롤을 찾고 속성에 액세스해야합니다. 하지만 컨트롤 이름 (key) 만 있으면 실제로 ID을 얻는 데 창의적이어야합니다.

ID가 dt_62f6f44864ec4c4892ac074da0209ff4 인 TextBox가있는 것으로 가정합니다. 폼 포스트에서는 ctl00$ContentPlaceHolder1$dt_62f6f44864ec4c4892ac074da0209ff4과 같을 것입니다. 따라서 우리는 ID을 다시 추출 할 수 있습니다.

ID가 FindControl을 사용하여 컨트롤을 찾으면 올바른 컨트롤 유형으로 캐스트되고 해당 속성이 반복됩니다.

//loop all the items in the form collection 
foreach (string key in Request.Form.Keys) 
{ 
    //check if the key contains a $, in which case it is probably an aspnet control 
    if (key.Contains("$")) 
    { 
     //split the control name 
     string[] keyArrar = key.Split('$'); 

     //get the last part in the array, which should be the ID of the control 
     string controlID = keyArrar[keyArrar.Length - 1]; 

     //try to find the control with findcontrol, in this case with master pages 
     object control = this.Master.FindControl("mainContentPane").FindControl(controlID) as object; 

     //check if the control exist and if it is a textbox 
     if (control != null && control is TextBox) 
     { 
      //cast the object to the actual textbox 
      TextBox tb = control as TextBox; 

      //loop all the attributes of the textbox 
      foreach (string attr in tb.Attributes.Keys) 
      { 
       //get the key and value of the attribute 
       Response.Write(attr + ": " + tb.Attributes[attr] + "<br>"); 
      } 
     } 
    } 
} 

출력

data-pagetype: main 
data-groupname: group_DATE 
data-rowindex: 0 
data-objtype: Datepicker 
data-columnname: DATE_FROM 

영문 페이지의 데모 텍스트 상자.

<asp:TextBox ID="dt_62f6f44864ec4c4892ac074da0209ff4" runat="server" 
    data-pagetype="main" 
    data-groupname="group_DATE" 
    data-rowindex="0" 
    data-objtype="Datepicker" 
    data-columnname="DATE_FROM"> 
</asp:TextBox> 

당신은

+0

거기에 문제가있다. 미안하지만, 내가 컨트롤을 동적으로 만들었 기 때문에 findcontrol을 사용할 수 없다. – Mennan

+0

그건 중요하지 않아, 동적으로 생성 된 버튼에 대해 FindControl을 사용할 수있다. 모든 포스트 백에서 재생성되기 때문에 내 스 니펫이 사용됩니다. – VDWWD

관련 문제