扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
<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> |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者