代码之家  ›  专栏  ›  技术社区  ›  Saher Ahwal

从用户控件项后面的代码访问Web用户控件(其ID)

  •  0
  • Saher Ahwal  · 技术社区  · 14 年前

    我在ASP.NET中创建了一个用户控件,用它填充 <asp:Table id = "...." /> 此表中的列包含DropDownList、TextFields和其他Web控件…

    在代码隐藏中,我无法访问这些Web控件,是否有一种方法可以访问它们……。或者我不应该在用户控件的代码隐藏中这样做????

    这是我的 户控件 代码标记:

    <%@ 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");
    
        }
    
    3 回复  |  直到 14 年前
        1
  •  1
  •   matt-dot-net    14 年前

    使用页上的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”

    例子:

    <asp:DropDownList ID = "residenceOfMother_Wife" runat = "server" DataSourceID="SqlDataSource1"></asp:DropDownList> 
    
        2
  •  0
  •   citronas    14 年前

    您可以尝试使用此.findcontrol(“id”)的递归实现

        3
  •  0
  •   Saher Ahwal    14 年前

    我通过检查每一行来解决这个问题。

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

    问题出在这行,我缺少inherits属性