请教一个asp.net的基础问题:页面IsPostBack对GridView中checkbox列选择状态的影响

step_123 2008-07-11 11:44:28
private void BindGrid(){
//动态创建列表中的列,以及包含的控件
}
Page_load()
{
if(!IsPostBack){}
//调用绑定列表
BindGrid();
}

private void Button1_click()
{
//获得checkbox的选择状态
}
问题:
当选择列表中的checkbox后,再点击Button的click事件时,都会执行page_load的if(!IsPoseBack){}外面的代码,然后再执行事件本身的代码
这样列表就重新绑定了,也就是GridView中的checkbox控件重新创建了,那它的状态怎么还能得到?
(事实上是可以得到的)问题是不明白为什么可以得到?

不知道问题是否描述清除,在此请教各位,谢谢!
...全文
136 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cpp2017 2008-07-11
  • 打赏
  • 举报
回复
虽然是重新创建,就像是动态创建控件一样,只要是同一位置,它的状态是可以保存的
chensuifu 2008-07-11
  • 打赏
  • 举报
回复
建议lz参照一下页面编程模型,可以找到你想要的结果
step_123 2008-07-11
  • 打赏
  • 举报
回复
列表的形式如果是使用GridView的模板列
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="600px">
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" ToolTip='<%# Bind("ObjectID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="内容1">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("LoginID") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="内容2">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("UserName") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
则这个BindGide()应该放在IsPostBack里面;
如果使用服务器控件Table来构造:
this.Table1.Rows.Clear();
TableRow row;
TableCell cell;
#region create title
row = new TableRow();
row.Height = new Unit(26, UnitType.Pixel);
row.VerticalAlign = VerticalAlign.Middle;
row.HorizontalAlign = HorizontalAlign.Center;
row.BackColor = System.Drawing.Color.Silver;

cell = new TableCell();//checkBox
cell.Text = "选择";
cell.Width = new Unit(5, UnitType.Percentage);
cell.ForeColor = System.Drawing.Color.Blue;
cell.HorizontalAlign = HorizontalAlign.Center;
row.Cells.Add(cell);

cell = new TableCell();
cell.Text = "内容11";
cell.Width = new Unit(8, UnitType.Percentage);
cell.ForeColor = System.Drawing.Color.Blue;
cell.HorizontalAlign = HorizontalAlign.Center;
row.Cells.Add(cell);

cell = new TableCell();
cell.Text = "内容22";
cell.Width = new Unit(8, UnitType.Percentage);
cell.ForeColor = System.Drawing.Color.Blue;
cell.HorizontalAlign = HorizontalAlign.Center;
row.Cells.Add(cell);

#endregion
this.Table1.Rows.Add(row);
int i=0;
foreach (DataRow dr in dt.Rows)
{
row = new TableRow();
row.Height = new Unit(20, UnitType.Pixel);
row.VerticalAlign = VerticalAlign.Middle;
row.HorizontalAlign = HorizontalAlign.Right;
if (i % 2 == 0)
row.BackColor = System.Drawing.Color.Aqua;
#region create content
cell = new TableCell();
CheckBox cb = new CheckBox();
cb.Checked = false;
cb.ID = "cb_" + dr[0].ToString();
cb.ToolTip = dr[2].ToString();
cell.Controls.Add(cb);
row.Cells.Add(cell);

cell = new TableCell();
TextBox txt = new TextBox();
txt.ID = "txt_" + dr[0].ToString();
txt.Text = dr[1].ToString();
cell.Controls.Add(txt);
row.Cells.Add(cell);

cell = new TableCell();
TextBox txt2 = new TextBox();
txt2.ID = "txt2_"+dr[0].ToString();
txt2.Text = dr[2].ToString();
cell.Controls.Add(txt2);
row.Cells.Add(cell);
#endregion
i++;
this.Table1.Rows.Add(row);
}
则这个BindGrid要放在IsPostBack外面
否则得不到CheckBox的状态

请问为什么会有这种区别?谢谢!
brooklyng60 2008-07-11
  • 打赏
  • 举报
回复
把绑定表格的放IsPostBack里面啊,要不然每次回调都重新绑定,肯定不行列

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧