GridView 删除/更新/取消/

本文介绍了一个基于ASP.NET的示例,展示了如何从XML文件加载数据并使用这些数据进行SQL查询来填充GridView控件。此外,还详细解释了如何实现编辑、取消编辑、更新、删除等功能,并通过事件处理来改变控件的可见性和状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//数据绑定 

private void InitData()
    {
 string sqlselect = "";
 XmlDocument document = new XmlDocument();
 document.Load(Server.MapPath("BannerList.xml"));
 XmlNodeList xn = document.SelectNodes("BannerList/item");
 string selch = "";selch = ddlchannel.SelectedItem.Value;
 if ( ddlchannel.SelectedItem.Value != "-1")
 {
 sqlselect = "select picid,picdiscription,piclink,picname,uploaddatetime,picchannel,picorder,isenjoin,banner,bannercode from flash where bannercode ='" + selch + " ' order by banner,picorder asc,picid desc";
 }
 else
 {
 sqlselect = "select picid,picdiscription,piclink,picname,uploaddatetime,picchannel,picorder,isenjoin,banner,bannercode from flash order by banner,picorder asc,picid desc";
 }
 string sqlselectnum = "select * from FlashItemNum";
 using (scn = new SqlConnection())
     {
  scn.ConnectionString = connectionstring;
  scn.Open();
  using ( sda = new SqlDataAdapter())
      {
   sda.SelectCommand = scn.CreateCommand();
   sda.SelectCommand.CommandType = CommandType.Text;
   sda.SelectCommand.CommandText = sqlselect;
   sda.SelectCommand.ExecuteNonQuery();
   ds = new DataSet();
   sda.Fill(ds,"list");
   gvFlashList.DataKeyNames = new string[] { "picid" };
   gvFlashList.DataSource = ds.Tables["list"];
   gvFlashList.DataBind();
   Label1.Text = "共" + ds.Tables[0].Rows.Count.ToString()+ "条,共" + gvFlashList.PageCount.ToString() + "页,当前第"+Convert.ToString(gvFlashList.PageIndex+1)+"页";
   for ( int j=0; j<gvFlashList.Rows.Count; j++)
       {   
    for (int i = 0; i <= 50; i++)
    ((DropDownList)gvFlashList.Rows[j].FindControl("ddlorder")).Items.Add(new ListItem(i.ToString(), i.ToString()));
    ((DropDownList)gvFlashList.Rows[j].FindControl("ddlorder")).SelectedIndex = Convert.ToInt32(ds.Tables[0].Rows[j]["picorder"]);
    ((DropDownList)gvFlashList.Rows[j].FindControl("ddlorder")).Visible = false;
    for ( int k =0; k< xn.Count; k++)
    {
        ((System.Web.UI.HtmlControls.HtmlSelect)gvFlashList.Rows[j].FindControl("selcode")).Items.Add(new ListItem(xn.Item(k).SelectSingleNode("option").InnerText, xn.Item(k).SelectSingleNode("option").Attributes["values"].Value.ToString()));
    }
    foreach ( ListItem li in ((System.Web.UI.HtmlControls.HtmlSelect)gvFlashList.Rows[j].FindControl("selcode")).Items)
        {
     if ( li.Value == ds.Tables[0].Rows[j]["bannercode"].ToString().Trim())
         {
      li.Selected = true;
      
         }
        }
    ((System.Web.UI.HtmlControls.HtmlSelect)gvFlashList.Rows[j].FindControl("selcode")).Visible = false;
    if (((LinkButton)gvFlashList.Rows[j].FindControl("lnkbtn")).Text == "启用")
        {
     gvFlashList.Rows[j].Attributes.Add("style","background:#aaaaaa;");
     ((LinkButton)gvFlashList.Rows[j].Cells[5].Controls[0]).Enabled = false;
        }
     }
      sda.SelectCommand.CommandText = sqlselectnum;
      sda.SelectCommand.ExecuteNonQuery();
      sda.Fill(ds,"num");
      itemnum.Value = ds.Tables["num"].Rows[0]["itemnum"].ToString();
  }
     }
    }

 

//编辑
    protected void gvFlashList_RowEditing(object sender, GridViewEditEventArgs e)
    {
     gvFlashList.EditIndex = e.NewEditIndex;
     InitData();
     ((DropDownList)gvFlashList.Rows[e.NewEditIndex].FindControl("ddlorder")).Visible = true;
     ((System.Web.UI.HtmlControls.HtmlGenericControl)gvFlashList.Rows[e.NewEditIndex].FindControl("spanorderid")).Visible = false;
     ((LinkButton)gvFlashList.Rows[e.NewEditIndex].FindControl("lnkbtn")).Visible = false;
     ((System.Web.UI.HtmlControls.HtmlSelect)gvFlashList.Rows[e.NewEditIndex].FindControl("selcode")).Visible = true;
     ((System.Web.UI.HtmlControls.HtmlGenericControl)gvFlashList.Rows[e.NewEditIndex].FindControl("lblcode")).Visible = false;

    }

 

//取消编辑
    protected void gvFlashList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
 gvFlashList.EditIndex = -1;
 InitData();
 ((DropDownList)gvFlashList.Rows[e.RowIndex].FindControl("ddlorder")).Visible = false;
 ((System.Web.UI.HtmlControls.HtmlGenericControl)gvFlashList.Rows[e.RowIndex].FindControl("spanorderid")).Visible = true;
 ((LinkButton)gvFlashList.Rows[e.RowIndex].FindControl("lnkbtn")).Visible = true;
 ((System.Web.UI.HtmlControls.HtmlSelect)gvFlashList.Rows[e.RowIndex].FindControl("selcode")).Visible = false;
 ((System.Web.UI.HtmlControls.HtmlGenericControl)gvFlashList.Rows[e.RowIndex].FindControl("lblcode")).Visible = true;

    }

 

//更新
    protected void gvFlashList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
 string sqlupdate = "update flash_upload set picdiscription='" + ((TextBox)gvFlashList.Rows[e.RowIndex].Cells[1].Controls[0]).Text + "',piclink='" + ((TextBox)gvFlashList.Rows[e.RowIndex].Cells[2].Controls[0]).Text + "',picorder=" + ((DropDownList)gvFlashList.Rows[e.RowIndex].FindControl("ddlorder")).Items[((DropDownList)gvFlashList.Rows[e.RowIndex].FindControl("ddlorder")).SelectedIndex].Value + ",banner='" + ((System.Web.UI.HtmlControls.HtmlSelect)gvFlashList.Rows[e.RowIndex].FindControl("selcode")).Value.ToString() + "' where picid=" + gvFlashList.DataKeys[e.RowIndex].Value;
 int updnum;
 using ( scn = new SqlConnection())
     {
  scn.ConnectionString = connectionstring;
  scn.Open();
  using ( sda = new SqlDataAdapter())
  {
      sda.UpdateCommand = scn.CreateCommand();
      sda.UpdateCommand.CommandType = CommandType.Text;
      sda.UpdateCommand.CommandText = sqlupdate;
      updnum = sda.UpdateCommand.ExecuteNonQuery();
  }
     }
 if ( updnum > 0)
 {
 gvFlashList.EditIndex = -1;
 Page.ClientScript.RegisterStartupScript(Page.GetType(),"","<script>alert(/"更新成功!/");</script>");
 }
 else
 {
     Page.ClientScript.RegisterStartupScript(Page.GetType(),"","<script>alert(/"更新失败!/");</script>");
 }
 InitData();
    }

 

//删除
    protected void gvFlashList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
 string sqldelete = "delete from flash_upload  where picid=" + gvFlashList.DataKeys[e.RowIndex].Value;
 int delnum;
 using (scn = new SqlConnection())
 {
     scn.ConnectionString = connectionstring;
     scn.Open();
     using (sda = new SqlDataAdapter())
     {
  sda.SelectCommand = scn.CreateCommand();
  sda.SelectCommand.CommandType = CommandType.Text;
  sda.SelectCommand.CommandText = sqldelete;
  delnum = sda.SelectCommand.ExecuteNonQuery();
     }
 }
 if (delnum > 0)
 {
     Page.ClientScript.RegisterStartupScript(Page.GetType(),"","<script>alert(/"删除成功!/");</script>");
 }
 InitData();
 
    }

 

//数据绑定
    protected void gvFlashList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
 if (e.Row.RowType == DataControlRowType.DataRow)
     {
  if (e.Row.RowState == DataControlRowState.Alternate || e.Row.RowState == DataControlRowState.Normal)
      {
   ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick","javascript:return confirm(/"您确定要删除“" + e.Row.Cells[1].Text  + "”吗?/");");
   LinkButton lnkb = new LinkButton();
   lnkb = ((LinkButton)e.Row.Cells[7].FindControl("lnkbtn"));
   lnkb.CommandArgument = e.Row.RowIndex.ToString();
      }
     }
    }

 

//命令
    protected void gvFlashList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
 if (e.CommandName == "lnkbtnenjone")
 {
     string btnname = ((LinkButton)gvFlashList.Rows[Convert.ToInt32(e.CommandArgument)].Cells[7].FindControl("lnkbtn")).Text;
     int bitnum = 0;
     if ( btnname == "启用")
     {
  bitnum = 1;
     }
     string strupdateenjone = "update flash_upload set isenjoin=" + bitnum.ToString() +" where picid=" + gvFlashList.DataKeys[Convert.ToInt32(e.CommandArgument)].Value;
     using (scn = new SqlConnection())
     {
  scn.ConnectionString = connectionstring;
  scn.Open();
  using (sda = new SqlDataAdapter())
  {
      sda.UpdateCommand = scn.CreateCommand();
      sda.UpdateCommand.CommandType = CommandType.Text;
      sda.UpdateCommand.CommandText = strupdateenjone;
      sda.UpdateCommand.ExecuteNonQuery();
  }
     }
InitData();
 }
 
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值