Getting a subcontrol to work in a repeater

J

James Geurts

Hi all

I have a component that contains the following code

<asp:repeater id="MyRepeater" Runat="server"><ItemTemplate><asp:HyperLink Text='<%# DataBinder.Eval(Container.DataItem, "Name")%>' NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "Path")%>' runat="server"/><MyTag:MyControl Name='<%# DataBinder.Eval(Container.DataItem, "Name")%>' Title='<%# DataBinder.Eval(Container.DataItem, "Title")%>' Path='<%# DataBinder.Eval(Container.DataItem, "Path")%>' runat="server"></MyTag:MyControl></ItemTemplate><SeparatorTemplate><br /></SeparatorTemplate></asp:Repeater

Now the problem that I'm having is that the hyperlink will show up properly, with Text and NavigateUrl properties containing the expected values. The other tag, a custom tag that I use for another custom control, does not contain values for the properties specified.

Now, if I do manually specify property values outside of the repeater, it works as expected:
<MyTag:MyControl Name="TestName" Title="TestTitle Path="TestPath" runat="server"></MyTag:MyControl

So, I'm curious why it is not populating the properties correctly from the repeater control. If I have not supplied enough information and you would like to see actual code, I can do that if it would help

Thank

Jim
 
J

Jeffrey Tan[MSFT]

Hi Jim,

Thank you for posting in the community!

Based on my understanding, you place your webcontrol in Repeater control.
Then use databinding to populate the data from database for these
properties.
But the webcontrol's properties do not populate well.

=====================================================
Actually, it should work, I have writen a sample project which works
well.(I use the SqlServer's default "jobs" table in "pubs" database)

<asp:repeater id="MyRepeater" Runat="server" DataSource="<%# dataSet11 %>">
<ItemTemplate>
<asp:HyperLink Text='<%# DataBinder.Eval(Container.DataItem,
"job_desc")%>' NavigateUrl='<%# DataBinder.Eval(Container.DataItem,
"job_id")%>' runat="server" ID="Hyperlink1"/>
<cc1:WebCustomControl1 id="WebCustomControl11" Text='<%#
DataBinder.Eval(Container.DataItem, "job_desc")%>' Title='<%#
DataBinder.Eval(Container.DataItem, "job_desc")%>' Path='<%#
DataBinder.Eval(Container.DataItem, "job_id")%>' runat="server">
</cc1:WebCustomControl1></P>
</ItemTemplate>
<SeparatorTemplate>
<br />
</SeparatorTemplate>
</asp:repeater>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>

At code behind,
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
sqlDataAdapter1.Fill(dataSet11);
MyRepeater.DataBind();
}
}

private void Button1_Click(object sender, System.EventArgs e)
{
foreach(RepeaterItem r in MyRepeater.Items)
{
foreach(Control c in r.Controls)
{
if(c is WebControlLibrary1.WebCustomControl1)
{
this.Response.Write(((WebControlLibrary1.WebCustomControl1)c).Path);
}
}
}
}

The webcontrol's code like this:

public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
public string Text
{
get
{
return (string)this.ViewState["Text"];
}

set
{
this.ViewState["Text"] = value;
}
}

public string Title
{
get
{
return (string)this.ViewState["Title"];
}

set
{
this.ViewState["Title"] = value;
}
}

public string Path
{
get
{
return (string)this.ViewState["Path"];
}

set
{
this.ViewState["Path"] = value;
}
}

protected override void Render(HtmlTextWriter output)
{
output.Write(Text);
}
}

Note, for the webcontrol's customized property, you should store it in the
ViewState, then when render out, the properties will be stored between
every postback.

===========================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

James Geurts

Thanks for the reply Jeffrey

After hunting around the code for a while, I found that the Control.Load event was not being handled. So, I added it back to the InitializeComponent method and all works as desired

Jim
 
J

Jeffrey Tan[MSFT]

Hi Jim,

Thanks for your feedback.

Oh, I am glad you find out the issue yourself, if you have any further
concern, please feel free to post :)

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top