GridView如何更新批量数据和单条记录(downmoon)?

后台代码段

Members #region Members
protected GridViewGVList;
public string PreUrl = Agronet.Common.Framework.Util.Globals.MemberUserPath + " Product " + @" / " ;

protected Agronet.Common.Framework.Util.Pagerpager;
protected TextBoxtxtSearchTxt;
protected ButtonbtnSearch;
protected HyperLinkcmdAdd;
protected LinkButtoncmdDel;
protected LinkButtoncmdRecommend;
protected LinkButtoncmdUnPublish;
protected TextBoxtxtWords;
protected DropDownListddlType;
protected LabelTradeTypeID;
protected LabelNoResult;
private int _page = 1 ;
public int _sort = 0 ;
/**/ /// <summary>
/// 关键字
/// downmoon2007年12月12日
/// </summary>

private string _word
... {
get
... {
object o = ViewState[ " strWords " ];
return (o == null ) ? "" :o.ToString();
}

set
... {
ViewState[
" strWords " ] = value;
}

}


// 审核状态
public short CheckState
... {
get
... {
object o = ViewState[ " CheckState " ];
return SQLParser.ShortParse(o);
}

set ... {ViewState[ " CheckState " ] = value;}
}

#endregion

Events #region Events

protected override void OnLoad(EventArgse)
... {
base .OnLoad(e);
Check #region Check
CheckSiteUser();
#endregion


initPage #region initPage
_page
= SQLParser.IntParse(Page.Request.QueryString[ " page " ]);
_word
= SQLParser.StringParse(Page.Request.QueryString[ " word " ]);
CheckState
= SQLParser.ShortParse(Page.Request.QueryString[ " st " ]);

if (txtSearchTxt != null )
... {
txtSearchTxt.Attributes.Add(
" maxlength " , " 20 " );
txtSearchTxt.Text
= _word;
}

if ( this .ddlType != null )
... {
this .ddlType.SelectedValue = this .CheckState.ToString();
this .ddlType.SelectedIndexChanged += new EventHandler(ddlType_SelectedIndexChanged);

}

if (cmdAdd != null ) ... {cmdAdd.NavigateUrl = PreUrl + " Insert.aspx " ;}
if (cmdDel != null ) ... {cmdDel.Click += new EventHandler(cmdDel_Click);}
if (cmdRecommend != null ) ... {cmdRecommend.Click += new EventHandler(cmdRecommend_Click);}
if (cmdUnPublish != null ) ... {cmdUnPublish.Click += new EventHandler(cmdUnPublish_Click);}
pager.PageIndex
= _page;
pager.PageSize
= Agronet.Common.Framework.Util.Globals.PageSize;
if (GVList != null )
... {
GVList.RowDataBound
+= new GridViewRowEventHandler(GVList_RowDataBound);
GVList.RowCommand
+= new GridViewCommandEventHandler(GVList_RowCommand);
GVList.RowUpdating
+= new GridViewUpdateEventHandler(GVList_RowUpdating);
}

if (NoResult != null ) ... {NoResult.Text = GetCurStateByID(CheckState) + " 列表 " ;}
if (btnSearch != null ) ... {btnSearch.Click += new EventHandler(btnSearch_Click);}

#endregion

BindList #region BindList
if ( ! IsPostBack)
... {
BindList();
}

pager.UrlFormat
= PreUrl + " ?page={0}&sort=0&dvid=2&word= " + _word + " &st= " + CheckState.ToString();
#endregion

}

Serarch #region Serarch
private void btnSearch_Click( object sender,EventArgse)
... {
string strurl = PreUrl + " ?dvid=2&sort=0&st={0}&word={1} " ;
this .CheckState = SQLParser.ShortParse( this .ddlType.SelectedItem.Value);
strurl
= string .Format(strurl,CheckState.ToString(),txtSearchTxt.Text.Trim());
PageLocation(strurl);
}

#endregion

ddlType #region ddlType
private void ddlType_SelectedIndexChanged( object sender,EventArgse)
... {
if ( this .ddlType != null && ddlType.SelectedValue.Trim() != "" )
... {
this .CheckState = SQLParser.ShortParse( this .ddlType.SelectedItem.Value);
string strurl = PreUrl + " ?dvid=2&page=1&st= " + CheckState.ToString() + " &word= " + this .txtSearchTxt.Text.Trim();;
PageLocation(strurl);
}

}

#endregion

protected void cmdDel_Click( object sender,EventArgse)
... {
执行批量操作 #region 执行批量操作
UpdateMultiRecordsByWhere(pager.PageSize,
- 4 , 1 );
#endregion

}

private void cmdUnPublish_Click( object sender,EventArgse)
... {
执行批量操作 #region 执行批量操作
UpdateMultiRecordsByWhere(pager.PageSize,
- 2 , 1 );
#endregion
;
}

private void GVList_RowCommand( object sender,GridViewCommandEventArgse)
... {
执行批量操作 #region 执行批量操作
if (e.CommandName == " Update " )
... {
try
... {
int days = 0 ;
if (muser.ClientClass > 0 ) ... {days = Globals.ProductinfoUpdatePerDays;}
long pid = SQLParser.LongParse(GVList.DataKeys[SQLParser.IntParse(e.CommandArgument)].Value);
// GridViewa=(GridView)sender;
if (pid > 0 )
... {
int retValue = Agronet.Product.Framework.Data.DataDTOProvider.ProducyUpdateModifyTime(pid,days,muser.LoginID);
switch (retValue)
... {
case - 1 :
Debug(
" 产品更新失败,原因:产品未通过审核! " , " Error " );
break ;
case 0 :
Debug(
" 产品更新失败,原因:未知! " , " Error " );
break ;
case 1 :
Index #region Index
Debug(
" 产品更新成功! " , " Succeed " );
#endregion

break ;
case 2 :
Debug(
" 尊敬用户 " + user.LoginID + @" 您好, 您选择的产品暂时还不能被更新,原因:单位时间内更新过与频繁,二十分钟之内不能连续更新! " , " Error " );
break ;
default :
break ;
}

}

}

catch (Exceptionex)
... {
return ;
}

}

#endregion

}

private void GVList_RowUpdating( object sender,GridViewUpdateEventArgse)
... {
// BindList();
}

private void cmdRecommend_Click( object sender,EventArgse)
... {
执行批量操作 #region 执行批量操作
UpdateMultiRecordsByWhere(pager.PageSize,
1 , 2 );
#endregion


}

protected void GVList_RowDataBound( object sender,GridViewRowEventArgse)
... {
if (e.Row.RowType == DataControlRowType.DataRow)
... {
HyperLinkcmdMod
= (HyperLink)e.Row.FindControl( " cmdMod " );
if (cmdMod != null ) ... {cmdMod.NavigateUrl = PreUrl + " Modify.aspx?dvid=2&PKID= " + DataBinder.Eval(e.Row.DataItem, " P_ID " );}
LinkButtonlbUpdate
= (LinkButton)e.Row.FindControl( " cmdUpdate " );
if (lbUpdate != null ) ... {lbUpdate.CommandArgument = e.Row.RowIndex.ToString();}
}

}

#endregion

Methods #region Methods
BindList #region BindList

private void BindList()
... {
string strWhere = string .Empty;
strWhere
= " (LoginID=' " + muser.LoginID + " ') " ;
Agronet.Product.Framework.Components.ProductCollectionsc
= Agronet.Product.Framework.Data.DataDTOProvider.GetProductReturnCollection(_word.Trim(),strWhere,pager.PageIndex,pager.PageSize,_sort,CheckState);
this .GVList.DataSource = sc;
this .GVList.DataBind();
if (sc.MaxItems > 0 )
... {pager.ItemCount = sc.MaxItems;}
else
... {
pager.Visible
= false ;
this .NoResult.Text = " 无符合条件的记录! " ;
}


}

MultipleRecordUpdateMethod #region MultipleRecordUpdateMethod
private void UpdateMultiRecordsByWhere( int mCount, short mState, short updateType)
... {
Check #region Check
CheckSiteUser();
#endregion

执行批量操作 #region 执行批量操作
try
... {
StringBuildersb
= new StringBuilder();
string strTemp = string .Empty;
string PKName = string .Empty;
int tempCount = 0 ;
if (GVList == null || GVList.Rows.Count == 0 ) ... { return ;}
PKName
= GVList.DataKeyNames[ 0 ].ToString();
sb.Append(
" where " );
sb.Append(
" LoginID=' " );
sb.Append(muser.LoginID);
sb.Append(
" 'AND " );
sb.Append(PKName);
sb.Append(
" in( " );
for ( int i = 0 ;i < GVList.Rows.Count;i ++ )
... {
CheckBoxchk
= (CheckBox)GVList.Rows[i].FindControl( " chkDelServer " );
if (chk != null && chk.Checked)
... {
sb.Append(GVList.DataKeys[i].Value.ToString());
sb.Append(
" , " );
tempCount
++ ;
}

}

if (tempCount == 0 ) return ;
if (tempCount > mCount) ... {Debug( " 超出批量操作的最大允许数量! " , this .ClientID, true ); return ;}
strTemp
= sb.ToString();
if (strTemp.EndsWith( " , " )) ... {strTemp = strTemp.Remove(strTemp.Length - 1 );}
strTemp
+= " ) " ;
if (updateType == 1 )
... {
Agronet.Product.Framework.Data.DataDTOProvider.ProductUpdateStatusByWhere(strTemp,mState,muser.LoginID);
}

else
... {
Agronet.Product.Framework.Data.DataDTOProvider.ProductUpdateRecommendStatusByWhere(strTemp,mState,muser.LoginID);
}

BindList();
}

catch (Exceptionex)
... {
Debug(ex.Message,
this .ClientID, true );
return ;
}

#endregion

}


#endregion

#endregion

#endregion


前台html

< script language ="JavaScript" > ...

<!--
function CheckAll(val)
... {
for ( var i = 0 ;i < document.forms[ 0 ].elements.length;i ++ )
... {
var e = document.forms[ 0 ].elements[i];
if (e.Type = " checkbox " && e.name != " _ctl2:_ctl0:radioSortDesc " )
... {
e.checked
= val;
}

}

}

// -->

</ script >
< table width ="95%" border ="0" cellpadding ="0" cellspacing ="0" align ="center" >
< tr >
< td height ="25" align ="right" bgcolor ="#EFEFEF" class ="tab_font_right" >
< strong > 产品搜索 </ strong >
< asp:TextBox ID ="txtSearchTxt" runat ="server" />
< asp:Button ID ="btnSearch" runat ="server" Text ="搜索" ></ asp:Button >
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 转到
< asp:DropDownList ID ="ddlType" runat ="server" CssClass ="maintext" AutoPostBack ="True" >
< asp:ListItem Value ="10000" > 所有产品 </ asp:ListItem >
< asp:ListItem Value ="2" > 已推荐 </ asp:ListItem >
< asp:ListItem Value ="1" > 当前发布 </ asp:ListItem >
< asp:ListItem Value ="0" > 等待审核 </ asp:ListItem >
< asp:ListItem Value ="-1" > 未通过审核 </ asp:ListItem >
< asp:ListItem Value ="-2" > 未发布 </ asp:ListItem >
</ asp:DropDownList >
</ td >
</ tr >
</ table >
< table width ="95%" border ="0" align ="center" cellpadding ="0" cellspacing ="1" >
< tr >
< td >
< asp:GridView ID ="GVList" runat ="server" ShowFooter ="true" AutoGenerateColumns ="False"
BorderStyle
="Solid" BorderColor ="#ffffff" GridLines ="Horizontal" CellSpacing ="1"
Width
="100%" HorizontalAlign ="NotSet" BorderWidth ="0px" EnableViewState ="true"
DataKeyNames
="P_ID" >
< HeaderStyle CssClass ="InfoHeaderText" ForeColor ="#000000" HorizontalAlign ="center"
Height
="22px" />
< AlternatingRowStyle BackColor ="#f7f7f7" />
< RowStyle HorizontalAlign ="center" Height ="20px" BackColor ="#E3EAEB" />
< SelectedRowStyle BackColor ="#C5BBAF" Font-Bold ="True" ForeColor ="#333333" />
< Columns >
< asp:TemplateField >
< HeaderStyle Width ="60px" BackColor ="#1C5E55" ForeColor ="White" />
< HeaderTemplate >
编号
</ HeaderTemplate >
< ItemTemplate >
< asp:Label ID ="PKID" Text ='<%# DataBinder.Eval(Container.DataItem,"P_ID")% > 'runat="server"/>
</ ItemTemplate >
</ asp:TemplateField >
< asp:TemplateField Visible ="False" >
< HeaderTemplate >
< asp:Label ID ="Image_Head" runat ="server" Text ="产品图片" />
</ HeaderTemplate >
< HeaderStyle Width ="120" />
< ItemTemplate >
< asp:HyperLink ID ="P_SmallImage" runat ="server" />
</ ItemTemplate >
</ asp:TemplateField >
< asp:TemplateField >
< HeaderTemplate >
&nbsp; 产品名称 </ HeaderTemplate >
< ItemStyle HorizontalAlign ="left" />
< HeaderStyle HorizontalAlign ="Left" />
< ItemTemplate >
&nbsp; · < asp:Label ID ="P_Name" Text ='<%# DataBinder.Eval(Container.DataItem,"P_Name")% > '
runat="Server"/>
< img src ='<%# "/Resource/images/Check"+DataBinder.Eval(Container,"DataItem.CurState")+".gif"% > '/> </ ItemTemplate >
</ asp:TemplateField >
< asp:BoundField DataField ="ModTime" HeaderText ="修改时间" DataFormatString ="{0:yyyy-MM-ddHH:mm}"
HtmlEncode
="false" />
< asp:BoundField DataField ="ModUser" HeaderText ="修改者" />
< asp:TemplateField >
< HeaderTemplate >
状态
</ HeaderTemplate >
< ItemTemplate >
< asp:Label ID ="CurState" Text ='<%# GetCurStateByID(DataBinder.Eval(Container.DataItem,"CurState"))% > '
runat="Server"/>
</ ItemTemplate >
</ asp:TemplateField >
< asp:TemplateField >
< HeaderStyle Width ="60px" />
< HeaderTemplate >
编辑
</ HeaderTemplate >
< ItemTemplate >
< asp:HyperLink ID ="cmdMod" CssClass ="ElementNavigation" Text ="编辑" runat ="server" />
</ ItemTemplate >
</ asp:TemplateField >
< asp:TemplateField >
< HeaderStyle Width ="60px" />
< HeaderTemplate >
更新
</ HeaderTemplate >
< ItemTemplate >
< asp:LinkButton ID ="cmdUpdate" CommandName ="Update" CssClass ="ElementNavigation" Text ="快速更新" runat ="server" OnClientClick ="returnconfirm('您确认快速更新该记录吗?');" />
</ ItemTemplate >
</ asp:TemplateField >
< asp:TemplateField >
< HeaderStyle Width ="40px" />
< HeaderTemplate >
< input type ="checkbox" name ="allbox" value ="CheckAll" onclick ="CheckAll(allbox.checked);" />
</ HeaderTemplate >
< ItemTemplate >
< asp:CheckBox ID ="chkDelServer" runat ="server" />
</ ItemTemplate >
</ asp:TemplateField >
</ Columns >
</ asp:GridView >
</ td >
</ tr >
< tr >
< td align ="right" >
< asp:HyperLink ID ="cmdAdd" ForeColor ="Red" Text ="新增" CssClass ="ElementNavigation"
runat
="server" />
&nbsp;&nbsp; < asp:LinkButton ID ="cmdRecommend" runat ="server" CssClass ="ElementNavigation"
Text
="推荐" CommandName ="ReCommend" CausesValidation ="false" OnClientClick ="javascript:returnconfirm('确定推荐或(取消推荐)已选择的数据吗?')" ></ asp:LinkButton >
&nbsp;&nbsp; < asp:LinkButton ID ="cmdUnPublish" runat ="server" CssClass ="ElementNavigation"
Text
="取消发布" CommandName ="UnPublish" CausesValidation ="false" OnClientClick ="javascript:returnconfirm('确定取消发布已选择的数据吗?')" ></ asp:LinkButton >
&nbsp;&nbsp; < asp:LinkButton ID ="cmdDel" ForeColor ="Red" Text ="删除" CssClass ="ElementNavigation"
CausesValidation
="false" runat ="server" CommandName ="Delete" OnClientClick ="javascript:returnconfirm('确定删除已选择的数据吗?')" />
</ td >
</ tr >
< tr >
< td >
< Agronet:Pager ID ="pager" runat ="server" CssClass ="Pager" LinkFormatActive ='<a href ="{0}" class ="Current" > {1} </ a > '
PrefixText="页码:"UseSpacer="False"/>
</ td >
</ tr >
</ table >


值处注意的是:
必须添加这个事件,即使它什么也不干

private void GVList_RowUpdating( object sender,GridViewUpdateEventArgse)
... {
// BindList();
}


否则会提示“未处理的RowUpdating事件”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值