读书人

绑定 gridview 有关问题

发布时间: 2012-01-22 22:38:43 作者: rapoo

绑定 gridview 问题
只能显示出这个checkbox的框但是不能得到值 为什么呢??

前台:
<asp:GridView ID= "GridView1 " runat= "server " AllowPaging = "True " AutoGenerateColumns = "False " Font-Size= "10pt "
HorizontalAlign = "Center " Width= "780px " ShowHeader = "False " borderwidth= "0px ">
<Columns >
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID= "CBox " runat= "server ">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
后台:
protected void Audi_btn_Click(object sender, EventArgs e)
{

CheckBox chk;
foreach (GridViewRow gvr in GridView1.Rows)
{
chk = (CheckBox)gvr.FindControl( "CBox ");
if (chk.Checked)
{
try
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandText = "update MessageBoard set isnew= 'false ' where id= ' " + GridView1.Rows[i].Cells[1].Text + " ' ";
ADOConnectionClass.ConnectingwithoutReturn(sqlcmd);
sqlcmd.Dispose();
}
catch (Exception ex)


{
sqlcmd.Dispose();
}
}
}

Page_Load里的代码是这样写的:

protected void Page_Load(object sender, EventArgs e)
{
if (inSession() != true)
{
Response.Redirect( "index.aspx ");
}
else
{
GV();
GridView1.databind();
}
}

结果就是只能显示出这个checkbox的框但是不能得到值


[解决办法]
if(!IsPostBack)
{
GV();
GridView1.databind();
}

这样写

[解决办法]
示列代码:
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID= "checkSelect " runat= "server " />
<asp:Label ID= "lblId " runat= "server " Text= ' <%# Bind( "ProductId ") %> ' Visible= "False "> </asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign= "Center " />
</asp:TemplateField>
放一个隐藏Label记录值
foreach (GridViewRow row in this.gvProductsList.Rows)
{
CheckBox checkItem = (CheckBox)row.FindControl( "checkSelect ");
Label lblId = (Label)row.FindControl( "lblId ");
if (checkItem.Checked == true)
{
string productId = lblId.Text;
DataTable dtProduct = DaoFactory.CreateProductDao().GetByProductId(productId, DaoConst.isDeleteFalse);
if (dtProduct.Rows.Count > 0)
{
DataTable dtProductType = DaoFactory.CreateProductTypeDao().GetByProductTypeId(dtProduct.Rows[0][ "ProductTypeId "].ToString());
if (dtProductType.Rows.Count > 0)
{
ProductType productType = new ProductType(dtProductType.Rows[0]);
productType.ProductTypeNum = (int)dtProductType.Rows[0][ "ProductTypeNum "] - 1;
DaoFactory.CreateProductTypeDao().update(productType);
}
}
ProductDao productDao = DaoFactory.CreateProductDao();
productDao.UpdateByProductId(productId, DaoConst.isDeleteTrue);


}
}

[解决办法]
支持2楼..

if(!IsPostBack)
...
[解决办法]
好像没问题哦! 关注中
[解决办法]
if(!IsPostBack)
{
if (inSession() != true)
{
Response.Redirect( "index.aspx ");
}
else
{
GV();
GridView1.databind();
}
}

这个试了吗。
[解决办法]
学习
[解决办法]
if(!IsPostBack)
{
if (inSession() != true)
{
Response.Redirect( "index.aspx ");
}
else
{
GV();
GridView1.databind();
}
}
这样写不可取,如果Session丢失,将无法判断

读书人网 >asp.net

热点推荐