GridView实现删除、更新、取消
- //数据绑定
- 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();
- }
- }
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · The instance of entity type ‘Customer’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked.
- · .NET8实时更新nginx ip地址归属地
- · 解决.NET Blazor子组件不刷新问题
- · .NET8如何在普通类库中引用 Microsoft.AspNetCore
- · .NET8 Mysql SSL error
- · ASP.NET Core MVC的Razor视图渲染中文乱码的问题
- · .NETCORE 依赖注入服务生命周期
- · asp.net zero改mysql
- · .NET5面试汇总
- · .Net连接Mysql数据库的Convert Zero Datetime日期问题
- · vue使用element-ui中的Message 、MessageBox 、Notification
- · Asp.Net Core Filter 深入浅出的那些事-AOP