- GridView的用法
- 1.GridView的分页:
- 首先设置AllowPaging="True",然后触发事件
- protected void gvProduct_PageIndexChanging(object sender,GridViewPageEventArgs e)
- {
- gvProduct.PageIndex = e.NewPageIndex;
- //GridView绑定
- DisplayContent(OrderType);
- }
- 2. //单击编辑
- protected void gvProduct_RowEditing(object sender, GridViewEditEventArgs e)
- {
- gvProduct.EditIndex = e.NewEditIndex;
- //GridView绑定
- DisplayContent(OrderType);
- }
- 3 //点击取消
- protected void gvProduct_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
- {
- gvProduct.EditIndex = -1;
- //GridView绑定
- DisplayContent(OrderType);
- }
- 4 //点击更新
- 取值有两种方法
- Station = ((TextBox)gvProduct.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString().Trim();
- Station = gvProduct.DataKeys[e.Row.RowIndex]["Pricetype"]
- 这个时候要注意设置DataKeyNames的属性
- protected void gvProduct_RowUpdating(object sender, GridViewUpdateEventArgs e)
- {
- DataSet ds;
- //广告ID
- int AdId;
- string Station, Size, PriceType, StartTime, EndTime, Remark, ADKind, ResultCode, ResultMsg;
- double Price;
- try
- {
- AdId = Convert.ToInt32(((TextBox)gvProduct.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString());
- //AdId = Convert.ToInt32(gvProduct.DataKeys[e.RowIndex]["AdvertiseId"].ToString());
- }
- catch
- {
- ScriptManager.RegisterClientScriptBlock(UpdatePanel3, this.GetType(), "click1", "alert('数据类型转换错误')", true);
- return;
- }
- //广告位置
- Station = ((TextBox)gvProduct.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString().Trim();
- if (Station.Length == 0)
- {
- ScriptManager.RegisterClientScriptBlock(UpdatePanel3, this.GetType(), "click", "alert('广告位置不可以为空')", true);
- return;
- }
- //广告大小
- Size = ((TextBox)gvProduct.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString().Trim();
- if (Size.Length == 0)
- {
- ScriptManager.RegisterClientScriptBlock(UpdatePanel3, this.GetType(), "click", "alert('广告大小不可以为空')", true);
- return;
- }
- //广告种类
- ADKind = ((DropDownList)(gvProduct.Rows[e.RowIndex].Cells[1].FindControl("ddlAdvertiseType"))).SelectedValue;
- //收费类型
- PriceType = ((DropDownList)(gvProduct.Rows[e.RowIndex].Cells[4].FindControl("ddlPriceTypeTwo"))).SelectedValue;
- //收费价格
- try
- {
- Price = Convert.ToDouble(((TextBox)gvProduct.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString().Trim());
- }
- catch
- {
- ScriptManager.RegisterClientScriptBlock(UpdatePanel3, this.GetType(), "click", "alert('价格转换失败,格式为:100.00')", true);
- return;
- }
- //开始时间
- StartTime = ((TextBox)gvProduct.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString().Trim();
- if (StartTime.Length != 8)
- {
- ScriptManager.RegisterClientScriptBlock(UpdatePanel3, this.GetType(), "click", "alert('时间格式为:20080101')", true);
- return;
- }
- //结束时间
- EndTime = ((TextBox)gvProduct.Rows[e.RowIndex].Cells[7].Controls[0]).Text.ToString().Trim();
- if (EndTime.Length != 8)
- {
- ScriptManager.RegisterClientScriptBlock(UpdatePanel3, this.GetType(), "click", "alert('时间格式为:20080101')", true);
- return;
- }
- Remark = txtBeizhu.Text.Trim();
- vmc = new VIPManagerClass(connection());
- vmc.update_AdvertiseOrder(AdId, OrderID, ADKind, Station, Size, "", PriceType, Price, StartTime, EndTime, Remark, out ds);
- ResultCode = Convert.ToString(ds.Tables[0].Rows[0][0]);
- ResultMsg = Convert.ToString(ds.Tables[0].Rows[0][1]);
- if (ResultCode == "0")
- {
- gvProduct.EditIndex = -1;
- DisplayContent(OrderType);
- }
- else
- {
- ScriptManager.RegisterClientScriptBlock(UpdatePanel3, this.GetType(), "click", "alert('" + ResultMsg + "')", true);
- }
- }
- 4.gvProduct_RowDataBound设置样式绑定,弹出对话框
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- string AdvertiseId = ((GridView)sender).DataKeys[e.Row.RowIndex]["AdvertiseId"].ToString();
- ((HyperLink)(e.Row.Cells[9].Controls[0])).Attributes.Add("onclick", "OpenDialo('ViewRemark.aspx?AdvertiseId=" + AdvertiseId + "&type=" + type + "','ViewRemark',600,350)");
- if (type == "0")
- {
- ((LinkButton)(e.Row.Cells[8].Controls[0])).Enabled = false;
- }
- DropDownList ADType = (DropDownList)e.Row.Cells[1].FindControl("ddlAdvertiseType");
- DropDownList PriceType = (DropDownList)e.Row.Cells[4].FindControl("ddlPriceTypeTwo");
- ddlDictBind("Djglx", PriceType);
- ddlDictBind("Dgglx", ADType);
- try
- {
- ADType.SelectedValue = ((GridView)sender).DataKeys[e.Row.RowIndex]["Kind"].ToString();
- }
- catch
- {
- ADType.SelectedValue = "";
- }
- try
- {
- PriceType.SelectedValue = ((GridView)sender).DataKeys[e.Row.RowIndex]["Pricetype"].ToString();
- }
- catch
- {
- PriceType.SelectedValue = "";
- }
- }
- if ((e.Row.RowState & DataControlRowState.Edit) != 0)
- {
- ((TextBox)e.Row.Cells[0].Controls[0]).ReadOnly = true;
- ((TextBox)e.Row.Cells[0].Controls[0]).Width = 24;
- ((TextBox)e.Row.Cells[0].Controls[0]).CssClass = "tbOper";
- ((DropDownList)e.Row.Cells[1].FindControl("ddlAdvertiseType")).Width = 94;
- ((DropDownList)e.Row.Cells[1].FindControl("ddlAdvertiseType")).CssClass = "ddlcss";
- ((TextBox)e.Row.Cells[2].Controls[0]).Width = 64;
- ((TextBox)e.Row.Cells[2].Controls[0]).CssClass = "tbOper";
- ((TextBox)e.Row.Cells[3].Controls[0]).Width = 64;
- ((TextBox)e.Row.Cells[3].Controls[0]).CssClass = "tbOper";
- ((DropDownList)e.Row.Cells[4].FindControl("ddlPriceTypeTwo")).Width = 94;
- ((DropDownList)e.Row.Cells[4].FindControl("ddlPriceTypeTwo")).CssClass = "ddlcss";
- ((TextBox)e.Row.Cells[5].Controls[0]).Width = 64;
- ((TextBox)e.Row.Cells[5].Controls[0]).CssClass = "tbOper";
- ((TextBox)e.Row.Cells[6].Controls[0]).Width = 60;
- ((TextBox)e.Row.Cells[6].Controls[0]).MaxLength = 8;
- ((TextBox)e.Row.Cells[6].Controls[0]).CssClass = "tbOper";
- ((TextBox)e.Row.Cells[7].Controls[0]).Width = 60;
- ((TextBox)e.Row.Cells[7].Controls[0]).MaxLength = 8;
- ((TextBox)e.Row.Cells[7].Controls[0]).CssClass = "tbOper";
- }
GridView的用法详解
最新推荐文章于 2021-09-01 18:20:48 发布