Search code examples
c#server-sideradiobuttonlist

ASP.NET Radio Button List | SelectedValue coming up NULL


I posted a similar RBL question but I have a new issue arising so I figured I'd make a new post.

Here is my code:

Page_Load

protected void Page_Load(object sender, EventArgs e)
{
    //Output Success/Error Message
    if (Session["formProcessed"] != null)
    {
        Label lblMessage = (Label)Master.FindControl("lblMessage");
        new Global().DisplayUserMessage("success", Session["formProcessed"].ToString(), lblMessage);
    }
    Session.Remove("formProcessed");

    if (Page.IsPostBack == false)
    {
        rblContentTypesGetAll.DataBind();
    }
}

rblContentTypesGetAll_Load

 protected void rblContentTypesGetAll_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        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()));
    }

}

HTML/ASP.NET front end

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

As soon as I submit the form it seems the selectedValue becomes blank. What am I doing that's so obviously incorrect?


Solution

  • Although all of you were helpful, the issue was much deeper. I had viewState disabled.