Home All Groups Group Topic Archive Search About

Bind data to control inside a Repeater

Author
20 Mar 2006 11:20 PM
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?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Author
20 Mar 2006 11:39 PM
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>




--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com



Show quoteHide quote
"David Thielen" wrote:

> 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?
>
> --
> thanks - dave
> david_at_windward_dot_net
> http://www.windwardreports.com
>
Are all your drivers up to date? click for free checkup

Author
21 Mar 2006 8:56 AM
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
---------------------------------------------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.
---------------------------------------------------------------------------
It is my pleasure to be of any assistance.
Author
21 Mar 2006 1:59 PM
David Thielen
Yes - getting the values back on a submit is still a problem.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com



""Wei-Dong XU [MSFT]"" wrote:

Show quoteHide quote
>
>
> 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
> ---------------------------------------------------------------------------
> This posting is provided "AS IS" with no warranties, and confers no rights.
> ---------------------------------------------------------------------------
> It is my pleasure to be of any assistance.
>
>
>
Author
22 Mar 2006 11:12 AM
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.
Author
22 Mar 2006 2:22 PM
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.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com



Show quoteHide quote
"Steven Cheng[MSFT]" wrote:

> 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.
>
>
Author
23 Mar 2006 2:10 AM
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.

Bookmark and Share