Search code examples
c#datasetradiobuttonlist

Populate radiobuttonlist with yes or no options, and save to database on clicking


C# code. I have populated checkboxes, but not radiobuttons before. Here is a sample page:

<asp:Content ID="Content" runat="server" ClientIDMode="Static">
    <asp:UpdatePanel ID="category" runat="server">
        <ContentTemplate>
            <div>
                <span>Data:</span>
                <asp:TextBox ID="txBox" runat="server" TextMode="MultiLine">
                </asp:TextBox>
            </div> 
            <!-- Need to add this to populate from dataset, and save to database table -->     
            <asp:RadioButtonList ID ="radioButtonList" runat="server" AutoPostBack="true"
                OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"  >
                <asp:ListItem ID="isEnabledTrue">Yes</asp:ListItem>
                <asp:ListItem ID ="isEnabledFalse" Selected="True">No</asp:ListItem>           
            </asp:RadioButtonList>    
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

How do I populate the radioButtonList in the same way. The table/dataset returned comes as Name : NVARCHAR(25) isEnabled: 1/0 - BIT

How do I attach the 'isEnabled' dataset to the radioButtonList control. I also need to monitor changes to the radioButtonlist control and save to database.

So, I have two methods associated with the control that need to: Populate values from database: TextBox and RadioButtonList from the DataSet Save values to database: TextBox and RadioButton selected, to be saved to table.

My alternate approach if this cannot work, is to get XML from database, then construct an XSLt from this XML structure. I can add events that make the database call via AJAX, to save the updated radiobutton/textbox if needed. I would need additional code to ensure the javascript methods work too. Just wanted to test the dataset approach before going the XML way..

My aspx.cs code-behind does this.

protected void Page_Load(object sender, EventArgs e)
{
    Populate();
}

private void Populate()
{
    DataSet dataSet = CustomCode.GetDataSet(somethingPassedIn);
    DataRow row = dataSet .Tables["Table1"].Rows[0];
    txBox.Text = row["Name"].ToString();
    //How to populate RadioButtonList with Yes/No checked appropriately? 
    //A bit column of 1/0 is returned per what needs to be checked 
    //(1=> Yes should be selected, 0=>No should be selected) 
}

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
    SaveData(); 
}
private void SaveData()
{
    CustomCode.SaveData(txBox.Text.Trim(), radioButtonList?);       
}

Solution

  • Binding:

    if((bool)row["IsEnabled"])
    {
        radioButtonList.SelectedValue = "Yes";
    }
    else
    {
        radioButtonList.SelectedValue = "No";
    }
    

    Saving:

    bool isEnabled;
    if(radioButtonList.SelectedValue = "Yes")
    {
        isEnabled = true;
    }
    CustomCode.SaveData(txBox.Text.Trim(), isEnabled);