zoukankan      html  css  js  c++  java
  • 有关 gridview的 (做东西 容易忘记 记下!)

     <asp:GridView ID="gvCardInfo" runat="server" CellPadding="4" 
                            GridLines
    ="None"  Width="766px" HorizontalAlign="Center" OnRowDeleting="gvCardInfo_RowDeleting" AutoGenerateColumns="False" Font-Size="9pt" OnRowDataBound="gvCardInfo_RowDataBound" OnPageIndexChanging="gvCardInfo_PageIndexChanging" PageSize="15" AllowPaging="True" ForeColor="#333333" DataKeyNames="ReserveID" OnSelectedIndexChanged="gvCardInfo_SelectedIndexChanged" OnRowCommand="gvCardInfo_RowCommand">
                            
    <FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />
                            
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                            
    <Columns>
                            
    <asp:BoundField DataField="ReserverID" HeaderText="回复ID" >
                                    
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                
    </asp:BoundField>
                                
    <asp:BoundField DataField="TopicID" HeaderText="帖子" >
                                    
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                
    </asp:BoundField>
                                
    <asp:TemplateField HeaderText="回复内容">
                                    
    <EditItemTemplate>
                                        
    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ReserveContent") %>'></asp:TextBox>
                                    
    </EditItemTemplate>
                                    
    <ItemTemplate>
                                        
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("ReserveContent") %>'></asp:Label>
                                    
    </ItemTemplate>
                                    
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                
    </asp:TemplateField>
                                
    <asp:BoundField DataField="ReserveDate" HeaderText="回复时间" DataFormatString="{0:D}">
                                    
    <ItemStyle HorizontalAlign="Center" />
                                
    </asp:BoundField>
                                
    <asp:TemplateField HeaderText="状态">
                                    
    <EditItemTemplate>
                                        
    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ReserveStatus") %>'></asp:TextBox>
                                    
    </EditItemTemplate>
                                    
    <ItemTemplate>
                                        
    <asp:Label ID="Lblstatus" runat="server" Text='<%# Bind("ReserveStatus") %>'></asp:Label>
                                    
    </ItemTemplate>
                                    
    <ItemStyle HorizontalAlign="Center" />
                                
    </asp:TemplateField>
                             
    <asp:HyperLinkField DataNavigateUrlFields="TopicID" DataNavigateUrlFormatString="~/Web/Topic.aspx?ID={0}"
                                    HeaderText
    ="详细信息" Text="详细信息" >
                                 
    <ControlStyle Font-Underline="False" />
                                 
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                             
    </asp:HyperLinkField>
                                
    <asp:CommandField HeaderText="删除" ShowDeleteButton="True" >
                                    
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                    
    <ControlStyle Font-Underline="False" />
                                
    </asp:CommandField>
                             
    <asp:HyperLinkField DataNavigateUrlFields="TopicID" DataNavigateUrlFormatString="MangerTopic.aspx?ID={0}"
                                    HeaderText
    ="处理" Text="处理" >
                                 
    <ControlStyle Font-Underline="False" />
                                 
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                             
    </asp:HyperLinkField>
                                
    <asp:ButtonField CommandName="pass" HeaderText="审核" Text="通过">
                                    
    <ItemStyle HorizontalAlign="Center" />
                                
    </asp:ButtonField>
                                
    <asp:TemplateField>
                                    
    <HeaderTemplate>
                                        
    &nbsp;<asp:CheckBox ID="Cball" runat="server" OnCheckedChanged="Cball_CheckedChanged"  AutoPostBack="true"/>
                                    
    </HeaderTemplate>
                                    
    <ItemTemplate>
                                        
    <asp:CheckBox ID="Cbsig" runat="server" />
                                    
    </ItemTemplate>
                                    
    <ItemStyle HorizontalAlign="Center" />
                                
    </asp:TemplateField>
                             
    </Columns>
                             
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                            
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                            
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                            
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                            
    <EditRowStyle BackColor="#999999" />
                        
    </asp:GridView>

    上面是前台的aspx的代码!
    1.用GridView ButtonField列 
    <asp:ButtonField Text="审核"  CommandName="pass"/>
    后台处理事件  这个利用 这个自定义 事件的!
     protected void gvCardInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        
    {
            
    if (e.CommandName == "pass")
            
    {
                
    int intRow = int.Parse(e.CommandArgument.ToString()); //获取当前所在行
                int ID = Convert.ToInt32(gvCardInfo.DataKeys[intRow].Value.ToString());
                
    string sql = "update Reserve set ReserveStatus=1 where ReserverID="+ID;
                
    if (sh.execCommand(sql))
                
    {
                    bing();
                }

            }


        }

    2 全选的效果!!
    aspx代码中的这个注意下 checkbox给他启用autopostback
      <asp:TemplateField>
                                    
    <HeaderTemplate>
                                        
    &nbsp;<asp:CheckBox ID="Cball" runat="server" OnCheckedChanged="Cball_CheckedChanged"  AutoPostBack="true"/>
                                    
    </HeaderTemplate>
                                    
    <ItemTemplate>
                                        
    <asp:CheckBox ID="Cbsig" runat="server" />
                                    
    </ItemTemplate>
                                    
    <ItemStyle HorizontalAlign="Center" />
                                
    </asp:TemplateField>

    后台
     foreach (GridViewRow gr in gvCardInfo.Rows)
            
    {
                CheckBox cb 
    = (CheckBox)gr.Cells[9].FindControl("Cbsig");
                
    if (!cb.Checked)
                
    {
                      cb.Checked
    =true;
                }

                
    else 
                
    {
                    cb.Checked 
    = false;
                }

            }

    代码全部贴处理吧!!
    public partial class Admin_MagReserve : System.Web.UI.Page
    {
        SqlHelper sh 
    = new SqlHelper();
        
    protected void Page_Load(object sender, EventArgs e)
        
    {
            
    if (!IsPostBack)
            
    { bing(); 
            }

           


        }

        
    public void bing()
        
    {
            
    string sqlstr = "select ReserverID,TopicID,ReserveContent,ReserveDate ,ReserveStatus=case  when ReserveStatus=0 then '未审核' when ReserveStatus=1 then '审核通过'end from Reserve order by ReserveDate Desc ";

            SqlConnection sqlconn 
    = sh.conCreate();
            sqlconn.Open();
            SqlDataAdapter sqldataadapter 
    = new SqlDataAdapter(sqlstr, sqlconn);
            DataSet mydataset 
    = new DataSet();
            sqldataadapter.Fill(mydataset);
            gvCardInfo.DataSource 
    = mydataset;
            gvCardInfo.DataKeyNames 
    = new string[] "ReserverID" };

            gvCardInfo.DataBind();


            sqlconn.Close();

        }

        
    protected void gvCardInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        
    {
            
    string sqlstr = "delete from Reserve where ReserverID='" + gvCardInfo.DataKeys[e.RowIndex].Value + "'";
            sh.execCommand(sqlstr);
            Response.Redirect(
    "List.aspx");
        }

        
    protected void gvCardInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        
    {
            
    if (e.Row.RowType == DataControlRowType.DataRow)
            
    {
                ((LinkButton)(e.Row.Cells[
    6].Controls[0])).Attributes.Add("onclick""return confirm('确定删除吗?')");
            }

            e.Row.Attributes.Add(
    "onmouseover""c=this.style.backgroundColor;this.style.backgroundColor='#E3E2E8'");
            
    //下面我们再设置当鼠标离开后背景色再还原
            e.Row.Attributes.Add("onmouseout""this.style.backgroundColor=c;");
            
    //为特定的数改变行样式这也是在这个事件里面,因为这个事件是在数据被绑定的时候执行的
            for (int i = 0; i <this.gvCardInfo.Rows.Count; i++)
            
    //为了对全部数据行都有用,我们使用循环
                string lbl = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ReserveStatus"));//我们得取出行中state字段绑定的值,用他作为判断条件
                if (lbl == "未审核")
                
    //如果他的值等于,那么
                    e.Row.BackColor = Color.LimeGreen;//给当前行的背景色赋值,
                }

            }


        }

        
    protected void gvCardInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        
    {
            gvCardInfo.PageIndex 
    = e.NewPageIndex;
            gvCardInfo.DataBind();
        }

        
    protected void gvCardInfo_SelectedIndexChanged(object sender, EventArgs e)
        
    {

        }

        
    protected void Button1_Click(object sender, EventArgs e)
        
    {
            
    for (int i = 0; i <= gvCardInfo.Rows.Count - 1; i++)
            
    {
                CheckBox cbox 
    = (CheckBox)gvCardInfo.Rows[i].FindControl("CBsig");
                
    if (cbox.Checked == true)
                
    {

                    
    string sqlstr = "delete from Reserve where  ReserverID ='" + gvCardInfo.DataKeys[i].Value + "'";

                    sh.execCommand(sqlstr);
                    
                }

            }

            bing();
           
        }

        
    protected void Cball_CheckedChanged(object sender, EventArgs e)
        
    {
            
    foreach (GridViewRow gr in gvCardInfo.Rows)
            
    {
                CheckBox cb 
    = (CheckBox)gr.Cells[9].FindControl("Cbsig");
                
    if (!cb.Checked)
                
    {
                      cb.Checked
    =true;
                }

                
    else 
                
    {
                    cb.Checked 
    = false;
                }

            }

        }

        
    protected void gvCardInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        
    {
            
    if (e.CommandName == "pass")
            
    {
                
    int intRow = int.Parse(e.CommandArgument.ToString()); //获取当前所在行
                int ID = Convert.ToInt32(gvCardInfo.DataKeys[intRow].Value.ToString());
                
    string sql = "update Reserve set ReserveStatus=1 where ReserverID="+ID;
                
    if (sh.execCommand(sql))
                
    {
                    bing();
                }

            }


        }

        
    protected void Btnpass_Click(object sender, EventArgs e)
        
    {
            
    for (int i = 0; i <= gvCardInfo.Rows.Count - 1; i++)
            
    {
                CheckBox cbox 
    = (CheckBox)gvCardInfo.Rows[i].FindControl("CBsig");
                
    if (cbox.Checked == true)
                
    {
                      
                    
    string sqlstr = "update Reserve set ReserveStatus=1 where ReserverID ='" + gvCardInfo.DataKeys[i].Value + "'";

                    sh.execCommand(sqlstr);

                }

            }

            bing();
        }



  • 相关阅读:
    Entity Framework+SQLite+DataBaseFirst
    接口的实现方式(显示和隐示)及协变和逆变
    C#线程
    UIScrollView放大缩小图片偏移的问题
    SQL Server 2008在添加用户时弹出错误提示:此版本的 Microsoft Windows 不支持 MUST_CHANGE 选项
    jmeter的正则表达式的使用
    Linux下安装jmeter
    系统测试方案
    [ASP.NET].NET逻辑分层架构总结
    [C#]C#时间日期操作
  • 原文地址:https://www.cnblogs.com/barney/p/1162583.html
Copyright © 2011-2022 走看看