【问题标题】:C# & Dynamic Radio Button List | Incorrect Radio Button Value on SubmitC# & 动态单选按钮列表 |提交时单选按钮值不正确
【发布时间】:2011-02-17 12:46:16
【问题描述】:

这是我加载 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()));

}

这是 HTML:

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

这是接受提交的表格:

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

无论我选择哪个单选按钮,值始终相同 - 第一个单选按钮。我做错了什么?

【问题讨论】:

    标签: c# radiobuttonlist


    【解决方案1】:

    我认为,原因是填充单选按钮列表的方法会在每次回发时清除并重建,因此在 submit_click 触发时,列表已重建并且选择丢失。试试这个,

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

    【讨论】:

    • 虽然您的修复听起来是正确的,但它只是阻止了列表的绑定。当我有表单显示时,我尝试绑定它,但它不起作用。
    • 4guysfromrolla.com/webtech/072701-1.shtml 这篇帖子已有 10 年历史,并且是 vb.net,但如果上述建议没有帮助,您可以尝试做类似的事情。
    • StackOverflowException 是 100% 正确的。您需要确保仅在页面首次加载时才调用 rblContentTypesGetAll_Load 方法。如果此答案不能解决问题,则会发生其他事情。顺便说一句 - 查看我对 StackOverflowException 代码的编辑以帮助减少嵌套。
    • 可能是因为表单在按下链接按钮后加载,而单选按钮列表不会加载,因为我有!IsPostback?
    猜你喜欢
    • 2012-04-05
    • 2015-09-19
    • 2018-05-12
    • 2017-11-05
    • 2013-04-10
    • 1970-01-01
    • 2011-04-20
    • 2023-03-29
    • 2013-01-21
    相关资源
    最近更新 更多