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

时间:2011-02-17 01:56:23

标签: 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);
}

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

1 个答案:

答案 0 :(得分:3)

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