By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,270 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,270 IT Pros & Developers. It's quick & easy.

Finding a PlaceHolder inside of a Repeater

P: n/a
I am trying to programmatically set a placeholder control in csharp which is
nested inside a repeater control between <ItemTemplateand </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>
May 24 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On May 24, 10:30 pm, "Brad Baker" <b...@nospam.nospamwrote:
I am trying to programmatically set a placeholder control in csharp which is
nested inside a repeater control between <ItemTemplateand </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>"));

}
May 24 '07 #2

P: n/a
I think that you need to consider BulletedList control - it could list
LinkButton controls as its list items

"Brad Baker" wrote:
I am trying to programmatically set a placeholder control in csharp which is
nested inside a repeater control between <ItemTemplateand </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>
May 24 '07 #3

P: n/a
you are trying to access the placeholder before its been added to the
repeater. use the itemdatabound event instead.

-- bruce (sqlwork.com)
Brad Baker wrote:
I am trying to programmatically set a placeholder control in csharp which is
nested inside a repeater control between <ItemTemplateand </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>

May 25 '07 #4

P: n/a
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.

May 25 '07 #5

P: n/a
Thank you for the overwhelming response! :) It's a bit complicated to
explain but I have to use <uland <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" <no****@nospam.comwrote in message
news:uz**************@TK2MSFTNGP04.phx.gbl...
you are trying to access the placeholder before its been added to the
repeater. use the itemdatabound event instead.

-- bruce (sqlwork.com)
Brad Baker wrote:
>I am trying to programmatically set a placeholder control in csharp which
is nested inside a repeater control between <ItemTemplateand
</ItemTemplatetags, 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").ToStrin g()),Container.ItemIndex)
%></li>
<asp:PlaceHolder ID="PlaceHolder1"
runat="server"></asp:PlaceHolder>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</ASP:Repeater>

May 25 '07 #6

P: n/a
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.

May 28 '07 #7

P: n/a
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
"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:IQ**************@TK2MSFTNGHUB02.phx.gbl...
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.

May 28 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.