读书人

求援GridView中动态切换审核开关的方

发布时间: 2011-12-26 23:09:59 作者: rapoo

求助,GridView中动态切换审核开关的方法
现在有一个GridView,审核部分用的是
ButtonField CommandName= "yes " Text= "通过审核 "
ButtonField CommandName= "no " Text= "取消审核 "

大概如下

标题 发布者 发布日期 编辑 删除
作品1 leee 2007-5-10 编辑 删除 通过审核 取消审核


我想实现如果是已经审核的行,就只显示“取消审核”
而未审核的,就显示“通过审核”,并切根据显示内容的不同,完成通过/取消的操作

作品1 leee 2007-5-10 编辑 删除 取消审核
作品2 sde 2007-5-12 编辑 删除 通过审核


我刚开始学,希望指点一下,最好给个相对完整点的代码片段(前台+cs代码),好研究一下,谢谢了

[解决办法]
请实现 grdPro_RowCommand 方法具体逻辑,
这里,假设你的“审核字段”只有二元值(如非1即0),因此更新的时候采取了技巧:
"UPDATE Product SET Reviewed = (CASE Reviewed = 1 WHEN 0 ELSE 1 END) WHERE ProductId = @ProductId ";

若是其他类型,请自行做响应修改

同时提供 CheckBox 示例

另外,你也可以设计两个Button,一个做通过,一个做取消,然后设定其Visible属性,


<%@ Page Language= "C# " %>
<%@ Import Namespace= "System.Data " %>

<%--
http://community.csdn.net/Expert/topic/5590/5590084.xml?temp=3.222293E-02
--%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<script runat= "server ">
void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
// 首次加载数据一定要放在 !IsPostBack 内,
// 避免回发的时候再次绑定数据,覆盖复选框状态
LoadProductData();
}
}

void chk_CheckedChanged(object sender, EventArgs e)
{
// 触发此事件的 CheckBox
CheckBox chk = sender as CheckBox;
// 得到 CheckBox 所在行
GridViewRow row = chk.NamingContainer as GridViewRow;
// 得到 GridView,当然这里可以直接引用控件ID
GridView grd = row.NamingContainer as GridView;
// 得到主键
int productId = (int)grd.DataKeys[row.RowIndex].Value;
// more codes
// ...
System.Diagnostics.Debug.Assert(false, productId.ToString());
Response.Write(row.RowIndex);
}

void grdPro_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName) {
case "Review ":
// 触发此事件的 Button
Button btnReview = e.CommandSource as Button;
// 得到 Button 所在行
GridViewRow row = btnReview.NamingContainer as GridViewRow;
// 得到 GridView,当然这里可以直接引用控件ID
GridView grd = row.NamingContainer as GridView;
int productId = (int)grd.DataKeys[row.RowIndex].Value;
// comment the line below in your release verion
Response.Write(productId);
// your more codes
// in your case, maybe take the update sql as following:
// string sqlUpd = "UPDATE Product SET Reviewed = (CASE Reviewed = 1 WHEN 0 ELSE 1 END) WHERE ProductId = @ProductId ";


// ...
// ...
break;
}
}

void LoadProductData()
{
DataTable dt = CreateProductTable();

grdPro.DataSource = dt;
grdPro.DataBind();
}

#region sample data

static DataTable CreateProductTable()
{
DataTable tbl = new DataTable( "Products ");

tbl.Columns.Add( "ProductID ", typeof(int));
tbl.Columns.Add( "ProductName ", typeof(string));
tbl.Columns.Add( "CategoryID ", typeof(int));
tbl.Columns.Add( "HasPic ", typeof(bool));
tbl.Columns.Add( "Reviewed ", typeof(bool));
DataRow row = tbl.NewRow();
row[0] = 1;
row[1] = "Chai ";
row[2] = 1;
row[3] = true;
row[4] = false;
tbl.Rows.Add(row);

row = tbl.NewRow();
row[0] = 2;
row[1] = "Chang ";
row[2] = 1;
row[3] = false;
row[4] = false;
tbl.Rows.Add(row);

row = tbl.NewRow();
row[0] = 3;
row[1] = "Aniseed Syrup ";
row[2] = 2;
row[3] = true;
row[4] = false;
tbl.Rows.Add(row);

row = tbl.NewRow();
row[0] = 4;
row[1] = "Chef Anton 's Cajun Seasoning ";
row[2] = 2;
row[3] = false;
row[4] = true;
tbl.Rows.Add(row);

row = tbl.NewRow();
row[0] = 5;
row[1] = "Chef Anton 's Gumbo Mix ";
row[2] = 2;
row[3] = true;
row[4] = true;
tbl.Rows.Add(row);

row = tbl.NewRow();
row[0] = 47;
row[1] = "Zaanse koeken ";
row[2] = 3;
row[3] = true;
row[4] = true;
tbl.Rows.Add(row);

row = tbl.NewRow();
row[0] = 48;
row[1] = "Chocolade ";
row[2] = 3;
row[3] = false;
row[4] = false;
tbl.Rows.Add(row);

row = tbl.NewRow();
row[0] = 49;
row[1] = "Maxilaku ";
row[2] = 3;
row[3] = true;
row[4] = false;
tbl.Rows.Add(row);

return tbl;
}

#endregion
</script>

<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> Untitled Page </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:GridView ID= "grdPro " runat= "server " AutoGenerateColumns= "false " DataKeyNames= "ProductID " OnRowCommand= "grdPro_RowCommand ">
<Columns>
<asp:BoundField DataField= "ProductName " HeaderText= "ProductName " />
<asp:TemplateField HeaderText= "HasPic ">
<ItemTemplate>
<asp:CheckBox ID= "chk " runat= "server " checked= ' <%# Eval( "HasPic ") %> ' AutoPostBack= "true " OnCheckedChanged= "chk_CheckedChanged " />
<br />
<asp:Label ID= "lbl " runat= "server " Visible= ' <%# Eval( "HasPic ") %> ' Text= ' <%# Eval( "ProductName ", "产品 {0} 没有图片 ") %> ' />


</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText= "HasPic ">
<ItemTemplate>
<asp:Button ID= "btnReview " runat= "server " Text= ' <%# (bool)Eval( "Reviewed ") ? "取消审核 " : "通过审核 " %> ' CommandName= "Review " />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

</div>
</form>
</body>
</html>


Good Luck!
[解决办法]
就用一个按钮就够了..

根据数据库的值去改变其Text显示

读书人网 >asp.net

热点推荐