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

发布于 2024-10-17 22:29:45 字数 2260 浏览 3 评论 0原文

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

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

Here is my code to load the 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()));

}

Here is the HTML:

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

Here is the form taking the submission:

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

No matter which radio button I select, the value is always the same - first radio button. What am I doing incorrectly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声情话 2024-10-24 22:29:45

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

The reason, I think, is that the method that is populating radio button list clears and rebuilds on every postback so the by the time submit_click fires, the list has been rebuilt and the selection is lost. Try this,

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()));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文