总结:使用Gridview的事件传递参数

一直以来,见过别人用Gridview传递参数的方法有多种,在此把一些可以借鉴的总结一下:

 

Code
<asp:GridView ID="gvwCaseList" runat="server" AllowSorting="True" AutoGenerateColumns="False"
DataKeyNames
="case_id,create_date" OnRowCommand="gvwCaseList_RowCommand" OnRowDataBound="gvwCaseList_RowDataBound"
OnRowDeleting
="gvwCaseList_RowDeleting" OnRowEditing="gvwCaseList_RowEditing"
OnSorting
="gvwCaseList_Sorting" Width="100%" OnRowUpdating="gvwCaseList_RowUpdating"
OnDataBinding
="gvwCaseList_DataBinding">
<Columns>
<asp:TemplateField HeaderText="案件名称" SortExpression="case_name">
<ItemTemplate>
<span title="<%#Server.HtmlEncode(Eval("case_name").ToString()) %>">
<%#ShowTitle(Eval("case_name").ToString(), 20)%>
</span>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:BoundField DataField="suspect_money" HeaderText="涉嫌金额" SortExpression="suspect_money">
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="illegal_date" DataFormatString="{0:yyyy-MM-dd}" HeaderText="案发时间"
HtmlEncode
="False" SortExpression="illegal_date">
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="import_num" HeaderText="导入记录" HtmlEncode="False" SortExpression="import_num">
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="update_date" SortExpression="update_date">
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:LinkButton ID="lkbSee" runat="server" CommandName="Edit">查看</asp:LinkButton>
<asp:LinkButton ID="lkbEdit" runat="server" CommandName="Update">修改</asp:LinkButton>
<asp:LinkButton ID="lkbDelete" runat="server" CommandName="Delete" OnClientClick="return confirm('确实要永久性地删除此文件吗?');">删除</asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>

 

 

Code
protected void gvwCaseList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Cases _cases
= new Cases();
CasesBLL _casesBLL
= new CasesBLL();
_cases.CaseId
= this.gvwCaseList.DataKeys[e.RowIndex].Value.ToString();
_casesBLL.Delete(_cases);
GridBinds();
}

protected void gvwCaseList_RowEditing(object sender, GridViewEditEventArgs e)
{
string casesID = this.gvwCaseList.DataKeys[e.NewEditIndex].Values[0].ToString();
Response.Redirect(
"administrative_punishment_select.aspx?casesID="+casesID+"&select=true");
}

protected void gvwCaseList_Sorting(object sender, GridViewSortEventArgs e)
{
if (e.SortExpression != "")
{
ViewState[
"sortStr"] = e.SortExpression;
}
if (ViewState["order"] == null)
{
ViewState[
"order"] = "ASC";
}
else
{
if (ViewState["order"].ToString() == "ASC")
{
ViewState[
"order"] = "DESC";
}
else
{
ViewState[
"order"] = "ASC";
}
}
GridSort();
}

protected void gvwCaseList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton btnEdit
= (LinkButton)e.Row.FindControl("lkbEdit");
LinkButton btnDelete
= (LinkButton)e.Row.FindControl("lkbDelete");
if (UserInfo.UserType == "02")
{
btnEdit.Visible
= false;
btnDelete.Visible
= false;
}
}
}

protected void gvwCaseList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string casesID = this.gvwCaseList.DataKeys[e.RowIndex].Values[0].ToString();
string ctime = this.gvwCaseList.DataKeys[e.RowIndex].Values[1].ToString();
Response.Redirect(
"administrative_punishment_enter.aspx?ctime="+ctime+"&casesID="+casesID);
}

protected void gvwCaseList_DataBinding(object sender, EventArgs e)
{
gvwCaseList.Columns[
4].HeaderText = HandText;
}
}

protected void gvwCaseList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton btnEdit
= (LinkButton)e.Row.FindControl("lkbEdit");
LinkButton btnDelete
= (LinkButton)e.Row.FindControl("lkbDelete");
if (UserInfo.UserType == "02")
{
btnEdit.Visible
= false;
btnDelete.Visible
= false;
}
}
}

==========================================================================

 

Code

 

 

Code
protected void gvwDocInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton btnSelect
= (LinkButton)e.Row.FindControl("lkbPriview");
btnSelect.CommandArgument
= this.gvwDocInfo.DataKeys[e.Row.RowIndex].Values[0].ToString();
LinkButton btnEdit
= (LinkButton)e.Row.FindControl("lkbEdit");
btnEdit.CommandArgument
= this.gvwDocInfo.DataKeys[e.Row.RowIndex].Values[0].ToString();
LinkButton btnDelete
= (LinkButton)e.Row.FindControl("lkbDelete");
btnDelete.CommandArgument
= this.gvwDocInfo.DataKeys[e.Row.RowIndex].Values[0].ToString();
LinkButton btnUpdate
= (LinkButton)e.Row.FindControl("lkbUpdate");
btnUpdate.CommandArgument
= this.gvwDocInfo.DataKeys[e.Row.RowIndex].Values[0].ToString();
btnUpdate.Visible
= false;
if (this.gvwDocInfo.DataKeys[e.Row.RowIndex].Values[2].ToString() != UserInfo.DepartmentID)
{
btnEdit.Visible
= false;
btnDelete.Visible
= false;
}
if (UserInfo.UnitGroupCode == "10")
{
btnEdit.Visible
= true;
btnDelete.Visible
= true;
btnUpdate.Visible
= true;
}
if (UserInfo.UserType == "02")//屏蔽查看用户的添加功能
{
btnEdit.Visible
= false;
btnDelete.Visible
= false;
btnUpdate.Visible
= false;
}
}
}

protected void gvwDocInfo_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
Response.Redirect(
"dynamic_select.aspx?menuid=" + Request.QueryString["menuid"].ToString() + "&infoID=" + e.CommandArgument.ToString());
}
if (e.CommandName == "Edit")
{
Response.Redirect(
"dynamic_enter.aspx?menuid=" + Request.QueryString["menuid"].ToString() + "&infoID=" + e.CommandArgument.ToString());
}
if (e.CommandName == "Delete")
{
_active.InfoId
= e.CommandArgument.ToString();
_activeBLL.Delete(_active);
if (ViewState["sortStr"] != null && ViewState["sortStr"].ToString() != "")
{
DataBinds(ViewState[
"order"].ToString(), ViewState["sortStr"].ToString(), this.txtTitle.Text.ToString());
}
else
{
DataBinds(
null, null, this.txtTitle.Text.ToString());
}
}
if (e.CommandName == "Update")
{
_active.InfoId
= e.CommandArgument.ToString();
_active.IsTop
= true;
_active.StartTime
= DateTime.Now.AddDays(20);
_activeBLL.UpdateIsTop(_active.InfoId, _active.IsTop, _active.StartTime);
if (ViewState["sortStr"] != null && ViewState["sortStr"].ToString() != "")
{
DataBinds(ViewState[
"order"].ToString(), ViewState["sortStr"].ToString(), this.txtTitle.Text.ToString());
}
else
{
DataBinds(
null, null, this.txtTitle.Text.ToString());
}
}
}
posted @ 2008-12-05 14:50  chunchill  阅读(732)  评论(0编辑  收藏  举报