我在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");
}
答案 0 :(得分: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”
示例:
<asp:DropDownList ID = "residenceOfMother_Wife" runat = "server" DataSourceID="SqlDataSource1"></asp:DropDownList>
答案 1 :(得分:0)
您可以尝试使用this.FindControl(“ID”)
的递归实现答案 2 :(得分:0)
我通过检查每一行来找出问题所在。
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MarriageControl.ascx.cs" %>
问题出在这一行我缺少继承属性