随笔-113  评论-60  文章-0  trackbacks-3
  2006年10月13日

 <asp:TemplateField HeaderText="全选">
        <HeaderTemplate>
                <asp:CheckBox ID="checkall" runat="server" Text="全选" 
                        AutoPostBack="true" OnCheckedChanged="checkAll" />
         </HeaderTemplate>
          <ItemTemplate>
                   <asp:CheckBox ID="check" runat="server" Text="选择" />
            </ItemTemplate>     
</asp:TemplateField>

   protected void checkAll(object sender, EventArgs e)
  {
       CheckBox cb = (CheckBox) sender;
       if (cb.Text == "全选")
       {
           foreach (GridViewRow gvr in gvLessons.Rows)
           {
               CheckBox cb1 = (CheckBox)gvr.FindControl("check");
               cb1.Checked = cb.Checked;
           }
       }
    }
注:参考天轰穿的DataGrid系列技巧(导出excel,事件,多种方式呈现数据,全选全删)等等等
在此感谢天轰穿
posted @ 2006-10-13 15:13 灵风 阅读(292) 评论(0) 编辑

在页面添加一个ID为ExportToExcelBtn的按钮,代码部分如下:


protected void ExportToExcelBtn_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=lessons.xls");
        Response.Charset = "gb2312";
        Response.ContentType = "application/vnd.xls";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

        gvLessons.AllowPaging = false;
        gvLessons.DataBind();
        gvLessons.RenderControl(htmlWrite);

        Response.Write(stringWrite.ToString());
        Response.End();

        gvLessons.AllowPaging = true;
        gvLessons.DataBind();
    }
 要重载VerifyRenderingInServerForm方法,不然将报错
    public override void VerifyRenderingInServerForm(Control control) { }

posted @ 2006-10-13 12:59 灵风 阅读(561) 评论(1) 编辑

 <asp:TemplateField HeaderText="类别" SortExpression="CategoryName">
        <EditItemTemplate>
        <asp:DropDownList ID="CategoryDDL2" runat="server" Width="180px" 
                  SelectedValue='<%# Bind("CategoryID") %>'
                  DataSourceID="CategoryDS" 
                  DataTextField="CategoryName" DataValueField="CategoryID"
>
        </asp:DropDownList>                   
         </EditItemTemplate>
               <InsertItemTemplate>
                   <asp:DropDownList ID="CategoryDDL3" runat="server" Width="180px" 
                           SelectedValue='<%# Bind("CategoryID") %>'     DataSourceID="CategoryDS" 
                           DataTextField="CategoryName" DataValueField="CategoryID">

                    </asp:DropDownList>
               </InsertItemTemplate>
               <ItemTemplate>
                   <asp:Label ID="Label1" runat="server" Text='<%# Bind("CategoryName") %>'>
                   </asp:Label>
               </ItemTemplate>



          <UpdateParameters>
                <asp:Parameter Name="LessonID" Type="Int32" />
                <asp:Parameter Name="CategoryID" Type="Int32" />
                <asp:Parameter Name="LessonName" Type="String" />
            </UpdateParameters>
            <InsertParameters>
               <asp:Parameter Name="CategoryID" Type="Int32" />
                <asp:Parameter Name="LessonName" Type="String" />
             </InsertParameters>

posted @ 2006-10-13 10:35 灵风 阅读(1834) 评论(4) 编辑

 

<asp:TemplateField HeaderText="序号" InsertVisible="False" SortExpression="LessonID">
 <ItemTemplate>
     <asp:Label ID="Label2" runat="server"  
      Text='<%# this.GridView1.PageIndex * this.GridView1.PageSize 
                        + this.GridView1.Rows.Count + 1
%>'  >
      </asp:Label>
</ItemTemplate>
</asp:TemplateField>
posted @ 2006-10-13 09:20 灵风 阅读(1835) 评论(5) 编辑