Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can I bind data to radiobuttonlist in vs2010?

Posted on 2011-03-14
4
Medium Priority
?
508 Views
Last Modified: 2012-05-11
I have a gridview that is being populated by a datatable that I have created manually. This gridview has text boxes and radiobuttonlists that are visible at all times. In other words the gridview is editable on all rows at all times. I need the radiobuttonlists selected index to be binded to the radiobuttonlist. I currently set up a ondatabound method to set this, but it's not working. Any help on this was be much appreciated.
protected void Page_Load(object sender, EventArgs e)
        {
            //forces postback when the treeview iten's check box is clicked.
            tvContracts.Attributes.Add("onclick", "postBackByObject()");

            ManagedUser = (Administrator)HttpContext.Current.Session["UserObject"];

            //Populating all drop downs for the contract modal window.
            if(!IsPostBack)
            {
                ManagedUser.GetTradeIds(ddlCompany);
                Contracts.getSource(ddlSource);
                Contracts.getTemplate(ddlContractType);
                CallOptions.fillEmptyCallOptionGV(CallOptionGV);
            }
        }


 protected void CallOptionGV_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            RadioButtonList rdl;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = Convert.ToInt32(e.Row.RowIndex.ToString());
                rdl = (RadioButtonList)e.Row.FindControl("RdCounterNotification");
                rdl.DataSource = CallOptions.dt;
                rdl.DataValueField = CallOptions.dt.Rows[i].Table.Columns[2].ToString();
                rdl.SelectedValue = CallOptions.dt.Rows[i].Table.Columns[2].ToString();
                rdl.DataBind();
            }


        }

Open in new window

aspx file

<asp:GridView ID="CallOptionGV" runat="server" BackColor="White" Width="1200" OnRowDataBound="CallOptionGV_RowDataBound" 
        BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" Font-Size="Small"
        ForeColor="Black" GridLines="Vertical">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" Visible="false" InsertVisible="False" ReadOnly="True"
SortExpression="id" />
                <asp:TemplateField HeaderText="Product Name" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtProductName" runat="server" Width="20" MaxLength="1" Text='<%# Bind("NAME") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Call Option Name" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtCallOptionName" runat="server" Width="75" Text='<%# Bind("CALL_OPTION_NAME") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Counterparty Notifications" InsertVisible="true">
                    <ItemTemplate>
                        <asp:RadioButtonList ID="RdCounterNotification" runat="server">
                            <asp:ListItem Text="Yes" Value="0"></asp:ListItem>
                            <asp:ListItem Text="No" Value="1"></asp:ListItem>
                        </asp:RadioButtonList>
                    </ItemTemplate>
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="EMO Notifications" InsertVisible="true">
                    <ItemTemplate>
                        <asp:RadioButtonList ID="RdEMONotification" runat="server">
                            <asp:ListItem Text="Yes" Value="0"></asp:ListItem>
                            <asp:ListItem Text="No" Value="1"></asp:ListItem>
                        </asp:RadioButtonList>
                    </ItemTemplate>
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Counterparty Email Groups" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtCounterEmail" runat="server" Width="100" Text='<%# Bind("COUNTERPARTY_EMAILS") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EMO Email Groups" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtEMOEmail" runat="server" Width="100" Text='<%# Bind("EMO_EMAILS") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Trades Deal Number" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtTradesNo" runat="server" Width="70" Text='<%# Bind("TRADES_DEAL_NUMBER") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Availability Default to Option Volume" InsertVisible="true">
                    <ItemTemplate>
                        <asp:RadioButtonList ID="RdDftOptionVol" runat="server">
                            <asp:ListItem>Yes</asp:ListItem>
                            <asp:ListItem>No</asp:ListItem>
                        </asp:RadioButtonList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="MW" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtMW" runat="server" Width="50" Text='<%# Bind("MW") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Allow Additional MW" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtAddMW" runat="server" Width="50" Text='<%# Bind("ALLOW_ADDITIONAL_MW") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Seller to Provide Availability by" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtSellerBy" runat="server" Width="50" Text='<%# Bind("SELLER_AVAILABILITY_BY") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EMO to Provide Schedule/Strike Notice by" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtEMOBy" runat="server" Width="50" Text='<%# Bind("EMO_STRIKE_NOTICE_BY") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Minimum Run Time" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtMinRunTime" runat="server" Width="50" Text='<%# Bind("MIN_RUN_TIME") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Minimum Down Time" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtMinDwnTime" runat="server" Width="50" Text='<%# Bind("MIN_DOWN_TIME") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Starts Per Day" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtStartsPerDay" runat="server" Width="30" Text='<%# Bind("STARTS_PER_DAY") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EMO to Provide Intraday Changes by" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtEMOIntraChg" runat="server" Width="50" Text='<%# Bind("EMO_INTRADAY_CHANGES_BY") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Minimum Starts During Contract Term" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtMinStarts" runat="server" Width="30" Text='<%# Bind("MIN_CONTRACT_STARTS") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Maximum Starts During Contract Term" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtMaxStarts" runat="server" Width="30" Text='<%# Bind("MAX_CONTRACT_STARTS") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Additional Start Charge for Dispatch > 24 Hours" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtStartCharge" runat="server" Width="80" Text='<%# Bind("ADDITIONAL_START_CHARGE") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Comments" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtCallOptionComments" runat="server" Width="100" Text='<%# Bind("COMMENTS") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>    
            </Columns>
            <FooterStyle BackColor="#CCCC99" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <RowStyle BackColor="#F7F7DE" />
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FBFBF2" />
            <SortedAscendingHeaderStyle BackColor="#848384" />
            <SortedDescendingCellStyle BackColor="#EAEAD3" />
            <SortedDescendingHeaderStyle BackColor="#575357" />
        </asp:GridView>

Open in new window

0
Comment
Question by:bschave2
  • 2
4 Comments
 
LVL 1

Expert Comment

by:pjbaratelli
ID: 35132272
Try this:

 RadioButtonList rdl;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = Convert.ToInt32(e.Row.RowIndex.ToString());
                rdl = (RadioButtonList)e.Row.FindControl("RdCounterNotification");
                rdl.DataSource = CallOptions.dt;
                rdl.DataValueField = [FieldNameForValue];
                rdl.SelectedValue = [FieldNameForText];
                rdl.DataBind();

try {
#Either
                rdl.Items.FindByText["TheDisplayText"].Selected = True
#OR
                rdl.Items.FindByValue["TheValueOfListItem"].Selected = True
}
catch
{
       // Do nothing
}

            }

Open in new window

The try catch makes sure no errors pop up if there are no matching value.

0
 

Author Comment

by:bschave2
ID: 35132685
i tried this and it doesn't work. e.Row.RowType never equals DataControlRowType.DataRow. could you please post full code with the method and all?
0
 
LVL 10

Accepted Solution

by:
hosneylk earned 1000 total points
ID: 35134554
can't you just bind the radiobuttonlist selected value manually?

e.g.
<asp:RadioButtonList ID="RdCounterNotification" runat="server" SelectedValue='<%# Bind("Field_Name") %>'>
   <asp:ListItem Text="Yes" Value="0"></asp:ListItem>
   <asp:ListItem Text="No" Value="1"></asp:ListItem>
</asp:RadioButtonList>

Open in new window

0
 

Author Closing Comment

by:bschave2
ID: 35261728
the right answer wasn't given.
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question