2017-03-16 14 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を使用してControlを見つけ、そのプロパティにアクセスする必要があります。しかし、あなたが持っているのはコントロール名(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 

aspxページ上のデモのTextBox。

<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

それは問題ではありません。すべてのPostBackで再生されているので、私のスニペットが使用される前に。 – VDWWD

関連する問題