C# &动态单选按钮列表|提交时的单选按钮值不正确
这是我加载 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为,原因是填充单选按钮列表的方法会在每次回发时清除并重建,因此当 Submit_click 触发时,列表已重建并且选择丢失。试试这个,
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,