【问题标题】:ASP.NET Radio Button List | SelectedValue coming up NULLASP.NET 单选按钮列表 | SelectedValue 出现 NULL
【发布时间】:2013-04-17 10:39:57
【问题描述】:

我发布了一个类似的 RBL 问题,但我遇到了一个新问题,所以我想我会发布一个新帖子。

这是我的代码:

页面加载

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 前端

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

我一提交表单,selectedValue 似乎就变成了空白。我在做什么明显不正确?

【问题讨论】:

    标签: c# server-side radiobuttonlist


    【解决方案1】:

    尽管你们所有人都提供了帮助,但问题要深得多。我禁用了 viewState。

    【讨论】:

    • 我要杀了view-state
    【解决方案2】:

    Page_Load 中的所有代码都需要在里面:

    if(Page.IsPostBack  == false)
    

    您在提交页面时重新填充列表,导致列表被重新填充,因此丢失了之前的项目,包括被选中的项目。

    http://gurustop.net

    【讨论】:

    • 按下提交按钮后该值似乎立即消失。我将 DataBind 包装在您上面发布的内容中。
    【解决方案3】:

    尝试在 page_load 中填写所需的绑定,不要忘记使用 (!IsPostBack)

    【讨论】:

    • 我要求的是绑定页面加载而不是单选按钮加载
    【解决方案4】:

    我认为唯一可能发生的事情是当您在此处设置原始选定索引时:

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

    它可能没有找到值,然后将其设置为“-1”。然后,如果您从未在页面上选择单选按钮,您将不会获得选定的值。

    我试过了,感觉还不错:

        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;
    
        }
    

    【讨论】:

    • 太奇怪了……我觉得这个问题很简单。我试过了,它仍然提交一个空白值。列表填充并且值具有值。我不知道是什么导致列表消失。
    • 选择的值在按下提交后立即删除,甚至还没有通过后面的代码
    • 一定有什么东西不见了,试着去掉母版页和任何没有显示的javascript,上面的代码有效,所以有一些外部代码弄乱了单选按钮列表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多