现在有一个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代码),好研究一下,谢谢了

解决方案 »

  1.   

    请实现 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!
      

  2.   

    就用一个按钮就够了..根据数据库的值去改变其Text显示
      

  3.   

    按钮显示已经实现了
    用的这样的方法是 ……
                                <asp:TemplateField HeaderText="审核开关" ShowHeader="False">
                                    <ItemStyle Width="80px" />
                                    <ItemTemplate>
                                        <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="false" CommandName="onoff">
                                        <%#  Eval("onoff").ToString() == "no" ? "取消审核" : "通过审核"%></asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
    但怎么对应的实现两种不同的操作,我就不明白了谢谢楼上给的代码,对我来说有点复杂了,好多地方看不懂,正在研究中……要是有更精练的也请发上来吧谢了
      

  4.   

    //aspx
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewDemo.aspx.cs" Inherits="GridViewDemo" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="au_id" DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand" OnRowCreated="GridView1_RowCreated">
                <Columns>
                    <asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True" SortExpression="au_id" />
                    <asp:BoundField DataField="au_lname" HeaderText="au_lname" SortExpression="au_lname" />
                    <asp:TemplateField HeaderText="审核开关" ShowHeader="False">
                        <ItemTemplate>
                            <asp:LinkButton ID="lbtnReview" runat="server" CausesValidation="false" CommandName="onoff" Text='<%#  Eval("contract").ToString() == "True" ? "通过审核" : "取消审核"%>'>
                            </asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=localhost;Initial Catalog=pubs;Integrated Security=True"
                ProviderName="System.Data.SqlClient" SelectCommand="SELECT top 2 [au_id], [au_lname], [contract] FROM [authors]">
            </asp:SqlDataSource>
        </div>
        </form>
    </body>
    </html>//aspx.cs
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;public partial class GridViewDemo : System.Web.UI.Page
    {
        private void Page_Load(object sender, System.EventArgs e)
        {    }    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            LinkButton lbtnReview;
            if (e.CommandName == "onoff")
            {
                lbtnReview = (LinkButton)GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("lbtnReview");
                if (lbtnReview != null)
                {
                    lbtnReview.Text = (lbtnReview.Text == "通过审核" ? "取消审核" : "通过审核");
                }
            }
        }    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            LinkButton lbtnReview;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                lbtnReview = (LinkButton)e.Row.Cells[2].FindControl("lbtnReview");
                if (lbtnReview != null)
                {
                    if (lbtnReview.CommandName == "onoff")
                        lbtnReview.CommandArgument = e.Row.RowIndex.ToString();
                }
            }
        }
    }
      

  5.   

    使用 e.CommandArgument存储当前行索引(e.Row.RowIndex)或主键值(au_id)是常见技巧
      

  6.   

    分析器错误信息: 未能加载类型“GridViewDemo”。源错误: 
    行 1:  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewDemo.aspx.cs" Inherits="GridViewDemo" %> 
    怎么会这样呢?
      

  7.   

    但怎么对应的实现两种不同的操作,我就不明白了谢谢楼上给的代码,对我来说有点复杂了,好多地方看不懂,正在研究中……
    ————————————————————
    呀,看不懂吗????
    "UPDATE Product SET Reviewed = (CASE Reviewed = 1 WHEN 0 ELSE 1 END) WHERE ProductId = @ProductId"; 
    ???我想我的代码已经是比较的精练的啦,至少在以上所有贴中 :D 希望你能看懂~使用 e.CommandArgument存储当前行索引(e.Row.RowIndex)
    ______________________________
    @amandag(高歌)
    哈,对于 RowIndex ,之前我也是这样用,被 MSDN 胡弄了,MSDN 上就是这样说的,
    然,现在,我更喜欢 GridViewRow row = ((Control)e.CommandSource).NamingContainer as GridViewRow;顺便 BS ASP.NET Team 不为 GridViewCommandEventArgs 提供 Row 属性
      

  8.   

    不提供貌似是为了统一EventArgs参数,但是对开发者来说确实不方便
      

  9.   

    aspx文件部分实现的很好了
    cs文件还没搞清楚主要是这几句
            if (e.Row.RowType == DataControlRowType.DataRow
            {
                lbtnReview = (LinkButton)e.Row.Cells[2].FindControl("lbtnReview");
                if (lbtnReview != null)
                {
                    if (lbtnReview.CommandName == "onoff")
                        lbtnReview.CommandArgument = e.Row.RowIndex.ToString();我要修改的文件原来是这样的,数据库里有个列名为onoff的,通过的值是yes,未通过是no
        protected void picview_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "yes")
            {
                Session["onoff"] = "yes";
                dbcom.CmdSqlstr("update Picnews set onoff='yes' where picid=" + int.Parse(this.picview.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString()) + "");
                dbcom.Dbselect("select * from Picnews,Picclass where picclass=Picclassid and  picclass='" + this.picclass.SelectedValue.ToString() + "' and onoff='yes' order by picid desc", this.picview);
                Response.Write("<script language='javascript'>alert(\"已允许进入前台!\")</script>");
            }
            if (e.CommandName == "no")
            {
                Session["onoff"] = "no";
                dbcom.CmdSqlstr("update Picnews set onoff='no' where picid=" + int.Parse(this.picview.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString()) + "");
                dbcom.Dbselect("select * from Picnews,Picclass where picclass=Picclassid and  picclass='" + this.picclass.SelectedValue.ToString() + "' and onoff='no' order by picid desc", this.picview);
                Response.Write("<script language='javascript'>alert(\"已经封锁!\")</script>");
            }
            if (e.CommandName == "pinlun")
            {
                Response.Redirect("picpinlunadmin.aspx?picid=" + this.picview.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString() + "");
            }
        }
      

  10.   

    LinkButton lbtnReview;
            if (e.CommandName == "onoff")
            {
                lbtnReview = (LinkButton)GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("lbtnReview");
                string str = string.Empty;
                if (lbtnReview != null)
                {
                    str =  (lbtnReview.Text == "通过审核" ? "yes" :"no");
                    lbtnReview.Text = (lbtnReview.Text == "通过审核" ? "取消审核" : "通过审核");
                }
                dbcom.CmdSqlstr("update Picnews set onoff='" + str + "' where picid=" + int.Parse(this.picview.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString()) + "");
                dbcom.Dbselect("select * from Picnews,Picclass where picclass=Picclassid and  picclass='" + this.picclass.SelectedValue.ToString() + "' and onoff=='" + str + "' order by picid desc", this.picview);
                
                if(str == "yes")
                 Response.Write("<script language='javascript'>alert(\"已允许进入前台!\")</script>");
                else
                      Response.Write("<script language='javascript'>alert(\"已经封锁!\")</script>");
            }
      

  11.   

    是的,我的想法就是把它修改成一个列。下面是我修改的,不知道是哪里改错了,总是提示说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.FormatException: 输入字符串的格式不正确。源错误: 
    行 243:            lbtnReview = (LinkButton)picview.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("lbtnReview");     protected void picview_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            LinkButton lbtnReview;
            if (e.CommandName == "onoff")
            {
                lbtnReview = (LinkButton)picview.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("lbtnReview");
                if (lbtnReview != null)
                {
                    lbtnReview.Text = (lbtnReview.Text == "通过审核" ? "取消审核" : "通过审核");
                }
            }
        }    protected void picview_RowCreated(object sender, GridViewRowEventArgs e)
        {
            LinkButton lbtnReview;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                lbtnReview = (LinkButton)e.Row.Cells[2].FindControl("lbtnReview");
                if (lbtnReview == null)
                {
                    if (lbtnReview.CommandName == "onoff")
                    //lbtnReview.CommandArgument = e.Row.RowIndex.ToString();
                    {
                        lbtnReview.CommandArgument = e.Row.RowIndex.ToString();
                        Session["onoff"] = "yes";
                        dbcom.CmdSqlstr("update Picnews set onoff='yes' where picid=" + int.Parse(this.picview.DataKeys[Convert.ToInt32(lbtnReview.CommandArgument)].Value.ToString()) + "");
                        dbcom.Dbselect("select * from Picnews,Picclass where picclass=Picclassid and  picclass='" + this.picclass.SelectedValue.ToString() + "' and onoff='yes' order by picid desc", this.picview);
                        Response.Write("<script language='javascript'>alert(\"已允许进入前台!\")</script>");
                    }
                    else
                    {
                        lbtnReview.CommandArgument = e.Row.RowIndex.ToString();
                        Session["onoff"] = "no";
                        dbcom.CmdSqlstr("update Picnews set onoff='no' where picid=" + int.Parse(this.picview.DataKeys[Convert.ToInt32(lbtnReview.CommandArgument)].Value.ToString()) + "");
                        dbcom.Dbselect("select * from Picnews,Picclass where picclass=Picclassid and  picclass='" + this.picclass.SelectedValue.ToString() + "' and onoff='no' order by picid desc", this.picview);
                        Response.Write("<script language='javascript'>alert(\"已经封锁!\")</script>");
                    }            }
            }
        }
      

  12.   

    我给你的代码里e.CommandArgument存放的是行索引,你有加下面的代码吗?    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            LinkButton lbtnReview;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                lbtnReview = (LinkButton)e.Row.FindControl("lbtnReview");
                if (lbtnReview != null)
                {
                    if (lbtnReview.CommandName == "onoff")
                        lbtnReview.CommandArgument = e.Row.RowIndex.ToString();
                }
            }
        }
      

  13.   

    加了,能正常显示,但是点按钮的时候就提示
    入字符串的格式不正确。 异常详细信息: System.FormatException: 输入字符串的格式不正确。源错误: 
    行 243:            lbtnReview = (LinkButton)picview.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("lbtnReview"); //aspx<asp:TemplateField HeaderText="审核开关" ShowHeader="False">
      <ItemStyle Width="80px" />
      <ItemTemplate>
         <asp:LinkButton ID="lbtnReview" runat="server" CausesValidation="false" CommandName="onoff" Text='<%#  Eval("onoff").ToString() == "no" ? "通过审核" : "取消审核"%>'>
         </asp:LinkButton>
      </ItemTemplate>//aspx.cs    protected void picview_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            LinkButton lbtnReview;
            if (e.CommandName == "onoff")
            {
                lbtnReview = (LinkButton)picview.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("lbtnReview");
                string str = string.Empty;
                if (lbtnReview != null)
                {
                    str = (lbtnReview.Text == "通过审核" ? "yes" : "no");
                    lbtnReview.Text = (lbtnReview.Text == "通过审核" ? "取消审核" : "通过审核");
                }
                dbcom.CmdSqlstr("update Picnews set onoff='" + str + "' where picid=" + int.Parse(this.picview.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString()) + "");
                dbcom.Dbselect("select * from Picnews,Picclass where picclass=Picclassid and  picclass='" + this.picclass.SelectedValue.ToString() + "' and onoff=='" + str + "' order by picid desc", this.picview);            if (str == "yes")
                    Response.Write("<script language='javascript'>alert(\"已允许进入前台!\")</script>");
                else
                    Response.Write("<script language='javascript'>alert(\"已经封锁!\")</script>");
            }
        }    protected void picview_RowCreated(object sender, GridViewRowEventArgs e)
        {
            LinkButton lbtnReview;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                lbtnReview = (LinkButton)e.Row.FindControl("lbtnReview");
                if (lbtnReview != null)
                {
                    if (lbtnReview.CommandName == "onoff")
                        lbtnReview.CommandArgument = e.Row.RowIndex.ToString();
                }
            }
        }                            </asp:TemplateField>
      

  14.   

    你单步调试看看 到这里 e.CommandArgument的值是什么?
      

  15.   

    不知道怎么注册RowCreated事件
     是在aspx文件里,选中gridview,然后在事件的行为里,RowCreated后面加上picview_RowCreated吧? 刚才没有,现在我刚加上 点了按钮以后,又开始提示:第 1 行: '=' 附近有语法错误。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 第 1 行: '=' 附近有语法错误。源错误: 行 142:        da1.Fill(ds); 
    不过又效果了,再打开的时候,发现审核状态已经改变就是不知道这个语法错误是怎么回事
      

  16.   

    好了,找到原因,原来是onoff=='" + str + "'这里多了一个等号
    太感谢您了,解决了困饶了我好几天的问题 虽然现在功能是实现了,但是我还没有彻底搞明白,还得再花点时间消化吸收一下我的QQ是31006414,您能加上我吗?我刚开始自学.net ,有太多问题却没人可以请教,希望在您不忙的时候可以指点一下我,感激不尽