pallosp
asked on
Binding a web user control in another ContentPlaceHolder
I have a master page with multiple ContentPlaceHolders. The first one contains a web user control of class Controls_CategorySelector. The second one contains a data source, and one of its parameters is bound to the SelectedCategory property of the CategorySelector. How can this binding be done without compilation error?
<asp:Content ID="Content1" ContentPlaceHolderID="Left MenuHolder " runat="Server">
<uc1:CategorySelector ID="catSelector" runat="server" OnLoad="CategorySelectorLo ad"/>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Cont entHolder" runat="Server">
<asp:SqlDataSource ID="ArticleDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:RTFMConn ectionStri ng %>"
SelectCommand="listRecentA rticles" SelectCommandType="StoredP rocedure">
<SelectParameters>
<asp:ControlParameter ControlID="catSelector" DefaultValue="0" Name="categoryId"
PropertyName="SelectedCate gory" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Content>
The ControlID "catSelector" is only found by the compiler if the data source and the CategorySelector are in the same ContentPlaceHolder.
<asp:Content ID="Content1" ContentPlaceHolderID="Left
<uc1:CategorySelector ID="catSelector" runat="server" OnLoad="CategorySelectorLo
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Cont
<asp:SqlDataSource ID="ArticleDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:RTFMConn
SelectCommand="listRecentA
<SelectParameters>
<asp:ControlParameter ControlID="catSelector" DefaultValue="0" Name="categoryId"
PropertyName="SelectedCate
</SelectParameters>
</asp:SqlDataSource>
</asp:Content>
The ControlID "catSelector" is only found by the compiler if the data source and the CategorySelector are in the same ContentPlaceHolder.
ASKER
It is a RadioButtonList populated from code. The category ID belonging to the SelectedIndex property is led out as SelectedCategory.
I was trying to figure out what uc1:CategorySelector was, and it doesn't sound like a RadioButtonList. Is it a third-party control, or a home-grown custom web control?
Bob
Bob
ASKER
Here is a simple code without web user controls that produce the same phenomenon:
masterpage.master:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.maste r.cs" Inherits="MasterPage" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"></asp:Conte ntPlaceHol der>
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server"></asp:Conte ntPlaceHol der>
</div>
</form>
</body>
</html>
default.aspx:
<%@ Page Language="C#" MasterPageFile="~/MasterPa ge.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Cont entPlaceHo lder1" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Cont entPlaceHo lder2" runat="Server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextB ox>
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSo urce1">
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetItems"
TypeName="Class1">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="prefix" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
</asp:Content>
app_code/class1.cs:
public class Class1
{
public static string[] GetItems(string prefix) {
return new string[] { prefix+"1", prefix+"2" };
}
}
After TextBox1 had been moved to the first ContentPlaceHolder, the code didn't compile.
masterpage.master:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.maste
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"></asp:Conte
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server"></asp:Conte
</div>
</form>
</body>
</html>
default.aspx:
<%@ Page Language="C#" MasterPageFile="~/MasterPa
CodeFile="Default.aspx.cs"
<asp:Content ID="Content1" ContentPlaceHolderID="Cont
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Cont
<asp:TextBox ID="TextBox1" runat="server"></asp:TextB
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSo
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetItems"
TypeName="Class1">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="prefix" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
</asp:Content>
app_code/class1.cs:
public class Class1
{
public static string[] GetItems(string prefix) {
return new string[] { prefix+"1", prefix+"2" };
}
}
After TextBox1 had been moved to the first ContentPlaceHolder, the code didn't compile.
It compiled for me with TextBox1 under Context1. I created a Web Site model type web site, and I don't know which type you created. But, I can't see where that would cause a problem.
Bob
Bob
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Big thanks!
Bob