2 Way databinding on Gridview RadioButtonList possible?

Posts   
 
    
trevorg
User
Posts: 104
Joined: 15-Nov-2007
# Posted on: 06-Jul-2009 23:42:59   

If I have a RadioButtonList in a TemplateField in a GridView, using a LLBLGenProDataSource datasource to populate the gridview, is it possible on postback for the .SelectedValue of the RadioButtonList to automatically be pushed back through to the datasource control, or do I have to do a for each on the gridview rows and access each control to load the entity instance and store the new SelectedValue?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 07-Jul-2009 08:20:54   

How do you bind the Field's value to the RadioButtonList ? Please post a code snippet.

trevorg
User
Posts: 104
Joined: 15-Nov-2007
# Posted on: 07-Jul-2009 17:02:39   

So, this works for loading and displaying the data:

<llblgenpro:LLBLGenProDataSource ID="llbComputerApplication" 
            DataContainerType="EntityCollection" runat="server" 
            EntityCollectionTypeName="VistaDesktopSurvey.Domain.CollectionClasses.ComputerApplicationCollection, VistaDesktopSurvey.Domain">
        </llblgenpro:LLBLGenProDataSource>
        <asp:GridView ID="gridComputerApps" DataSourceID="llbComputerApplication" runat="server" AutoGenerateColumns="False" EnableViewState="true" 
            EmptyDataText ="NO APPLICATIONS FOUND FOR THIS COMPUTER." 
            DataKeyNames="ComputerID, ApplicationID" 
            style="border-style:dotted;border-width:thin">
            <Columns>
                <asp:BoundField DataField="ApplicationID" HeaderText="Application ID" SortExpression="ApplicationID" Visible="True" />
                <asp:BoundField DataField="ComputerID" HeaderText="ComputerID" SortExpression="ComputerID" Visible="True" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:RadioButtonList ID="rblRequirementOption" RepeatDirection="Horizontal"  runat="server"
                         SelectedValue='<%#Bind("RequirementOption")%>'>
                            <asp:ListItem Value="Need Now" Text="Need Now"></asp:ListItem>
                            <asp:ListItem Value="Need Someday" Text="Need Someday"></asp:ListItem>
                            <asp:ListItem Value="Do Not Need" Text="Do Not Need"></asp:ListItem>
                        </asp:RadioButtonList>
                        <asp:RequiredFieldValidator 
                            ID="ReqiredFieldValidator1"
                            runat="server"
                            ControlToValidate="rblRequirementOption"
                            ErrorMessage="Select an Application Requirment Value.">
                        </asp:RequiredFieldValidator>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="NormalizedNotes" HeaderText="Notes" Visible="False" />
            </Columns>
        </asp:GridView>

This is how I'm saving it now:

        For iRow As Integer = 0 To Me.gridComputerApps.Rows.Count - 1
            Dim selectedValue As String = CType(Me.gridComputerApps.Rows(iRow).FindControl("rblRequirementOption"), RadioButtonList).SelectedValue
            Dim computerApp As New ComputerApplicationEntity(Me.gridComputerApps.DataKeys(iRow).Item("ComputerID").ToString, CType(Me.gridComputerApps.DataKeys(iRow).Item("ApplicationID"), Integer))
            computerApp.RequirementOption = selectedValue
            If computerApp.IsNew Then Throw New System.Exception("Error looking up Computer/Application") '  good enough, should never happen
            computerApp.Save()
        Next
trevorg
User
Posts: 104
Joined: 15-Nov-2007
# Posted on: 07-Jul-2009 23:39:46   

I also wanted to ask:

  1. Do you know of any way to put an entire GridView into edit mode so every row can be edited at once, and then have all rows saved on postback (again, with two way databinding)
  2. How does one add a new row, edit, and save (again, with two way databinding)
Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 08-Jul-2009 10:17:57   

In the GridView, using a template field, yo ushould provide an EditItemTemplate for the edit mode. Similar to the Itemtemplate you provided.

  1. Do you know of any way to put an entire GridView into edit mode so every row can be edited at once, and then have all rows saved on postback (again, with two way databinding)
  2. How does one add a new row, edit, and save (again, with two way databinding)

I've blogged about this before: 1- http://walaapoints.blogspot.com/2007/05/bulk-edit-in-aspnet-20-gridview.html 2- http://walaapoints.blogspot.com/2007/05/inserting-from-aspnet-gridview.html

trevorg
User
Posts: 104
Joined: 15-Nov-2007
# Posted on: 08-Jul-2009 19:38:32   

Ok thanks.

Did you have any idea about my RadioButtonList issue?:

trevorg wrote:

If I have a RadioButtonList in a TemplateField in a GridView, using a LLBLGenProDataSource datasource to populate the gridview, is it possible on postback for the .SelectedValue of the RadioButtonList to automatically be pushed back through to the datasource control, or do I have to do a for each on the gridview rows and access each control to load the entity instance and store the new SelectedValue?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 09-Jul-2009 10:16:37   

Walaa wrote:

In the GridView, using a template field, you should provide an EditItemTemplate for the edit mode. Similar to the Itemtemplate you provided.