C# 和动态单选按钮列表 |提交时的单选按钮值不正确

标签 c# radiobuttonlist

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

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

最佳答案

我认为原因是填充单选按钮列表的方法会在每次回发时清除并重建,因此当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()));
}

关于C# 和动态单选按钮列表 |提交时的单选按钮值不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5024316/

相关文章:

r - r Shiny 中对齐的单选按钮

C#正则表达式硬盘问题

c# - 无法在 Windows Phone 8.1 Silverlight 上使用 WNS 发送推送通知

c# - 安装 Visual Studio 11 beta 后,不再通过 Visual Studio 2010 执行测试

javascript - 使用javascript在单选按钮列表上调用onclick

asp.net - 如何通过 jQuery 禁用 ASP.NET 单选按钮列表

java - 将 double 组转换为字节数组 : What is the Java way of C# Buffer. BlockCopy?

c# - Entity Framework : map multiple classes to one table

html - 单选按钮列表 CSS 样式