ASP.NET:即使发生更改,页面在回发时也会呈现不变
我有一个包含生成内容的页面。
Page.aspx:
<asp:DropDownList ID="cmbUsers" runat="server" AutoPostBack="True"
oninit="cmbUsers_Init">
</asp:DropDownList>
<asp:Panel ID="pnlRights" runat="server">
Page.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
string [] roles = Roles.GetAllRoles();
string sel_user = ... ; // get the user name selected by combo
foreach (string role in roles)
{
CheckBox chk = new CheckBox();
chk.Text = role;
chk.Checked = Roles.IsUserInRole(sel_user, role);
pnlRights.Controls.Add(chk);
}
}
protected void cmbUsers_Init(object sender, EventArgs e)
{
... // fill the combo with user list
if (!IsPostBack)
{
{
cmbUsers.SelectedValue = // the current signed username;
}
}
}
第一次加载时,页面是正确的 - 所有复选框均按应有的方式设置(选中用户所在的角色)。 当您更改组合中的用户时,就会出现问题。更改后调用回发,所有复选框都再次正确设置(在调试器中看到),但浏览器显示设置为先前用户的复选框。我不怀疑浏览器错误(在 IE、Maxthon、Mozilla 中尝试过),但有些设置我忘记设置。是不是跟缓存有关?请给我一些提示好吗?
I have a page which contains the generated content.
Page.aspx:
<asp:DropDownList ID="cmbUsers" runat="server" AutoPostBack="True"
oninit="cmbUsers_Init">
</asp:DropDownList>
<asp:Panel ID="pnlRights" runat="server">
Page.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
string [] roles = Roles.GetAllRoles();
string sel_user = ... ; // get the user name selected by combo
foreach (string role in roles)
{
CheckBox chk = new CheckBox();
chk.Text = role;
chk.Checked = Roles.IsUserInRole(sel_user, role);
pnlRights.Controls.Add(chk);
}
}
protected void cmbUsers_Init(object sender, EventArgs e)
{
... // fill the combo with user list
if (!IsPostBack)
{
{
cmbUsers.SelectedValue = // the current signed username;
}
}
}
On the first load the page is correct - all checkboxes are set as they should be (the roles where user is in are checked).
The problem happens when you change the user in combo. After change the postback is called, all checkboxes are set correctly again (saw in debugger), BUT the browser shows the checkboxes set to previous user. I do not suspect browser mistake (tried in IE, Maxthon, Mozilla), but some setting I forget to set. Is it something with caching? Could you give me some hint, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次回发时,您都会将页面重建为新状态。检查
Page
对象的IsPostBack
属性,以确保仅初始化页面一次。编辑-再次查看您的示例,这不会完全起作用,您应该有一个按钮或生成回发的东西,并在那里执行响应逻辑,而不是在 page_load 中。这就是您看到这种行为的原因。
Your rebuilding the page to a fresh state each postback. Check the
IsPostBack
property of thePage
object to insure you only init the page once.Edit - Looking at your example again, this won't work exactly, you should have a button or something that is generating the postback, and do your response logic there, not in the page_load. This is the reason your seeing this behavior though.