2017-03-16 2 views
0

Ich möchte alle Eingabesteuerelemente auf Postback-Ereignis erhalten.Erhalten Sie alle Steuerelemente mit Attributen von Request.Form?

Dies ist ein Beispielsteuer ich habe:

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

Handle alle Schlüssel

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

Sind die Eingangs-Tags dynamisch generiert von Client-Seite oder sind sie bereits in ASPX Seite als TextBox-Serversteuer zur Entwurfszeit? – Win

+0

Nach dem Namen zu urteilen, sieht es so aus, als wären sie bereits auf der Seite. Wenn nicht, würde meine Antwort nicht viel helfen ... – VDWWD

Antwort

0

Attribute werden nicht in der Request.Form Sammlung enthalten. Daher müssen Sie FindControl verwenden, um die Eigenschaften von Control zu finden und darauf zuzugreifen. Aber da alles, was Sie haben, ist der Control Name (key), müssen Sie kreativ sein, um die tatsächliche ID.

Nehmen wir an, Sie haben eine TextBox mit der ID dt_62f6f44864ec4c4892ac074da0209ff4. In der Form Post wird es so etwas wie ctl00$ContentPlaceHolder1$dt_62f6f44864ec4c4892ac074da0209ff4, so basierend darauf können wir die ID wieder extrahieren.

Mit der ID FindControl verwenden, um das Steuerelement zu finden, wird Cast auf den richtigen Steuerelementtyp und Schleife seine Eigenschaften.

//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>"); 
      } 
     } 
    } 
} 

Ausgabe

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

Die Demo TextBox auf der aspx Seite.

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

Wie Sie sehen, es getan werden kann, ist es jedoch eine sehr komplizierte Art und Weise ist ...

+0

Es gibt ein Problem darin tut mir leid, dass ich das übertrug, ich erstellte Kontrollen dynamisch, also konnte ich findcontrol – Mennan

+0

nicht verwenden, das Sie FindControl für dynamisch erzeugte Tasten noch verwenden können, solange wie sie auf jedem PostBack neu generiert werden und bevor mein Snippet verwendet wird. – VDWWD

Verwandte Themen