从用户控件项的代码隐藏访问 Web 用户控件(它们的 ID)

发布于 2024-09-08 00:48:43 字数 10030 浏览 6 评论 0原文

我在 Asp.Net 中创建了一个用户控件,并用 填充该控件 该表包含包含下拉列表、文本字段和其他 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");

    }

I created a user control in Asp.Net which I populate with <asp:Table id = "...." />
this table has columns that contain dropdownlists, textfields and other web controls...

In the code behind I am not able to access these web controls, is there a way I can access them.......or I shouldn't be doing that in the code behind of the user control???

here is my ascx code markup:

<%@ 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>

in my code behind (ascx.cs) i am trying to fill the dropdown with data ... I just can't
type the id of anything I have and use it.

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

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

百思不得你姐 2024-09-15 00:48:43

在页面上使用 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> 

use an asp:ObjectDataSource on the page to do this, and on each asp:DropDownList you will set the DataSourceID property

EDIT:
Actually in your case a SqlDataSource would be appropriate. Add this to your .aspx markup:

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

and then on the dropdownlists that you want to contain the country list add DataSourceID="SqlDataSource1"

example:

<asp:DropDownList ID = "residenceOfMother_Wife" runat = "server" DataSourceID="SqlDataSource1"></asp:DropDownList> 
暮年慕年 2024-09-15 00:48:43

您可以尝试使用 this.FindControl("ID") 的递归实现

You can try using a recursive implementation of this.FindControl("ID")

没有你我更好 2024-09-15 00:48:43

我通过检查每一行找出了问题所在。

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

问题是在这一行中我缺少继承属性

I have figured out the problem by checking every single line.

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

The problem is in this line I was missing the inherits attribute

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文