473,545 Members | 529 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding a PlaceHolder inside of a Repeater

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

This:
MyControl = this.FindContro l("$form$tabs_r epeater$PlaceHo lder1");

Produces this error:
Object reference not set to an instance of an object.
This:
MyControl =
(PlaceHolder)ta bs_repeater.Ite ms[intSiteNum].FindControl("P laceHolder1");

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 GenerateNavBarL ink(string currentdepartme ntid, int SiteNum)
{
int intSiteNum = SiteNum;
string strdepartmentid = currentdepartme ntid;

LinkButton btn = new LinkButton();
btn.ID = "LinkButton 1";
btn.Command += new_department_ button_Click;
btn.Text = "Add Department +";

Control MyControl = new Control();

// Two different ways of updating the place holder
//MyControl =
this.FindContro l("form$tabs_re peater$PlaceHol der1");
//MyControl =
(PlaceHolder)ta bs_repeater.Ite ms[intSiteNum].FindControl("P laceHolder1");

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

}

<ASP:Repeater id="tabs_repeat er" DataSourceID="r epeater_datasou rce"
runat="server">
<HeaderTemplate >
</HeaderTemplate>
<ItemTemplate >
<li><%#
GenerateNavBarL ink((Eval("depa rtment_id").ToS tring()),Contai ner.ItemIndex)
%></li>
<asp:PlaceHolde r ID="PlaceHolder 1"
runat="server"> </asp:PlaceHolder >
</ItemTemplate>
<FooterTemplate >
</FooterTemplate>
</ASP:Repeater>
May 24 '07 #1
7 9069
On May 24, 10:30 pm, "Brad Baker" <b...@nospam.no spamwrote:
I am trying to programmaticall y set a placeholder control in csharp which is
nested inside a repeater control between <ItemTemplatean d </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 ="GenerateNavBa rLink">

remove from the ItemTemplate

<li><%#
GenerateNavBarL ink((Eval("depa rtment_id").ToS tring()),Contai ner.ItemIndex)
%></li>

and write new event handler

protected void GenerateNavBarL ink(object sender, RepeaterItemEve ntArgs
e)
{

.... your code here

if( e.Item.ItemType == ListItemType.It em)
MyControl =
((PlaceHolder)e .Item.FindContr ol("PlaceHolder 1")).Controls.A dd(btn);

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

}
May 24 '07 #2
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 programmaticall y set a placeholder control in csharp which is
nested inside a repeater control between <ItemTemplatean d </ItemTemplate>
tags, however I am running into problems. I've tried several different
approaches for finding the placeholder:

This:
MyControl = this.FindContro l("$form$tabs_r epeater$PlaceHo lder1");

Produces this error:
Object reference not set to an instance of an object.
This:
MyControl =
(PlaceHolder)ta bs_repeater.Ite ms[intSiteNum].FindControl("P laceHolder1");

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 GenerateNavBarL ink(string currentdepartme ntid, int SiteNum)
{
int intSiteNum = SiteNum;
string strdepartmentid = currentdepartme ntid;

LinkButton btn = new LinkButton();
btn.ID = "LinkButton 1";
btn.Command += new_department_ button_Click;
btn.Text = "Add Department +";

Control MyControl = new Control();

// Two different ways of updating the place holder
//MyControl =
this.FindContro l("form$tabs_re peater$PlaceHol der1");
//MyControl =
(PlaceHolder)ta bs_repeater.Ite ms[intSiteNum].FindControl("P laceHolder1");

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

}

<ASP:Repeater id="tabs_repeat er" DataSourceID="r epeater_datasou rce"
runat="server">
<HeaderTemplate >
</HeaderTemplate>
<ItemTemplate >
<li><%#
GenerateNavBarL ink((Eval("depa rtment_id").ToS tring()),Contai ner.ItemIndex)
%></li>
<asp:PlaceHolde r ID="PlaceHolder 1"
runat="server"> </asp:PlaceHolder >
</ItemTemplate>
<FooterTemplate >
</FooterTemplate>
</ASP:Repeater>
May 24 '07 #3
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 programmaticall y set a placeholder control in csharp which is
nested inside a repeater control between <ItemTemplatean d </ItemTemplate>
tags, however I am running into problems. I've tried several different
approaches for finding the placeholder:

This:
MyControl = this.FindContro l("$form$tabs_r epeater$PlaceHo lder1");

Produces this error:
Object reference not set to an instance of an object.
This:
MyControl =
(PlaceHolder)ta bs_repeater.Ite ms[intSiteNum].FindControl("P laceHolder1");

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 GenerateNavBarL ink(string currentdepartme ntid, int SiteNum)
{
int intSiteNum = SiteNum;
string strdepartmentid = currentdepartme ntid;

LinkButton btn = new LinkButton();
btn.ID = "LinkButton 1";
btn.Command += new_department_ button_Click;
btn.Text = "Add Department +";

Control MyControl = new Control();

// Two different ways of updating the place holder
//MyControl =
this.FindContro l("form$tabs_re peater$PlaceHol der1");
//MyControl =
(PlaceHolder)ta bs_repeater.Ite ms[intSiteNum].FindControl("P laceHolder1");

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

}

<ASP:Repeater id="tabs_repeat er" DataSourceID="r epeater_datasou rce"
runat="server">
<HeaderTemplate >
</HeaderTemplate>
<ItemTemplate >
<li><%#
GenerateNavBarL ink((Eval("depa rtment_id").ToS tring()),Contai ner.ItemIndex)
%></li>
<asp:PlaceHolde r ID="PlaceHolder 1"
runat="server"> </asp:PlaceHolder >
</ItemTemplate>
<FooterTemplate >
</FooterTemplate>
</ASP:Repeater>

May 25 '07 #4
Hi Brad,

I agree with Alexey. However,if you want to add dynamic controls into each
repeaterItem row, you can use the "ItemCreate d" 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_ItemC reated(object sender, RepeaterItemEve ntArgs
e)
{
PlaceHolder ph = e.Item.FindCont rol("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
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.ItemInde x; //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******** ******@TK2MSFTN GP04.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 programmaticall y set a placeholder control in csharp which
is nested inside a repeater control between <ItemTemplatean d
</ItemTemplatetag s, however I am running into problems. I've tried
several different approaches for finding the placeholder:

This:
MyControl = this.FindContro l("$form$tabs_r epeater$PlaceHo lder1");

Produces this error:
Object reference not set to an instance of an object.
This:
MyControl =
(PlaceHolder)t abs_repeater.It ems[intSiteNum].FindControl("P laceHolder1");

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 GenerateNavBarL ink(string currentdepartme ntid, int
SiteNum)
{
int intSiteNum = SiteNum;
string strdepartmentid = currentdepartme ntid;

LinkButton btn = new LinkButton();
btn.ID = "LinkButton 1";
btn.Command += new_department_ button_Click;
btn.Text = "Add Department +";

Control MyControl = new Control();

// Two different ways of updating the place holder
//MyControl =
this.FindContr ol("form$tabs_r epeater$PlaceHo lder1");
//MyControl =
(PlaceHolder)t abs_repeater.It ems[intSiteNum].FindControl("P laceHolder1");

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

}

<ASP:Repeate r id="tabs_repeat er" DataSourceID="r epeater_datasou rce"
runat="server" >
<HeaderTemplate >
</HeaderTemplate>
<ItemTemplate >
<li><%#
GenerateNavBar Link((Eval("dep artment_id").To String()),Conta iner.ItemIndex)
%></li>
<asp:PlaceHolde r ID="PlaceHolder 1"
runat="server" ></asp:PlaceHolder >
</ItemTemplate>
<FooterTemplate >
</FooterTemplate>
</ASP:Repeater>

May 25 '07 #6
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
"DataRowVie w", if datasource is "Datareader ", the dataitem instance will be
of "IDataRecor d" 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
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.ToStrin g(DataBinder.Ev al(objRow,
"department_id" ));

Thanks for all the help. :)
Brad
"Steven Cheng[MSFT]" <st*****@online .microsoft.comw rote in message
news:IQ******** ******@TK2MSFTN GHUB02.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
"DataRowVie w", if datasource is "Datareader ", the dataitem instance will
be
of "IDataRecor d" 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1987
by: Scott Schluer | last post by:
Hi All, I have a functioning datagrid on "Page 1" that displays order information for a single order (this is for an e-commerce site). It's actually a combination of a couple datagrids to display all of the information. I now have a need to drop this datagrid into a repeater on "Page 2". The repeater will grab ALL orders within a given...
4
1518
by: News | last post by:
I have a page with many controls. Among these controls there is a table which is a datagrid with nested repeater inside. My problem is that I can not use DataGridCommandEventArgs to get datagrid elements because submit button is not in datagrid and uses onClick event btnSaveServicePlanUpdate. Here is the code: Sub...
3
7407
by: Simon Harvey | last post by:
Hi All. In one of my user controls I add a textbox to a placeholder sitting on the user control. txtUsername = new TextBox(); txtUsername.ID = "txtUsername"; phUsernamePlaceholder.Controls.Add(txtUsername); Then in an event handler for the pages save changes button I try to find the
4
34511
by: huzz | last post by:
I am trying to access a DropDownList control inside a repeater using ItemCommand as shown below but for some reason i can't access the DropDownList. When i step through the debug i get <undefine value> for the DropDownList What am i doing wrong? <asp:Repeater ID="Repeater1" Runat="server" OnItemDataBound="create_ddl"...
3
5667
by: Leigh Webber | last post by:
I have an HTMLAnchor control on my aspx page. When it's not inside a repeater, it works fine. When I put it inside a repeater control, the handler never gets fired. I have a handler for the htmlAnchor's ServerClick event (works when not inside a repeater), and a handler for the repeater's ItemCommand event. My html and vb source code is shown...
0
1825
by: Reza Nabi | last post by:
Dear All: Banckgroud: I have a datagrid which lives inside a repeater. Which is working fine. What i need is to dyanamically set the column width of the grid (which lieves inside the repeater). Is there any way, we accomplish this? Any advice on this would be greatly appreciated. Reza. ---BEGIN PART Of ListApp.aspx --->
8
3010
by: fernandezr | last post by:
I would like to use a user control as a template inside a repeater. Some of the fields in the control should be hidden depending on whether or not there is data. I'm still a ASP .Net newbie so the way I'm going about doing this might be a little off. I'd appreciate some help. Below is the code I have thus far but I'm not sure how to...
1
7313
by: champ.supernova | last post by:
Hi, I have a dropdownlist which is repeated inside a repeater. What I'm wanting is for when one instance of the dropdownlist has its selection changed, for 1) this to trigger an 'OnSelectedIndexChanged' subroutine, and 2) for that subroutine to be able to somehow identify which 'row' of the repeater the dropdownlist belongs to. To keep...
1
1926
by: Øyvind Isaksen | last post by:
I need to know how to handle controls inside a repeater, how to send and recieve data from another page to one spesific control inside a repeater. Clue: I have a repeater that dynamicly lists out different kind of webcontrols (based on some criterias). Each textboxes inside the repeater need to have a "browse" button, when clicking that...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5325
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.