Repeater

  • Thread starter Thread starter Christoph Boget
  • Start date Start date
C

Christoph Boget

Could someone point me to a resource that discusses how to
set up other controls (input box, checkbox, drop down list), etc
to be used in a Repeater? I'm not having a problem displaying
text in the ItemTemplate, but displaying controls has been another
story altogether.

thnx,
Christoph
 
Christoph said:
Could someone point me to a resource that discusses how to
set up other controls (input box, checkbox, drop down list), etc
to be used in a Repeater? I'm not having a problem displaying
text in the ItemTemplate, but displaying controls has been another
story altogether.

thnx,
Christoph

Filling the controls is easy: just add them to the ItemTemplate,
and use a databinding expression:

<ItemTemplate>
<asp:TextBox id="IDOfTextBox" Text='<%# DataBinder.Eval..... %>'
runat="server" />
</ItemTemplate>

On the checkbox, you databind the Checked property.
On the dropdownlist, you databind the SelectedIndex property.

Reading the controls goes like this:
To read the textbox on the 5th item (code for VB):
strInput = CType(Repeater1.Items(6).FindControl("IDOfTextBox"),TextBox).Text

The other controls are treated similarly.

I hope this helps,
 
Filling the controls is easy: just add them to the ItemTemplate,
and use a databinding expression:
<ItemTemplate>
<asp:TextBox id="IDOfTextBox" Text='<%# DataBinder.Eval..... %>'
runat="server" />
</ItemTemplate>

Ok, how would you go about binding a control to the Repeater that,
itself, needs to be databound. Say, for example, a DropDownList?

thnx,
Christoph
 
Christoph said:
Ok, how would you go about binding a control to the Repeater that,
itself, needs to be databound. Say, for example, a DropDownList?

thnx,
Christoph

In that case, you bind it in the OnItemDataBound
event of the Repeater.
The catch here is that you need to refill the dropdownlist on
postback, otherwise its selection would be lost.

This means that, even on postback, you need to databind
the repeater (and automatically, the dropdownlist). So do
NOT use the If(Not IsPostback) condition.

After databinding, in the case of postback, you can read
the selected value in the dropdownlist.
 
Ok, how would you go about binding a control to the Repeater that,
In that case, you bind it in the OnItemDataBound event of the Repeater.

Could you provide code (or another resource) that shows how to do this?
Because I've tried using that event but the drop down just isn't getting
filled.
When I'm use FindControl() to get the reference to the control, it always
returns null. Here's my code:

private void Repeater1_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
UserDetailsList userDetailsList = UserDetails.FindAll();
DropDownList ddl = (DropDownList)e.Item.FindControl("DropDownList2");
if(ddl != null)
{
ddl.DataSource = userDetailsList;
ddl.DataTextField = "Name";
ddl.DataValueField = "UserId";
ddl.DataBind();
}
}

'userDetailsList' is just an ArrayList. Since the variable 'ddl' is always
null, it never populates. I must be doing *something* wrong...
The catch here is that you need to refill the dropdownlist on
postback, otherwise its selection would be lost.
Alright.

This means that, even on postback, you need to databind
the repeater (and automatically, the dropdownlist). So do
NOT use the If(Not IsPostback) condition.

Then how do I determine if the form needs processing because
the user 'Submit'ted the form?
After databinding, in the case of postback, you can read
the selected value in the dropdownlist.

It's insane that using a DropDownList in a Repeater control
requires the developer to jump through so many hoops...

thnx,
Christoph
 
Christoph said:
Could you provide code (or another resource) that shows how to do
this? Because I've tried using that event but the drop down just
isn't getting filled.
When I'm use FindControl() to get the reference to the control, it
always returns null. Here's my code:

private void Repeater1_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
UserDetailsList userDetailsList = UserDetails.FindAll();
DropDownList ddl = (DropDownList)e.Item.FindControl("DropDownList2");
if(ddl != null)
{
ddl.DataSource = userDetailsList;
ddl.DataTextField = "Name";
ddl.DataValueField = "UserId";
ddl.DataBind();
}
}

'userDetailsList' is just an ArrayList. Since the variable 'ddl' is
always null, it never populates. I must be doing *something* wrong...


Then how do I determine if the form needs processing because
the user 'Submit'ted the form?


It's insane that using a DropDownList in a Repeater control
requires the developer to jump through so many hoops...

thnx,
Christoph

It's not easy, I admit.

Your ItemDataBound code is missing this check:
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem) {

That causes your code to execute for the HeaderTemplate
as well (and firstly), and obviously, there's no dropdownlist
in the header!

See:
http://msdn.microsoft.com/library/e...ControlsRepeaterClassOnItemDataBoundTopic.asp
for more info.

This modification should get you rolling to fill the dropdownlists.

I was wrong about the IsPostback part. I think that the
dropdownlist will keep its state on postback automatically
thanks to viewstate.

Some articles:
http://aspnet.4guysfromrolla.com/articles/080702-1.aspx
(uses another technique for databinding the ddl)
http://www.4guysfromrolla.com/webtech/050801-1.shtml
http://www.developer.com/net/vb/article.php/3320941

All of them use a datagrid, not a repeater.
When they use the EditCommand handler, you should use
ItemDataBound.
When they use the UpdateCommand handler, you should
use the iteration through all the items, as I explained
in my first reply.
 
Back
Top