none
用户控件引用外部控件的问题 RRS feed

  • 问题

  • 我有一个用户控件 搜索栏
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="SearchControl.ascx.cs" Inherits="SearchControl" %>
    <asp:Label ID="Label1" runat="server" Text="请输入图书的ISBN:"></asp:Label>
    <asp:TextBox ID="txtRearchContent" runat="server"></asp:TextBox>
    <asp:Button ID="btnSearch" runat="server" Text="搜 索" OnClick="btnSearch_Click" />
    方法如下
    public partial class SearchControl : System.Web.UI.UserControl
    {
        private string id = "";
        public string Id
        {
            set { id = value; }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string isbn=txtRearchContent.Text;
            foreach (Control ctl in Page.Controls)
            {
                if (ctl is System.Web.UI.WebControls.DataList&&ctl.ID==id)
                {
                    ((DataList)ctl).DataSource = BookManager.GetBookByISBN(isbn);
                    ((DataList)ctl).DataBind();
                }
            }
        }
    }
    我在页面上面有一个DataList1用于展示信息,上面的用户控件(搜索栏)点击确定后把搜索结果显示到DataList1里面,可是运行后发现
    DataList1的内容没有修改,断点后发现foreach里面Page.Controls.count=5,this.Controls.count=6,而页面上的控件的数量绝对是>10的,就是说,这里的遍历只遍历了用户控件里面的内容,如何才能遍历到外面的DataList1,进行数据的更新呢

    2009年11月27日 10:05

答案

  •   protected void Button1_Click(object sender, EventArgs e)
            {
               DataList s= this.Parent.FindControl("Datalist1")as DataList;
               if (s != null)
               {
                   Response.Write("成功");
               }
            }

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="WebApplication1.WebUserControl2" %>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    努力学习wpf
    • 已标记为答案 ww-air 2009年11月28日 16:01
    2009年11月27日 10:50
  • 你好,建议在用户控件中添加一个属性(也可以是控件ID,通过 FindControl 来找),在控件代码中直接使用这个属性来操作 datalist

    private DataList innerDataList;
    
    public DataList InnerDataList
    {
        get { return innerDataList; }
        set { innerDataList = value; }
    }

    页面代码中设置用户控件的这个属性。

    this.WebUserControl1.InnerDataList = this.DataList1;


    知识改变命运,奋斗成就人生!
    • 已标记为答案 ww-air 2009年11月28日 16:01
    2009年11月28日 2:12
    版主
  • Default.aspx页面的代码:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <%@ Register src="UserControls/GetSearch.ascx" tagname="GetSearch" tagprefix="uc1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>DataList测试</title>
        <style type="text/css">
            .trContent
            {
                background-color: #FFFFFF;
                height: 20px;
                color: #344b50;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <br />
        <uc1:GetSearch ID="GetSearch1" runat="server" ParentDataListId="dlList"/>
        <br />
        <br />
        <asp:DataList ID="dlList" runat="server" Width="300px" BackColor="#D3EAEF" CellPadding="0"
            CellSpacing="1" ShowFooter="False" ShowHeader="False" RepeatColumns="3">
            <ItemStyle HorizontalAlign="Center" CssClass="trContent" />
            <ItemTemplate>
                <%# Container.DataItem %>
            </ItemTemplate>
        </asp:DataList>
        </form>
    </body>
    </html>
    Default.aspx.cs 代码

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindMyData();
            }
        }
    
        protected void BindMyData()
        {
            this.dlList.DataSource = CommonClass.GetAllPlayers();
            this.dlList.DataBind();
        }
    }
    App_Code 文件夹中的CommonClass.cs类

    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Collections.Generic;
    
    /// <summary>
    ///CommonClass 的摘要说明
    /// </summary>
    public class CommonClass
    {
        public CommonClass()
        {
            
        }
    
        public static List<string> GetAllPlayers()
        {
            return new List<string>() { "张三", "李四", "王武", "周华健", "周杰伦", "梁朝伟", "吴孟达", "周星驰", "李亚鹏" };
        }
    
        public static List<string> GetPlayersByFirstName(string prefixText)
        {
            var list = GetAllPlayers();
            var query = from m in list where m.Substring(0, 1) == prefixText select m;
            return query.ToList();
        }
    }
    用户控件中的代码:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="GetSearch.ascx.cs" Inherits="UserControls_GetSearch" %>
    请输入演员的姓氏:<asp:TextBox ID="txtSearchFirstName" runat="server"></asp:TextBox>
    <asp:Button ID="btnSearch" runat="server" Text="搜索" OnClick="btnSearch_Click" />
    用户控件后台代码:

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    public partial class UserControls_GetSearch : System.Web.UI.UserControl
    {
        private string parentDataListId;
    
        public string ParentDataListId
        {
            set
            {
                parentDataListId = value;
            }
            get
            {
                return parentDataListId;
            }
        }
    
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string firstName = this.txtSearchFirstName.Text.Trim();
            DataList dlList = this.Parent.FindControl(ParentDataListId) as DataList;
            dlList.DataSource = CommonClass.GetPlayersByFirstName(firstName);
            dlList.DataBind();
        }
    }

    努力!奋斗
    • 已标记为答案 ww-air 2009年11月28日 16:01
    2009年11月28日 8:34

全部回复

  •   protected void Button1_Click(object sender, EventArgs e)
            {
               DataList s= this.Parent.FindControl("Datalist1")as DataList;
               if (s != null)
               {
                   Response.Write("成功");
               }
            }

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="WebApplication1.WebUserControl2" %>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    努力学习wpf
    • 已标记为答案 ww-air 2009年11月28日 16:01
    2009年11月27日 10:50
  • 你好,建议在用户控件中添加一个属性(也可以是控件ID,通过 FindControl 来找),在控件代码中直接使用这个属性来操作 datalist

    private DataList innerDataList;
    
    public DataList InnerDataList
    {
        get { return innerDataList; }
        set { innerDataList = value; }
    }

    页面代码中设置用户控件的这个属性。

    this.WebUserControl1.InnerDataList = this.DataList1;


    知识改变命运,奋斗成就人生!
    • 已标记为答案 ww-air 2009年11月28日 16:01
    2009年11月28日 2:12
    版主
  • Default.aspx页面的代码:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <%@ Register src="UserControls/GetSearch.ascx" tagname="GetSearch" tagprefix="uc1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>DataList测试</title>
        <style type="text/css">
            .trContent
            {
                background-color: #FFFFFF;
                height: 20px;
                color: #344b50;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <br />
        <uc1:GetSearch ID="GetSearch1" runat="server" ParentDataListId="dlList"/>
        <br />
        <br />
        <asp:DataList ID="dlList" runat="server" Width="300px" BackColor="#D3EAEF" CellPadding="0"
            CellSpacing="1" ShowFooter="False" ShowHeader="False" RepeatColumns="3">
            <ItemStyle HorizontalAlign="Center" CssClass="trContent" />
            <ItemTemplate>
                <%# Container.DataItem %>
            </ItemTemplate>
        </asp:DataList>
        </form>
    </body>
    </html>
    Default.aspx.cs 代码

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindMyData();
            }
        }
    
        protected void BindMyData()
        {
            this.dlList.DataSource = CommonClass.GetAllPlayers();
            this.dlList.DataBind();
        }
    }
    App_Code 文件夹中的CommonClass.cs类

    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Collections.Generic;
    
    /// <summary>
    ///CommonClass 的摘要说明
    /// </summary>
    public class CommonClass
    {
        public CommonClass()
        {
            
        }
    
        public static List<string> GetAllPlayers()
        {
            return new List<string>() { "张三", "李四", "王武", "周华健", "周杰伦", "梁朝伟", "吴孟达", "周星驰", "李亚鹏" };
        }
    
        public static List<string> GetPlayersByFirstName(string prefixText)
        {
            var list = GetAllPlayers();
            var query = from m in list where m.Substring(0, 1) == prefixText select m;
            return query.ToList();
        }
    }
    用户控件中的代码:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="GetSearch.ascx.cs" Inherits="UserControls_GetSearch" %>
    请输入演员的姓氏:<asp:TextBox ID="txtSearchFirstName" runat="server"></asp:TextBox>
    <asp:Button ID="btnSearch" runat="server" Text="搜索" OnClick="btnSearch_Click" />
    用户控件后台代码:

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    public partial class UserControls_GetSearch : System.Web.UI.UserControl
    {
        private string parentDataListId;
    
        public string ParentDataListId
        {
            set
            {
                parentDataListId = value;
            }
            get
            {
                return parentDataListId;
            }
        }
    
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string firstName = this.txtSearchFirstName.Text.Trim();
            DataList dlList = this.Parent.FindControl(ParentDataListId) as DataList;
            dlList.DataSource = CommonClass.GetPlayersByFirstName(firstName);
            dlList.DataBind();
        }
    }

    努力!奋斗
    • 已标记为答案 ww-air 2009年11月28日 16:01
    2009年11月28日 8:34