开发者

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开发者_StackOverflow("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?


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


All the code in your Page_Load needs to be inside:

if(Page.IsPostBack  == false)

You are re-filling the list when the page is submitted causing the list to be repopulated and hence losing the previous items including which one was selected.

http://gurustop.net


Try by filling your required binding in page_load don't forgot to use (!IsPostBack)


The only thing I think that could be happening is when you set the original selected index here:

rblContentTypesGetAll.SelectedIndex = rblContentTypesGetAll.Items.IndexOf(rblContentTypesGetAll.Items.FindByValue(((siteParams)Session["myParams"]).DefaultContentType.ToLower()));

it might not be finding the value and then setting it to "-1". Then if you don't ever select a radio button on the page you would get no selected value.

I tried this and it seemed fine:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Pretending to call your stored proc..
            DataTable dt = new DataTable();
            dt.Columns.Add("contentType");
            dt.Columns.Add("description");
            dt.Columns.Add("ID");
            dt.AcceptChanges();
            for (int i = 0; i < 6; i++)
            {
                DataRow dr = dt.NewRow();
                dr["contentType"] = "cnt" + i.ToString();
                dr["description"] = "desc" + i.ToString();
                dr["ID"] = i.ToString();
                dt.Rows.Add(dr);
            }


            //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 = 0; //this could be -1 also
        }

        lblMessage.Text = "rblContentTypesGetAll.SelectedValue :" + rblContentTypesGetAll.SelectedValue;

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜