Search code examples
c#radiobuttonlist

C# & Dynamic Radio Button List | Incorrect Radio Button Value on Submit


Here is my code to load the rbl:

protected void rblContentTypesGetAll_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(Global.conString))
    {
        using (SqlCommand cmd = new SqlCommand("contentTypeGetAll", con))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                da.Fill(dt);
            }
        }
    }
    //Clear Items before reloading
    rblContentTypesGetAll.Items.Clear();

    //Populate Radio button list
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        rblContentTypesGetAll.Items.Add(new ListItem(dt.Rows[i]["contentType"].ToString() + " - " + dt.Rows[i]["description"].ToString(),
            dt.Rows[i]["ID"].ToString()));
    }

    //Set Default Selected Item by Value
    rblContentTypesGetAll.SelectedIndex = rblContentTypesGetAll.Items.IndexOf(rblContentTypesGetAll.Items.FindByValue(((siteParams)Session["myParams"]).DefaultContentType.ToLower()));

}

Here is the HTML:

<asp:RadioButtonList id="rblContentTypesGetAll" OnLoad="rblContentTypesGetAll_Load"  runat="server">
</asp:RadioButtonList>

Here is the form taking the submission:

 protected void Submit_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(Global.conString))
    {
        con.Open();
        using (SqlCommand cmd = new SqlCommand("contentPageInsert", con))
        {
            cmd.Parameters.Add("@title", SqlDbType.VarChar).Value = Global.SafeSqlLiteral(txtPage.Text, 1);
            cmd.Parameters.Add("@contentTypeID", SqlDbType.VarChar).Value = rblContentTypesGetAll.SelectedValue;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.ExecuteNonQuery();
        }
        con.Close();

        //Update Content Page Repeater
        using (SqlCommand cmd = new SqlCommand("contentPageGetAll", con))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                da.Fill(dt);
            }
        }
    }

    Session["formProcessed"] = "Page added!";
    Response.Redirect(redirectURL);
}

No matter which radio button I select, the value is always the same - first radio button. What am I doing incorrectly?


Solution

  • The reason, I think, is that the method that is populating radio button list clears and rebuilds on every postback so the by the time submit_click fires, the list has been rebuilt and the selection is lost. Try this,

    protected void rblContentTypesGetAll_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }
    
        var dt = new DataTable();
        using (var con = new SqlConnection(Global.conString))
        using (var cmd = new SqlCommand("contentTypeGetAll", con))
        using (var da = new SqlDataAdapter(cmd))
        {
            da.Fill(dt);
        }
        //Clear Items before reloading
        rblContentTypesGetAll.Items.Clear();
    
        //Populate Radio button list
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            rblContentTypesGetAll.Items.Add(new ListItem(dt.Rows[i]["contentType"].ToString() + " - " + dt.Rows[i]["description"].ToString(),
                dt.Rows[i]["ID"].ToString()));
        }
    
        //Set Default Selected Item by Value
        rblContentTypesGetAll.SelectedIndex = rblContentTypesGetAll.Items.IndexOf(rblContentTypesGetAll.Items.FindByValue(((siteParams)Session["myParams"]).DefaultContentType.ToLower()));
    }