Finding a PlaceHolder inside of a Repeater

B

Brad Baker

I am trying to programmatically set a placeholder control in csharp which is
nested inside a repeater control between <ItemTemplate> and </ItemTemplate>
tags, however I am running into problems. I've tried several different
approaches for finding the placeholder:

This:
MyControl = this.FindControl("$form$tabs_repeater$PlaceHolder1");

Produces this error:
Object reference not set to an instance of an object.


This:
MyControl =
(PlaceHolder)tabs_repeater.Items[intSiteNum].FindControl("PlaceHolder1");

Produces this error:
Index was out of range. Must be non-negative and less than the size of the
collection. Parameter name: index


Below is an abbreviated excerpt of my code. Can anyone suggest what I might
be doing wrong?

Thanks,
Brad


protected string GenerateNavBarLink(string currentdepartmentid, int SiteNum)
{
int intSiteNum = SiteNum;
string strdepartmentid = currentdepartmentid;

LinkButton btn = new LinkButton();
btn.ID = "LinkButton1";
btn.Command += new_department_button_Click;
btn.Text = "Add Department +";

Control MyControl = new Control();

// Two different ways of updating the place holder
//MyControl =
this.FindControl("form$tabs_repeater$PlaceHolder1");
//MyControl =
(PlaceHolder)tabs_repeater.Items[intSiteNum].FindControl("PlaceHolder1");

MyControl.Controls.Add(new LiteralControl("<li>"));
MyControl.Controls.Add(btn);
MyControl.Controls.Add(new LiteralControl("</li></ul>"));

}

<ASP:Repeater id="tabs_repeater" DataSourceID="repeater_datasource"
runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<li><%#
GenerateNavBarLink((Eval("department_id").ToString()),Container.ItemIndex)
%></li>
<asp:placeHolder ID="PlaceHolder1"
runat="server"></asp:placeHolder>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</ASP:Repeater>
 
G

Guest

I am trying to programmatically set a placeholder control in csharp which is
nested inside a repeater control between <ItemTemplate> and </ItemTemplate>
tags, however I am running into problems. I've tried several different
approaches for finding the placeholder:

Hi Brad

I would recommend to use the OnItemDataBound event

Change the repeater

.... OnItemDataBound="GenerateNavBarLink">

remove from the ItemTemplate

<li><%#
GenerateNavBarLink((Eval("department_id").ToString()),Container.ItemIndex)
%></li>

and write new event handler

protected void GenerateNavBarLink(object sender, RepeaterItemEventArgs
e)
{

.... your code here

if( e.Item.ItemType == ListItemType.Item)
MyControl =
((PlaceHolder)e.Item.FindControl("PlaceHolder1")).Controls.Add(btn);

MyControl.Controls.Add(new LiteralControl("<li>"));
MyControl.Controls.Add(btn);
MyControl.Controls.Add(new LiteralControl("</li></ul>"));

}
 
G

Guest

I think that you need to consider BulletedList control - it could list
LinkButton controls as its list items
 
B

bruce barker

you are trying to access the placeholder before its been added to the
repeater. use the itemdatabound event instead.

-- bruce (sqlwork.com)
 
S

Steven Cheng[MSFT]

Hi Brad,

I agree with Alexey. However,if you want to add dynamic controls into each
repeaterItem row, you can use the "ItemCreated" event since you can
directly get the current RepeaterItem that is being created. You can locate
existing control in each repeater item or add new controls into it at that
time:

===========
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs
e)
{
PlaceHolder ph = e.Item.FindControl("holder1") as PlaceHolder;
//...
}
==========

Will this help for your case?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Brad Baker

Thank you for the overwhelming response! :) It's a bit complicated to
explain but I have to use <ul> and <li> - it's a requirement of another app
I am integrating with. I can't do anything about that.

I abbreviated my example below for simplicity sake but the function I am
calling actually does more. I think I have managed to adjust most of my
code but I am having some difficulty with:

int intSiteNum = e.Item.ItemIndex; //this seems to work
DbDataRecord objRow = (DbDataRecord)e.Item.DataItem; // so does this
string strdepartmentid = objRow["department_id"].ToString(); // this craps
out

I am getting: "Object reference not set to an instance of an object" on the
third line above.

I haven't used the DbDataRecord datatype before so maybe I am using it
incorrectly. Basically as the repeater iterates I need to grab the value of
department_id and run some conditional logic on it which dictates formatting
on the output.



bruce barker said:
you are trying to access the placeholder before its been added to the
repeater. use the itemdatabound event instead.

-- bruce (sqlwork.com)


Brad said:
I am trying to programmatically set a placeholder control in csharp which
is nested inside a repeater control between <ItemTemplate> and
</ItemTemplate> tags, however I am running into problems. I've tried
several different approaches for finding the placeholder:

This:
MyControl = this.FindControl("$form$tabs_repeater$PlaceHolder1");

Produces this error:
Object reference not set to an instance of an object.


This:
MyControl =
(PlaceHolder)tabs_repeater.Items[intSiteNum].FindControl("PlaceHolder1");

Produces this error:
Index was out of range. Must be non-negative and less than the size of
the collection. Parameter name: index


Below is an abbreviated excerpt of my code. Can anyone suggest what I
might be doing wrong?

Thanks,
Brad


protected string GenerateNavBarLink(string currentdepartmentid, int
SiteNum)
{
int intSiteNum = SiteNum;
string strdepartmentid = currentdepartmentid;

LinkButton btn = new LinkButton();
btn.ID = "LinkButton1";
btn.Command += new_department_button_Click;
btn.Text = "Add Department +";

Control MyControl = new Control();

// Two different ways of updating the place holder
//MyControl =
this.FindControl("form$tabs_repeater$PlaceHolder1");
//MyControl =
(PlaceHolder)tabs_repeater.Items[intSiteNum].FindControl("PlaceHolder1");

MyControl.Controls.Add(new LiteralControl("<li>"));
MyControl.Controls.Add(btn);
MyControl.Controls.Add(new LiteralControl("</li></ul>"));

}

<ASP:Repeater id="tabs_repeater" DataSourceID="repeater_datasource"
runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<li><%#
GenerateNavBarLink((Eval("department_id").ToString()),Container.ItemIndex)
%></li>
<asp:placeHolder ID="PlaceHolder1"
runat="server"></asp:placeHolder>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</ASP:Repeater>
 
S

Steven Cheng[MSFT]

Hi Brad,

As for the following error you encountered:

========
I am getting: "Object reference not set to an instance of an object" on the
third line above.
========

I think you can check the object's type and local variable to see whether
they're of the expected type and casted to the correct instance? If
possible you can set breakpoint in the code and check the value in
debugger.

Also, when you accessing the dataitem from e.Item.DataItem(in template
databound control), you need to make sure what's the datasource you use to
bind. If you're using Dataset/DataView, the "dataItem" is instance of
"DataRowView", if datasource is "Datareader", the dataitem instance will be
of "IDataRecord" type.

Please feel free to post here if you have any other finding or question on
this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Brad Baker

I ended up using the following code (in case this helps someone else out in
the future)

// Here we are getting the department id
DataRowView objRow = (DataRowView)e.Item.DataItem;
string strdepartmentid = Convert.ToString(DataBinder.Eval(objRow,
"department_id"));

Thanks for all the help. :)
Brad
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top