Bind data to control inside a Repeater

D

David Thielen

Hi;

This is one of those things that I thought would be easy - but can't get it
to work and can't find anything through google.

Inside a Repeater tag I have a CheckBox, DropDownList, and a TextBox. I have
bound a typed collection to the repeater and it works fine for the
<%#Eval("name")%>. But I cannot get it to set initial values for the controls
from the collection and I cannot get it to set the values in the collection
from the controls.

How can I do this?
 
D

David Thielen

Hi;

Ok, I have how to initialize controls - still have a problem that on return
the repeater.DataSource is null so I have to walk the controls. For those
that need this.

code:
string[] vars = template.GetVars();
TemplateMapItemCollection coll = new TemplateMapItemCollection();
foreach (string name in vars)
{
TemplateMapItem item = new TemplateMapItem(0, 0);
item.Name = name;
item.Type = TemplateMapItem.TYPE.TEXT;
coll.Add(item);
}
mapRows.DataSource = coll;
mapRows.DataBind();

aspx:
<table style="width: 100%">
<asp:Repeater ID="mapRows" runat="server">
<HeaderTemplate>
<tr>
<th>
Required</th>
<th>
Name</th>
<th>
Type</th>
<th>
Select</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID="mapRequired" runat="server"
Checked='<%#Eval("Required")%>' /></td>
<td>
<%#Eval("Name")%>
</td>
<td>
<asp:DropDownList ID="mapType" runat="server"
SelectedIndex='<%#Eval("Type")%>'>
<asp:ListItem>Currency</asp:ListItem>
<asp:ListItem>Date</asp:ListItem>
<asp:ListItem>Integer</asp:ListItem>
<asp:ListItem>Number</asp:ListItem>
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Text</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="mapSelect" runat="server" Columns="60"
Text='<%#Eval("Select")%>'></asp:TextBox></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
 
W

Wei-Dong XU [MSFT]

Hi Dave,

Thanks for posting at the newsgroup!

"still have a problem that on return the repeater.DataSource is null"
Does this issue still remain at your scenario? Please feel free to let me
know if any further question.

Have a nice day!

Best Regards,
Wei-Dong XU
Microsoft Support
 
S

Steven Cheng[MSFT]

Hi Dave,

The databinding expression, like <%# %> just put a databind specific
literal control into the certain place. If you also want to retrieve the
value back later, I suggest you put some controls in the repeater's
template, such as textbox, label for bind with the data, so that we can
retrieve back the value from these controls later. e.g:

=====================
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<br /><asp:Label ID="Label1" runat="server" Text='<%#
Eval("CategoryName") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
============================

We can then loop through the Repeater Items in the repeater and read the
Label's Text in each item via the following code:

=====================
protected void Button1_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in Repeater1.Items)
{
Label lbl = item.FindControl("Label1") as Label;

Response.Write("<br/>" + item.ItemIndex + ": " + lbl.Text);
}

}
====================


Hope this helps you.


Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


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

David Thielen

Hi;

Is this correct - data that is bound to a repeater (or GridView??) is
one-way. The data is placed in the controls as the initial value. But the
control does not put it's value back in the data object on post back?

I was assuming that the data went both directions.
 
S

Steven Cheng[MSFT]

Thanks for the response Dave,

Yes, you're right. Due to web application(html page based)'s stateless and
request/response based nature, we can not provide the two-way data binding
like winform application(since they're always in memory). Web application's
web control(databound) is bound with datasource when performing
databinding, after that, the data is stored in viewstate and has no
relation with the original datasource.(though we can manually extract those
values from the databound control...)

and in ASP.NET 2.0, some new databound control, such as GridView,
detailsView support BoundField which can help perform "two-way" like(still
not completely two-way) databinding if work with datasource control. The
datasource control can get parameters from the certain fields in the
databound control(the code that extract values from databound control is
encapsulated in these cases).


Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


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,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top