//GridView有全选/反选复选框,点击查看,编辑,删除,进行不同的操作,并对他们的权限进行判断,决定显示或隐藏
<script language="javascript">
         function CheckAllCheckboxes()
    {
      var d=document.getElementById("GridView1").getElementsByTagName("input");
     
      for (var i=1;i<d.length;i++)
                {
                if(d[i].type=="checkbox")
                {
                d[i].checked=!d[i].checked;
                }
               }

    }
    </script>
<asp:GridView ID="GridView1" runat="server" 
                      AutoGenerateColumns="False" Width="100%"
                     BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
                     CellPadding="3"
                     DataKeyNames="con_id"
                     onrowcommand="GridView1_RowCommand"
                     onrowdatabound="GridView1_RowDataBound">
                     <FooterStyle BackColor="White" ForeColor="#000066" />
                     <RowStyle ForeColor="#000066" />
                     <Columns>
                      
                          <%-- <asp:TemplateField HeaderText="选择">
                            <headertemplate>
                                <asp:CheckBox ID="all" runat="server" AutoPostBack="True" oncheckedchanged="item_CheckedChanged" />
                            </headertemplate>
                           
                            <itemtemplate>
                                <asp:CheckBox ID="item" runat="server" AutoPostBack="True"
                                     />
                            </itemtemplate>
                         </asp:TemplateField>--%>
                         <asp:TemplateField HeaderText="全选&lt;input type=checkbox id='ChAll' onclick='CheckAllCheckboxes()'/&gt;">
               <ItemTemplate>
               <asp:CheckBox runat="server" ID="item" />
               </ItemTemplate>
               </asp:TemplateField>
                         <asp:TemplateField HeaderText="编号" InsertVisible="False"
                             SortExpression="con_id">
                             <ItemTemplate>
                                 <asp:Label ID="lblcon_id" runat="server" Text='<%# Bind("con_id") %>'></asp:Label>
                             </ItemTemplate>
                             <EditItemTemplate>
                                 <asp:Label ID="Label1" runat="server" Text='<%# Eval("con_id") %>'></asp:Label>
                             </EditItemTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="姓名" SortExpression="con_name">
                             <ItemTemplate>

                                 <%--<a  href="Updatecus_contant.aspx?conid=<%# Eval("con_id") %>"></a>--%>
                                     <asp:Label ID="lblEdit" runat="server" Text='<%# Eval("con_name") %>'></asp:Label>
                                
                             </ItemTemplate>
                             <EditItemTemplate>
                                 <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("con_name") %>'></asp:TextBox>
                             </EditItemTemplate>
                         </asp:TemplateField>
                         <asp:BoundField DataField="con_zhiwu" HeaderText="职务"
                             SortExpression="con_zhiwu" />
                         <asp:TemplateField HeaderText="手机" SortExpression="con_mobile">
                             <EditItemTemplate>
                                 <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("con_mobile") %>'></asp:TextBox>
                             </EditItemTemplate>
                             <ItemTemplate>
                                 <asp:Label ID="lblcon_mobile" runat="server" Text='<%# Bind("con_mobile") %>'></asp:Label>
                             </ItemTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="客户姓名" SortExpression="con_customerid">
                             <ItemTemplate>
                                 <%--<asp:Label ID="Label2" runat="server" Text='<%# Bind("con_customerid") %>'></asp:Label>--%>
                                  <%# GetCus_Name(DataBinder.Eval(Container,"DataItem.con_customerid").ToString())%>
                               <%--    <%# EIP.Common.StringPares.GetSubString(DataBinder.Eval(Container, "DataItem.TopicTitle").ToString(),16) %>--%>
                             </ItemTemplate>
                             <EditItemTemplate>
                                 <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("con_customerid") %>'></asp:TextBox>
                             </EditItemTemplate>
                         </asp:TemplateField>
                         <asp:BoundField DataField="con_officephone" HeaderText="办公电话"
                             SortExpression="con_officephone" />
                          <asp:TemplateField ShowHeader="False">
                              <ItemTemplate>
                                  <asp:LinkButton ID="lbtnSelect" runat="server" CausesValidation="False"
                                      CommandName="Select" Text="查看" CommandArgument='<%# Bind("con_id") %>'></asp:LinkButton>
                                    &nbsp;<asp:LinkButton ID="lbtnEdit" runat="server" CausesValidation="False"
                                      CommandName="Edit" Text="编辑" CommandArgument='<%# Bind("con_id") %>'></asp:LinkButton>
                                 
                                  &nbsp;<asp:LinkButton ID="lbtnDelete" runat="server" CausesValidation="False"
                                      CommandName="Delete" Text="删除" CommandArgument='<%# Bind("con_id") %>' OnClientClick='return confirm("是否要删除该记录?");'></asp:LinkButton>
                              </ItemTemplate>
                          </asp:TemplateField>
                     </Columns>
                     <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                     <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                     <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                 </asp:GridView>

 


//自定义GridView中的编辑按钮,点击编辑后用CommandName区分更新和取消
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                    DataKeyNames="DirCategoryID" DataSourceID="SqlDataSource1"  BackColor="White" BorderColor="#CCCCCC"
                BorderStyle="None" BorderWidth="1px" CellPadding="3" onrowdatabound="GridView1_RowDataBound">
                <FooterStyle BackColor="White" ForeColor="#000066" />
         <RowStyle ForeColor="#000066" />
         <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
         <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
         <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                     <Columns>
                         <asp:BoundField DataField="DirCategoryID" HeaderText="通讯录分组ID"
                             InsertVisible="False" ReadOnly="True" SortExpression="DirCategoryID" />
                         <asp:BoundField DataField="DirCategoryName" HeaderText="通讯录分组名称"
                             SortExpression="DirCategoryName" />
                        
                         <asp:TemplateField ShowHeader="False">
                              <ItemTemplate>
                                  <asp:LinkButton ID="lbtnEdit" runat="server" CausesValidation="False"
                                      CommandName="Edit" Text="编辑" ></asp:LinkButton>
                                 
                                  &nbsp;<asp:LinkButton ID="lbtnDelete" runat="server" CausesValidation="False"
                                      CommandName="Delete" Text="删除" OnClientClick='return confirm("是否要删除组?");'></asp:LinkButton>
                              </ItemTemplate>
                              <EditItemTemplate>
                                <asp:LinkButton ID="ButtonUpdate" runat="server" CommandName="update" Text="更新" />
                                <asp:LinkButton ID="ButtonCancle" runat="server" CommandName="Cancel" Text="取消" />
                              </EditItemTemplate>
                          </asp:TemplateField>
                         
                     </Columns>
                </asp:GridView>

 

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)//行命令事件,在操作行时触发命令(Command)时走的事件
    {
        int rowIndex = -1;
        rowIndex = Convert.ToInt32(e.CommandArgument);

        if (e.CommandName == "Edit")//判断当前所触发的命令类型
        {
            Response.Redirect("Updatecus_contant.aspx?conid="+rowIndex);
        }
        if (e.CommandName == "Select")
        {
            Response.Redirect("ContactLook.aspx?conid=" + rowIndex);
        }
        if (e.CommandName == "Delete")
        {
            contant con = new contant();
            con.Delete(rowIndex);
            Response.Redirect("Contactlist.aspx");
        }
    }

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
 if(e.Row.RowState == DataControlRowState.Edit)//判断当前行的状态(当前行是在什么模式下)
 {}
 if (e.Row.RowType == DataControlRowType.DataRow)//判断当前行的类型(是Header,Footer,DataRow...)
 {}
}

posted on 2010-05-12 10:34  秦威龙  阅读(328)  评论(0编辑  收藏  举报