ASP.NET 单选按钮列表 | SelectedValue 出现 NULL
我发布了一个类似的 RBL 问题,但出现了一个新问题,所以我想我应该发布一个新帖子。
这是我的代码:
Page_Load
protected void Page_Load(object sender, EventArgs e)
{
//Output Success/Error Message
if (Session["formProcessed"] != null)
{
Label lblMessage = (Label)Master.FindControl("lblMessage");
new Global().DisplayUserMessage("success", Session["formProcessed"].ToString(), lblMessage);
}
Session.Remove("formProcessed");
if (Page.IsPostBack == false)
{
rblContentTypesGetAll.DataBind();
}
}
rblContentTypesGetAll_Load
protected void rblContentTypesGetAll_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
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.NET 前端
<asp:RadioButtonList id="rblContentTypesGetAll" OnLoad="rblContentTypesGetAll_Load" runat="server">
</asp:RadioButtonList>
一旦我提交表单,selectedValue
就会变成空白。我在做什么,这明显是不正确的?
I posted a similar RBL question but I have a new issue arising so I figured I'd make a new post.
Here is my code:
Page_Load
protected void Page_Load(object sender, EventArgs e)
{
//Output Success/Error Message
if (Session["formProcessed"] != null)
{
Label lblMessage = (Label)Master.FindControl("lblMessage");
new Global().DisplayUserMessage("success", Session["formProcessed"].ToString(), lblMessage);
}
Session.Remove("formProcessed");
if (Page.IsPostBack == false)
{
rblContentTypesGetAll.DataBind();
}
}
rblContentTypesGetAll_Load
protected void rblContentTypesGetAll_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
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.NET front end
<asp:RadioButtonList id="rblContentTypesGetAll" OnLoad="rblContentTypesGetAll_Load" runat="server">
</asp:RadioButtonList>
As soon as I submit the form it seems the selectedValue
becomes blank. What am I doing that's so obviously incorrect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尽管你们所有人都提供了帮助,但问题更为深刻。我禁用了 viewState。
Although all of you were helpful, the issue was much deeper. I had viewState disabled.
Page_Load
中的所有代码都需要位于内部:提交页面时重新填充列表,导致列表被重新填充,从而丢失之前的项目,包括选择的项目。
All the code in your
Page_Load
needs to be inside:You are re-filling the list when the page is submitted causing the list to be repopulated and hence losing the previous items including which one was selected.
尝试在 page_load 中填写所需的绑定,不要忘记使用 (!IsPostBack)
Try by filling your required binding in page_load don't forgot to use (!IsPostBack)
我认为唯一可能发生的事情是当您在此处设置原始选定的索引时:
它可能找不到该值然后将其设置为“-1”。那么,如果您从未选择页面上的单选按钮,您将不会获得任何选定的值。
我尝试了这个,看起来不错:
The only thing I think that could be happening is when you set the original selected index here:
it might not be finding the value and then setting it to "-1". Then if you don't ever select a radio button on the page you would get no selected value.
I tried this and it seemed fine: