GridView的用法详解

  1. GridView的用法
  2. 1.GridView的分页:
  3. 首先设置AllowPaging="True",然后触发事件
  4. protected void gvProduct_PageIndexChanging(object sender,GridViewPageEventArgs e)
  5.     {
  6.         gvProduct.PageIndex = e.NewPageIndex;
  7.         //GridView绑定
  8.         DisplayContent(OrderType);
  9.     }
  10. 2. //单击编辑
  11.     protected void gvProduct_RowEditing(object sender, GridViewEditEventArgs e)
  12.     {
  13.         gvProduct.EditIndex = e.NewEditIndex;
  14.         //GridView绑定
  15.         DisplayContent(OrderType);
  16.     }
  17. 3  //点击取消
  18.     protected void gvProduct_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  19.     {
  20.         gvProduct.EditIndex = -1;
  21.         //GridView绑定
  22.         DisplayContent(OrderType);
  23.     }
  24. 4  //点击更新
  25. 取值有两种方法
  26. Station = ((TextBox)gvProduct.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString().Trim();
  27. Station = gvProduct.DataKeys[e.Row.RowIndex]["Pricetype"]
  28. 这个时候要注意设置DataKeyNames的属性
  29.     protected void gvProduct_RowUpdating(object sender, GridViewUpdateEventArgs e)
  30.     {
  31.         DataSet ds;
  32.         //广告ID
  33.         int AdId;
  34.         string Station, Size, PriceType, StartTime, EndTime, Remark, ADKind, ResultCode, ResultMsg;
  35.         double Price;
  36.         try
  37.         {
  38.             AdId = Convert.ToInt32(((TextBox)gvProduct.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString());
  39.             //AdId = Convert.ToInt32(gvProduct.DataKeys[e.RowIndex]["AdvertiseId"].ToString());
  40.         }
  41.         catch
  42.         {
  43.             ScriptManager.RegisterClientScriptBlock(UpdatePanel3, this.GetType(), "click1""alert('数据类型转换错误')"true);
  44.             return;
  45.         }
  46.         //广告位置
  47.         Station = ((TextBox)gvProduct.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString().Trim();
  48.         if (Station.Length == 0)
  49.         {
  50.             ScriptManager.RegisterClientScriptBlock(UpdatePanel3, this.GetType(), "click""alert('广告位置不可以为空')"true);
  51.             return;
  52.         }
  53.         //广告大小
  54.         Size = ((TextBox)gvProduct.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString().Trim();
  55.         if (Size.Length == 0)
  56.         {
  57.             ScriptManager.RegisterClientScriptBlock(UpdatePanel3, this.GetType(), "click""alert('广告大小不可以为空')"true);
  58.             return;
  59.         }
  60.         //广告种类
  61.         ADKind = ((DropDownList)(gvProduct.Rows[e.RowIndex].Cells[1].FindControl("ddlAdvertiseType"))).SelectedValue;
  62.         //收费类型
  63.         PriceType = ((DropDownList)(gvProduct.Rows[e.RowIndex].Cells[4].FindControl("ddlPriceTypeTwo"))).SelectedValue;
  64.         //收费价格
  65.         try
  66.         {
  67.             Price = Convert.ToDouble(((TextBox)gvProduct.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString().Trim());
  68.         }
  69.         catch
  70.         {
  71.             ScriptManager.RegisterClientScriptBlock(UpdatePanel3, this.GetType(), "click""alert('价格转换失败,格式为:100.00')"true);
  72.             return;
  73.         }
  74.         //开始时间
  75.         StartTime = ((TextBox)gvProduct.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString().Trim();
  76.         if (StartTime.Length != 8)
  77.         {
  78.             ScriptManager.RegisterClientScriptBlock(UpdatePanel3, this.GetType(), "click""alert('时间格式为:20080101')"true);
  79.             return;
  80.         }
  81.         //结束时间
  82.         EndTime = ((TextBox)gvProduct.Rows[e.RowIndex].Cells[7].Controls[0]).Text.ToString().Trim();
  83.         if (EndTime.Length != 8)
  84.         {
  85.             ScriptManager.RegisterClientScriptBlock(UpdatePanel3, this.GetType(), "click""alert('时间格式为:20080101')"true);
  86.             return;
  87.         }
  88.         Remark = txtBeizhu.Text.Trim();
  89.         vmc = new VIPManagerClass(connection());
  90.         vmc.update_AdvertiseOrder(AdId, OrderID, ADKind, Station, Size, "", PriceType, Price, StartTime, EndTime, Remark, out ds);
  91.         ResultCode = Convert.ToString(ds.Tables[0].Rows[0][0]);
  92.         ResultMsg = Convert.ToString(ds.Tables[0].Rows[0][1]);
  93.         if (ResultCode == "0")
  94.         {
  95.             gvProduct.EditIndex = -1;
  96.             DisplayContent(OrderType);
  97.         }
  98.         else
  99.         {
  100.             ScriptManager.RegisterClientScriptBlock(UpdatePanel3, this.GetType(), "click""alert('" + ResultMsg + "')"true);
  101.         }
  102.     }
  103. 4.gvProduct_RowDataBound设置样式绑定,弹出对话框
  104. if (e.Row.RowType == DataControlRowType.DataRow)
  105.         {
  106.             string AdvertiseId = ((GridView)sender).DataKeys[e.Row.RowIndex]["AdvertiseId"].ToString();
  107.             ((HyperLink)(e.Row.Cells[9].Controls[0])).Attributes.Add("onclick""OpenDialo('ViewRemark.aspx?AdvertiseId=" + AdvertiseId + "&type=" + type + "','ViewRemark',600,350)");
  108.             if (type == "0")
  109.             {
  110.                 ((LinkButton)(e.Row.Cells[8].Controls[0])).Enabled = false;
  111.             }
  112.             DropDownList ADType = (DropDownList)e.Row.Cells[1].FindControl("ddlAdvertiseType");
  113.             DropDownList PriceType = (DropDownList)e.Row.Cells[4].FindControl("ddlPriceTypeTwo");
  114.             ddlDictBind("Djglx", PriceType);
  115.             ddlDictBind("Dgglx", ADType);
  116.             try
  117.             {
  118.                 ADType.SelectedValue = ((GridView)sender).DataKeys[e.Row.RowIndex]["Kind"].ToString();
  119.             }
  120.             catch
  121.             {
  122.                 ADType.SelectedValue = "";
  123.             }
  124.             try
  125.             {
  126.                 PriceType.SelectedValue = ((GridView)sender).DataKeys[e.Row.RowIndex]["Pricetype"].ToString();
  127.             }
  128.             catch
  129.             {
  130.                 PriceType.SelectedValue = "";
  131.             }
  132.         }
  133.         if ((e.Row.RowState & DataControlRowState.Edit) != 0)
  134.         {
  135.             ((TextBox)e.Row.Cells[0].Controls[0]).ReadOnly = true;
  136.             ((TextBox)e.Row.Cells[0].Controls[0]).Width = 24;
  137.             ((TextBox)e.Row.Cells[0].Controls[0]).CssClass = "tbOper";
  138.             ((DropDownList)e.Row.Cells[1].FindControl("ddlAdvertiseType")).Width = 94;
  139.             ((DropDownList)e.Row.Cells[1].FindControl("ddlAdvertiseType")).CssClass = "ddlcss";
  140.             ((TextBox)e.Row.Cells[2].Controls[0]).Width = 64;
  141.             ((TextBox)e.Row.Cells[2].Controls[0]).CssClass = "tbOper";
  142.             ((TextBox)e.Row.Cells[3].Controls[0]).Width = 64;
  143.             ((TextBox)e.Row.Cells[3].Controls[0]).CssClass = "tbOper";
  144.             ((DropDownList)e.Row.Cells[4].FindControl("ddlPriceTypeTwo")).Width = 94;
  145.             ((DropDownList)e.Row.Cells[4].FindControl("ddlPriceTypeTwo")).CssClass = "ddlcss";
  146.             ((TextBox)e.Row.Cells[5].Controls[0]).Width = 64;
  147.             ((TextBox)e.Row.Cells[5].Controls[0]).CssClass = "tbOper";
  148.             ((TextBox)e.Row.Cells[6].Controls[0]).Width = 60;
  149.             ((TextBox)e.Row.Cells[6].Controls[0]).MaxLength = 8;
  150.             ((TextBox)e.Row.Cells[6].Controls[0]).CssClass = "tbOper";
  151.             ((TextBox)e.Row.Cells[7].Controls[0]).Width = 60;
  152.             ((TextBox)e.Row.Cells[7].Controls[0]).MaxLength = 8;
  153.             ((TextBox)e.Row.Cells[7].Controls[0]).CssClass = "tbOper";
  154.         }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值