首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

#GridView嵌套棘手有关问题# 高手帮忙~

2012-02-19 
#######GridView嵌套棘手问题####### 高手帮忙~~~~~~~我现在做一个GridView嵌套的东西--(图见附件)绑定外

#######GridView嵌套棘手问题####### 高手帮忙~~~~~~~
我现在做一个GridView嵌套的东西--(图见附件)
绑定外面的GridView1很容易 直接写一个绑定方法一调用就可以了
但是里面嵌套的GridView2需要通过得到外面GridView1的主键值做为sql语句的where条件(我想我说的能让你明白)
protected void Page_Load(object sender, EventArgs e)
  {
  Page.SmartNavigation = true;
  if (!IsPostBack)
  {
  Bind();
  }
  }

  #region//公用方法
  public void conString()
  {
  string connstr = System.Configuration.ConfigurationSettings.AppSettings["HotelConString"];
  con = new SqlConnection(connstr);
  }
  #endregion

  #region//页面加载时绑定
  public void Bind()
  {
  conString();
  string sql = "select * from NullRoomInfo where i_reserve_del = 1";
  da = new SqlDataAdapter(sql,con);
  ds = new DataSet();
  da.Fill(ds);
  GridView1.DataSource = ds;
  GridView1.DataBind();
  okEnable();
  DRoomBind();
  RoomTypeBind();

  for (int i = 0; i < GridView1.Rows.Count; i++)
  {
  GridView1.Rows[i].Cells[5].FindControl("GridView2").Visible = false;
  }
  }
  #endregion

#region//页面加载时绑定,如果房间数大于1则显示"+",否则显示"-"
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
  Button delDel = (Button)e.Row.Cells[8].FindControl("delBtn");
  //添加GridView1的删除按钮提示
  delDel.Attributes.Add("onclick", "javascript:return confirm('是否删除订房人信息,请注意,如果删除,这个人订的所有房间都会被删除!')");
  //加号时变成减号,减号时变成加号
  Label lblnum = (Label)e.Row.Cells[5].FindControl("lblrc");
  ImageButton img = (ImageButton)e.Row.Cells[5].FindControl("imgSign");
  if (Convert.ToInt32(lblnum.Text) >= 1)
  {
  img.ImageUrl = @"\Hotel\style\img\add.gif";
  }
  //*****************************************************************************
  //绑定嵌套的GridView2
  GridView gvin = (GridView)e.Row.Cells[5].FindControl("GridView2");
  int idin = Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex].Value.ToString());
  string sqlin = "select * from hotel_DroomInfo where i_reserve_id = '" + idin + "'";
  conString();
  da = new SqlDataAdapter(sqlin, con);
  ds = new DataSet();
  da.Fill(ds);
  gvin.DataSource = ds;
  gvin.DataBind();
  }
  }
  #endregion

现在这样已经把GridView1和GridView2都已经绑定好了
但是后面要做一个GridView2(里面被嵌套的GridView)的模板列删除操作
删除后不能像GridView1一样调用自己的Bind()方法,只能通过进入GridView2_RowDataBound()事件进行绑定,这样删除GridView2中数据后页面才回重新绑定(我想做个绑定操作的朋友都应该能理解我的一样),
现在我已经把GridView2_RowDataBound()事件中的代码写好了:
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
  e.Row.Cells[3].Attributes.Add("onclick", "javascript:return confirm('是否删除订房人信息?')");
  GridView Gv2 = (GridView)e.Row.Parent.Parent;
  GridViewRow gvr = (GridViewRow)Gv2.NamingContainer;
  //得到触发源所在GridView1行的ID
  int id = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex].Value.ToString());


  string sql = "select * from hotel_DroomInfo where i_reserve_id = '" + id + "'";
  conString();
  da = new SqlDataAdapter(sql, con);
  ds = new DataSet();
  da.Fill(ds);
  Gv2.DataSource = ds;
  Gv2.DataBind();
  }
  }
先通过得到找外面GridView1的每行主键值,这样里面的sql语句就有where条件了。
当我加断点的时候他会进入断点,而且我看取到的值都是正确的,但进入页面后,并没有绑定上。
把GridView1_RowDataBound()事件中绑定GridView2的代码注释后,留下GridView2_RowDataBound()中绑定代码,结果什么都没有绑定上

要是GridView1_RowDataBound和GridView2_RowDataBound中绑定代码都留着的话,页面加载时提示该页无法显示,郁闷了。。

[解决办法]


 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gv = (GridView)e.Row.FindControl("GridViewSpendDetail");

gv.DataSource = GetDetailInfo(Int32.Parse(e.Row.Cells[0].Text));
gv.DataBind();



}

}

GridView2_RowDataBound事件 不用写获取信息的内容。


[解决办法]
把GRIDVIEW2 做成用户控件试试

热点排行