如何循环userControl c#

本文关键字:userControl 循环 何循环 | 更新日期: 2023-09-27 17:53:34

我是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" />

和我的特质。在这里,我得到了在我的控件中找到的字段。

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

一切都可以工作,但是我希望我可以改进我的代码,例如,通过循环动态地获取用户控件的字段,这将使我的代码行

有人可以帮助我。谢谢你

如何循环userControl c#

我认为最好在usercontrol中声明属性,以保存文本框控件的值。

的例子:

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

然后在试验中。Aspx页面,比如

  newValues["NombreDeKm"] = userControl.NombreDeKm