<form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> </div> <br/> <asp:Button ID="BtnExport" runat="server" OnClick="BtnExport_Click" Text="Export to Excel" /> </form> |
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindData(); } } private void BindData() { string query = "SELECT * FROM customers"; SqlConnection myConnection = new SqlConnection(ConnectionString); SqlDataAdapter ad = new SqlDataAdapter(query, myConnection); DataSet ds = new DataSet(); ad.Fill(ds, "customers"); GridView1.DataSource = ds; GridView1.DataBind(); } public override void VerifyRenderingInServerForm(Control control) { // Confirms that an HtmlForm control is rendered for } protected void Button1_Click(object sender, EventArgs e) { Response.Clear(); Response.AddHeader("content-disposition","attachment;filename=FileName.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); GridView1.AllowPaging = false; BindData(); GridView1.RenderControl(htmlWrite); Response.Write(stringWrite.ToString()); Response.End(); GridView1.AllowPaging = true; BindData(); } protected void paging(object sender,GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; BindData(); } |
public DataSet PopulateDropDownList() { SqlConnection myConnection =new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString); SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM tblPhone", myConnection); DataSet ds = new DataSet(); ad.Fill(ds, "tblPhone"); return ds; } |
<ItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" DataSource="<%# PopulateDropDownList() %>" DataTextField="Phone" DataValueField = "PhoneID"> </asp:DropDownList> </ItemTemplate> |
protected void Button2_Click(object sender, EventArgs e) { StringBuilder str = new StringBuilder(); foreach (GridViewRow gvr in GridView1.Rows) { string selectedText = ((DropDownList)gvr.FindControl("DropDownList1")).SelectedItem.Text; str.Append(selectedText); } Response.Write(str.ToString()); } |
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="PersonID" DataSourceID="mySource" Width="366px" CellPadding="4" ForeColor="#333333" GridLines="None"> <Columns> <asp:CommandField ShowSelectButton="True" /> <asp:BoundField DataField="PersonID" HeaderText="PersonID" InsertVisible="False" ReadOnly="True" SortExpression="PersonID" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:TemplateField HeaderText="Select"> <ItemTemplate> <asp:CheckBox ID="chkSelect" runat="server" /> </ItemTemplate> <HeaderTemplate> </HeaderTemplate> </asp:TemplateField> </Columns> </asp:GridView> |
for (int i = 0; i < GridView1.Rows.Count; i++) { GridViewRow row = GridView1.Rows[i]; bool isChecked = ((CheckBox) row.FindControl("chkSelect")).Checked; if (isChecked) { str.Append(GridView1.Rows[i].Cells[2].Text); } } Response.Write(str.ToString()); |
<HeaderTemplate> <input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server" type="checkbox" /> </HeaderTemplate> |
<script language=javascript> function SelectAllCheckboxes(spanChk){ var oItem = spanChk.children; var theBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0]; xState=theBox.checked; elm=theBox.form.elements; for(i=0;i<elm.length;i++) if(elm[i].type=="checkbox" && elm[i].id!=theBox.id) { if(elm[i].checked!=xState) elm[i].click(); } } </script> |
<asp:GridView DataKeyNames="CategoryID" ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting"> <Columns> <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" /> <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" /> <asp:TemplateField HeaderText="Select"> <ItemTemplate> <asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("CategoryID") %>' CommandName="Delete" runat="server">Delete</asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> |
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1"); l.Attributes.Add('onclick", "javascript:return " + "confirm("是否要删除该记录? " + DataBinder.Eval(e.Row.DataItem, "id") + "')"); } } |
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Delete") { int id = Convert.ToInt32(e.CommandArgument); // 删除记录的专门过程 DeleteRecordByID(id); } } |
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { int categoryID = (int) GridView1.DataKeys[e.RowIndex].Value; DeleteRecordByID(categoryID); } |