2010-07-04 74 views
0

我在Asp.Net中創建了一個用戶控件,我用<asp:Table id = "...." /> 填充了這個表格,列中包含下拉列表,文本框和其他Web控件...從用戶控制項目後面的代碼訪問網絡用戶控件(他們的編號)

在後面的代碼中,我無法訪問這些Web控件,有沒有一種方法可以訪問它們.......或者我不應該在用戶控件背後的代碼中這樣做? ??

這裏是我的ASCX代碼標籤:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MarriageControl.ascx.cs" %> 
<%@ Register assembly="BasicFrame.WebControls.BasicDatePicker" namespace="BasicFrame.WebControls" tagprefix="BDP" %> 

    <asp:TextBox ID = test runat = "server" /> 

    <asp:Table ID = "marriageInfoTable" runat = "server" Caption="معلومات الزواج" 
    CellPadding="2" CellSpacing="2" Width="979px" Height="164px"> 
    <asp:TableRow runat="server"> 
     <asp:TableHeaderCell runat = "server" Text = " " /> 
     <asp:TableHeaderCell runat = "server" ID = "husbandHeader" Text = "الزوج" HorizontalAlign = "Right" /> 
     <asp:TableHeaderCell runat = "server" ID = "wifeHeader" Text = "الزوجة" HorizontalAlign = "Right" /> 
    </asp:TableRow> 
    <asp:TableRow runat="server"> 
     <asp:TableCell runat = "server" Text ="الاسم الرباعي" /> 
     <asp:TableCell runat = "server"> 
      <asp:TextBox runat ="server" ID = "fullNameHusbandTextBox" Height = "30px" Width = "250px" Font-Bold = "true" Font-Size = "20px"/> 
     </asp:TableCell> 
     <asp:TableCell runat = "server"> 
      <asp:TextBox runat ="server" ID = "fullNameWifeTextBox" Height = "30px" Width = "250px" Font-Bold = "true" Font-Size = "20px"/> 
     </asp:TableCell> 
    </asp:TableRow> 
    <asp:TableRow runat="server"> 
     <asp:TableCell ID="dateOfBirthCell" runat = "server" Text ="تاريخ الميلاد" /> 
     <asp:TableCell runat = "server"> 
      <BDP:BasicDatePicker ID="dateOfBirth_husband" runat="server" DateFormat = "yyyy/MM/dd" /> 
     </asp:TableCell> 
     <asp:TableCell ID="TableCell3" runat = "server"> 
      <BDP:BasicDatePicker ID="dateOfBirth_wife" runat="server" DateFormat = "yyyy/MM/dd"/> 
     </asp:TableCell> 
    </asp:TableRow> 
    <asp:TableRow runat="server"> 
     <asp:TableCell runat = "server" Text = "مكان الإقامة" /> 
     <asp:TableCell runat = "server"> 
      <asp:DropDownList runat = "server" ID = "residenceHusbandPickList" /> 
     </asp:TableCell> 
     <asp:TableCell runat = "server"> 
      <asp:DropDownList runat = "server" ID = "residenceWifePickList" /> 
     </asp:TableCell> 
    </asp:TableRow> 
    <asp:TableRow runat="server"> 
     <asp:TableCell runat = "server" Text = "اسم و شهرة الأب والأم"/> 
     <asp:TableCell runat = "server"> 
      <asp:Table runat = "server" ID = "nameOfFatherMother_Husband"> 
       <asp:TableRow runat = "server"> 
        <asp:TableCell runat = "server"> 
        <asp:Label runat = "server" Text = "اسم الأب الرباعي" /> 
        </asp:TableCell> 
        <asp:TableCell runat = "server"> 
        <asp:TextBox runat = "server" id = "nameOfFather_Husband" Width = "200px" Font-Bold = "true" Font-Size = "20px" /> 
        </asp:TableCell> 
       </asp:TableRow> 
       <asp:TableRow runat = "server"> 
        <asp:TableCell runat = "server"> 
         <asp:Label ID="Label1" runat = "server" Text = "اسم الأم الرباعي" /> 
        </asp:TableCell> 
        <asp:TableCell runat = "server"> 
         <asp:TextBox runat = "server" id = "nameOfMother_Husband" Width = "200px" Font-Bold = "true" Font-Size = "20px" /> 
        </asp:TableCell> 
       </asp:TableRow> 
      </asp:Table> 
     </asp:TableCell> 
     <asp:TableCell runat = "server"> 
      <asp:Table runat = "server" ID = "Table1"> 
       <asp:TableRow ID="TableRow1" runat = "server"> 
        <asp:TableCell ID="TableCell1" runat = "server"> 
        <asp:Label ID="Label2" runat = "server" Text = "اسم الأب الرباعي" /> 
        </asp:TableCell> 
        <asp:TableCell ID="TableCell2" runat = "server"> 
        <asp:TextBox runat = "server" id = "nameOfFather_Wife" Width = "200px" Font-Bold = "true" Font-Size = "20px" /> 
        </asp:TableCell> 
       </asp:TableRow> 
       <asp:TableRow ID="TableRow2" runat = "server"> 
        <asp:TableCell ID="TableCell4" runat = "server"> 
         <asp:Label ID="Label3" runat = "server" Text = "اسم الأم الرباعي" /> 
        </asp:TableCell> 
        <asp:TableCell ID="TableCell5" runat = "server"> 
         <asp:TextBox runat = "server" id = "nameOfMother_Wife" Width = "200px" Font-Bold = "true" Font-Size = "20px" /> 
        </asp:TableCell> 
       </asp:TableRow> 
      </asp:Table>     
     </asp:TableCell> 
    </asp:TableRow> 
    <asp:TableRow runat="server"> 
     <asp:TableCell runat = "server"> 
      <asp:Label runat = "server" Text = "مكان إقامة الأب والأم"></asp:Label> 
     </asp:TableCell> 
     <asp:TableCell runat = "server"> 
      <asp:Table runat = "server"> 
       <asp:TableRow runat = "server"> 
        <asp:TableCell runat = "server"> 
         <asp:Label runat="server" Text = "مكان سكن الأب" ></asp:Label> 
        </asp:TableCell> 
        <asp:TableCell runat = "server"> 
         <asp:DropDownList ID = "residenceOfFather_Husband" runat = "server"></asp:DropDownList> 
        </asp:TableCell> 
       </asp:TableRow> 
       <asp:TableRow runat = "server"> 
        <asp:TableCell ID="TableCell6" runat = "server"> 
         <asp:Label ID="Label4" runat="server" Text = "مكان سكن الأم" ></asp:Label> 
        </asp:TableCell> 
        <asp:TableCell ID="TableCell7" runat = "server"> 
         <asp:DropDownList ID = "residenceOfMother_Husband" runat = "server"></asp:DropDownList> 
        </asp:TableCell> 
       </asp:TableRow>    

      </asp:Table>     
     </asp:TableCell> 
     <asp:TableCell ID="TableCell8" runat = "server"> 
      <asp:Table ID="Table2" runat = "server"> 
       <asp:TableRow ID="TableRow3" runat = "server"> 
        <asp:TableCell ID="TableCell9" runat = "server"> 
         <asp:Label ID="Label5" runat="server" Text = "مكان سكن الأب" ></asp:Label> 
        </asp:TableCell> 
        <asp:TableCell ID="TableCell10" runat = "server"> 
         <asp:DropDownList ID = "residenceOfFather_Wife" runat = "server"></asp:DropDownList> 
        </asp:TableCell> 
       </asp:TableRow> 
       <asp:TableRow ID="TableRow4" runat = "server"> 
        <asp:TableCell ID="TableCell11" runat = "server"> 
         <asp:Label ID="Label6" runat="server" Text = "مكان سكن الأم" ></asp:Label> 
        </asp:TableCell> 
        <asp:TableCell ID="TableCell12" runat = "server"> 
         <asp:DropDownList ID = "residenceOfMother_Wife" runat = "server"></asp:DropDownList> 
        </asp:TableCell> 
       </asp:TableRow>    

      </asp:Table>     
     </asp:TableCell>     
    </asp:TableRow> 
    <asp:TableRow runat="server"> 
     <asp:TableCell runat = "server"> 
      <asp:Label runat = "server" ID = "nameOfWitnesses" Text = "أسماء الشهود" /> 
     </asp:TableCell> 
     <asp:TableCell ID="tblCellWitnesses_Husband" runat = "server"> 
      <asp:Table runat = "server"> 
       <asp:TableRow runat = "server"> 
        <asp:TableCell runat = "server"> 
         <asp:TextBox ID = "witnessName" runat = "server" Width = "250px" /> 
         <asp:Button ID = "addWitness" runat = "server" Text = "أضف شاهد" /> 
        </asp:TableCell> 
       </asp:TableRow>       
      </asp:Table> 
     </asp:TableCell> 
     <asp:TableCell ID="tblCellWitnesses_Wife" runat = "server"> 
      <asp:TextBox ID = "TextBox1" runat = "server" Width = "250px" /> 
      <asp:Button ID = "addWitness2" runat = "server" Text = "أضف شاهد" /> 
     </asp:TableCell> 
    </asp:TableRow>    
</asp:Table> 
在背後說我的代碼

(ascx.cs)我試圖用數據填充下拉...我只是不能 類型的ID我擁有並使用它的任何東西。

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     //appropriate connection start 
     dataBaseConnection = new SqlConnection(); 
     dataBaseConnection.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\ChurchApp\ChurchApplication\App_Data\Database.mdf;Integrated Security=True;User Instance=True"; 
     dataAdapter = new SqlDataAdapter("SELECT * FROM Country"); 
     ds = new DataSet(); 
     dataAdapter.Fill(ds, "CountryTable"); 

    } 
+1

你能後的ASPX標記和你正在嘗試在後面的代碼呢? – Oded 2010-07-04 20:29:21

回答

0

我已經找出了通過檢查每一行的問題。

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

的問題是在這條線,我錯過了繼承屬性

1

使用一個asp:ObjectDataSource控件的頁面上做到這一點,並在每一個asp:DropDownList的你會設置DataSourceID屬性

編輯: 其實你的情況一個SqlDataSource是適當的。添加到您的.aspx標記:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=E:\ChurchApp\ChurchApplication\App_Data\Database.mdf;Integrated Security=True;User Instance=True" 
SelectCommand="SELECT * FROM Country" 
EnableCaching="true" CacheDuration="Infinite"/> 

,然後要包含國家列表中添加了DataSourceID = 「SqlDataSource1」

例如dropdownlists:

<asp:DropDownList ID = "residenceOfMother_Wife" runat = "server" DataSourceID="SqlDataSource1"></asp:DropDownList> 
+0

能否請你解釋一下,如何用一個例子來實現...我對C#很陌生。 謝謝 – 2010-07-06 07:38:46

0

你可以試試使用遞歸實現this.FindControl(「ID」)

+0

我試過了,但沒有奏效....你有什麼想法,爲什麼發生這種情況? (不能直接使用id?)是一個錯誤?或用戶控制有一些特殊的規則,以便您不能直接在其中使用Web控件? – 2010-07-06 07:38:14