Solved

How can I bind data to radiobuttonlist in vs2010?

Posted on 2011-03-14
4
480 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 500 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now