读书人

关于gridview的超级怪有关问题

发布时间: 2012-01-03 22:16:06 作者: rapoo

关于gridview的超级怪问题
前台代码:
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID= "celected " runat= "server " > </asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>

<asp:Button ID= "btnhy " runat= "server " Text= "还原 " CssClass= "button " OnClick= "btnhy_Click "/>

后台代码:
protected void btnhy_Click(object sender, EventArgs e)
{
bool isdelete = false;

for (int i = 0; i < this.GridView1.Rows.Count; i++)
{

bool isChecked = ((CheckBox)GridView1.Rows[i].FindControl( "celected ")).Checked;
string strId = GridView1.DataKeys[i].Values[0].ToString();
if (isChecked)
{
isdelete = true;
string sql = "update t_message set deleteid=0 where id= " + strId + " ";
obj.New();
obj.execnonsql(sql);
obj.Close();

}
}
if (isdelete == true)
{
mainlr.Text = " <font color=red> 操作成功! </font> ";//输出错误信息

dgdbind();
}
else
{
Response.Write( " <script type= 'text/javascript ' > alert( '请先选择记录! '); </script> ");
}
}

各位:帮帮忙。
这是一个关于gridview的问题,在点击还原按钮的时候,老提示请先选择记录,我明明勾选了复选框的,但就是提示没有选中记录,为什么,请帮忙看哈代码,感谢。

------解决方案--------------------


执行 btnhy_Click 前 Page_Load 运行在先了
如果你是在 Page_Load 里面绑定数据 这里 Grid 就被重新刷了
那么请你给 Page_Load 里的代码加个限制
if (!IsPostBack)
{
// 原来 Page_Load 里的代码
}

这样应该就可以了

读书人网 >C#

热点推荐