2015-06-04 67 views
1

我是C#的初學者,我有一個問題。這裏是我的用戶控件的下面如何循環userControl c#

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="JorgesAppli.WebUserControl1" %> 

<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> 

<table id="Table2" cellspacing="2" cellpadding="1" width="100%" border="0" rules="none" 
    style="border-collapse: collapse;"> 
    <tr class="EditFormHeader"> 
     <td colspan="2"> 
      <b>Enregistrer un nouveau partenaire</b> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <table id="Table3" width="450px" border="0" class="module"> 
       <tr> 
        <td>Raison sociale 1:</td> 
        <td> 
         <asp:TextBox ID="TextBox9" Text='<%# Bind("RaisonSocial") %>' runat="server" TabIndex="8"> 
         </asp:TextBox> 
        </td> 
       </tr> 
       <tr> 
        <td>Adresse : 
        </td> 
        <td> 
         <asp:TextBox ID="TextBox7" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Adresse") %>'> 
         </asp:TextBox> 
        </td> 
       </tr> 
       <tr> 
        <td>Code postal: 
        </td> 
        <td> 
         <asp:TextBox ID="TextBox8" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CodePostal") %>'>' TabIndex="1"> 
         </asp:TextBox> 
        </td> 
       </tr> 
       <tr> 
        <td>Type partenaire: 
        </td> 
        <td> 
         <telerik:RadDropDownList ID="RadDropDownList1" runat="server" DropDownHeight="200px" Width="226px" Skin="Default" 
          DefaultMessage="Sélectionnez le type de partennaires" DropDownWidth="226px" 
          DataValueField="IdTypePartenaire" DataTextField="Description" DataSourceID="SqlDataSource2" AutoPostBack="true"> 
         </telerik:RadDropDownList> 
        </td> 
       </tr> 
       <tr> 
        <td colspan="2"> 
         <b>SLE :</b> 
        </td> 
       </tr> 
       <tr> 
        <td>Prescription CE : 
        </td> 
        <td> 
         <asp:TextBox ID="TextBox2" Text='<%# Bind("Presription") %>' runat="server" TabIndex="8"> 
         </asp:TextBox> 
        </td> 
       </tr> 

       <tr> 
        <td>Raison sociale 2: 
        </td> 
        <td> 
         <asp:TextBox ID="TextBox10" Text='<%# Bind("RaisonSocial2") %>' runat="server" TabIndex="8"> 
         </asp:TextBox> 
        </td> 
       </tr> 
       <tr> 
        <td>Adresse(2): 
        </td> 
        <td> 
         <asp:TextBox ID="TextBox11" Text='<%# Bind("Adresse2") %>' runat="server" TabIndex="8"> 
         </asp:TextBox> 
        </td> 
       </tr> 
       <tr> 
        <td>Ville : 
        </td> 
        <td> 
         <asp:TextBox ID="TextBox4" Text='<%# DataBinder.Eval(Container, "DataItem.Commentaires") %>' runat="server" TabIndex="11"> 
         </asp:TextBox> 
        </td> 
       </tr> 
       <tr> 
        <td>Téléphone : 
        </td> 
        <td> 
         <telerik:RadMaskedTextBox ID="HomePhoneBox" runat="server" SelectionOnFocus="SelectAll" 
          Text='<%# DataBinder.Eval(Container, "DataItem.Telephone") %>' PromptChar="_" Mask="(###) ###-####" 
          TabIndex="3"> 
         </telerik:RadMaskedTextBox> 
        </td> 
       </tr> 
      </table> 
     </td> 
     <td style="vertical-align: top"> 
      <table id="Table1" cellspacing="1" cellpadding="1" width="250" border="0" class="module"> 
       <tr> 
        <td>Commentaires : 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <asp:TextBox ID="TextBox1" Text='<%# DataBinder.Eval(Container, "DataItem.Commentaires") %>' runat="server" TextMode="MultiLine" 
          Rows="5" Columns="40" TabIndex="5"> 
         </asp:TextBox> 
        </td> 
       </tr> 
       <tr> 
        <td>Groupe : 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <asp:TextBox ID="TextBox6" Text='<%# DataBinder.Eval(Container, "DataItem.Groupe") %>' runat="server" TextMode="MultiLine" 
          Rows="2" Columns="40" TabIndex="6"> 
         </asp:TextBox> 
        </td> 
       </tr> 

       <tr> 
        <td>Nombre de Kms : 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <asp:TextBox ID="TextBox5" Text='<%# DataBinder.Eval(Container, "DataItem.NombreDeKm") %>' runat="server" TextMode="MultiLine" 
          Rows="2" Columns="40" TabIndex="6"> 
         </asp:TextBox> 
        </td> 
       </tr> 


       <tr> 
        <td>Actif : 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <asp:RadioButtonList ID="RadioButtonList1" runat="server" TabIndex="2" RepeatDirection="Horizontal" BorderStyle="None" Width="191px"> 
            <asp:ListItem Text="Oui" Selected="True"></asp:ListItem> 
            <asp:ListItem Text="Non"></asp:ListItem> 
           </asp:RadioButtonList> 
        </td> 
       </tr> 
      </table> 
     </td> 
    </tr> 
    <tr> 
     <td colspan="2"></td> 
    </tr> 
    <tr> 
     <td></td> 
     <td></td> 
    </tr> 
    <tr> 
     <td align="right" colspan="2"> 
      <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' 
       runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'></asp:Button>&nbsp; 
            <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" 
             CommandName="Cancel"></asp:Button> 
     </td> 
    </tr> 
</table> 
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="Data Source=CDEV01;Initial Catalog=test;Integrated Security=True" SelectCommand="SELECT * FROM TypePartenaire" /> 

,並在這裏我traitement.aspx網頁代碼中,我得到我的控制被發現的領域。

newValues["RaisonSocial"] = (userControl.FindControl("TextBox9") as TextBox).Text; 
newValues["Adresse"] = (userControl.FindControl("TextBox7") as TextBox).Text; 
newValues["CodePostal"] = (userControl.FindControl("TextBox8") as TextBox).Text; 
newValues["Presription"] = (userControl.FindControl("TextBox2") as TextBox).Text; 
newValues["RaisonSocial2"] = (userControl.FindControl("TextBox10") as TextBox).Text; 
newValues["Adresse2"] = (userControl.FindControl("TextBox11") as TextBox).Text; 
newValues["Ville"] = (userControl.FindControl("TextBox4") as TextBox).Text; 
newValues["Commentaires"] = (userControl.FindControl("TextBox1") as TextBox).Text; 
newValues["NombreDeKm"] = (userControl.FindControl("TextBox5") as TextBox).Text; 
newValues["Groupe"] = (userControl.FindControl("TextBox6") as TextBox).Text; 
newValues["Telephone"] = (userControl.FindControl("HomePhoneBox") as RadMaskedTextBox).Text; 
newValues["IdTypePartenaire"] = (userControl.FindControl("RadDropDownList1") as RadDropDownList).SelectedValue; 
//Affectation des dites valeurs 
String RaisonSocial = (String)newValues["RaisonSocial"]; 
String Adresse = (String)newValues["Adresse"]; 
String Presription = (String)newValues["Presription"]; 
String RaisonSocial2 = (String)newValues["RaisonSocial2"]; 
int CodePostal = int.Parse(newValues["CodePostal"].ToString()); 
int IdTypePartenaire = int.Parse(newValues["IdTypePartenaire"].ToString()); 
String Adresse2 = (String)newValues["Adresse2"]; 
String Ville = (String)newValues["Ville"]; 
String Commentaires = (String)newValues["Commentaires"]; 
String NombreDeKm = (String)newValues["NombreDeKm"]; 
String Groupe = (String)newValues["Groupe"]; 
String Telephone = (String)newValues["Telephone"]; 

一切正常,但我希望我能提高我的代碼,並通過循環動態獲取我的用戶控制的領域,例如,這將使我行代碼。

有人可以幫助我。謝謝

回答

1

我認爲它最好在usercontrol中聲明將保存文本框控件的值的屬性。

例子:

public string NombreDeKm 
{ 
    get 
    { 
     return TextBox5.Text; 
    } 
    set 
    { 
     TextBox5.Text = value; 
    } 
} 

然後,在你這樣稱呼它

newValues["NombreDeKm"] = userControl.NombreDeKm 
+0

做了這樣的回答幫助traitement.aspx頁? – Bayeni

+0

謝謝你貝尼尼的幫助 –

+0

不客氣,請標記爲答案,如果它幫助:) – Bayeni