1---------------------------------------------GridView导出excel问题
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=book;Integrated Security=True");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from book", con);
DataTable dt = new DataTable();
da.Fill(dt);
DataGrid1 .DataSource = dt;
DataGrid1.DataBind();
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
//关闭 ViewState
EnableViewState = false;
StreamWriter MyWriter;
//将信息写入字符串
string Directorypath = Server.MapPath("~/2.excel");
MyWriter = new System.IO.StreamWriter(Directorypath);
//在Web窗体页上写出一系列连续的HTML特定字符和文本
HtmlTextWriter MyWeb = new HtmlTextWriter(MyWriter);
//将DataGrid中的内容输出到HtmlTextWriter对象中
DataGrid1.RenderControl(MyWeb);
//把HTML写回浏览器
Response.Write(MyWriter.ToString());
}
2------------------------------------window.open和listbox数据绑定的问题?
protected void Page_Load(object sender, EventArgs e)
{
}
static string a = "";
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=book;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("delete from book where id='"+a+"'", con);
cmd.ExecuteNonQuery();
con.Close();
ListBox1.DataSourceID = null;
ListBox1.DataSource = SqlDataSource1;
ListBox1.DataBind();
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
a = ((System.Web.UI.WebControls.ListControl)(sender)).SelectedItem.Value;
}
3-------------------------------------------------Session取值问题
没有public partial class Database : System.Web.UI.Page
没有继承System.Web.UI.Page类所以
说session在上下文中不存在
4-----------------------------------------------static 中怎么输出数据
public static void testWrite()
{
try
{
}
catch(Exception e)
{
System.Web.UI.Page p = new Page();
p.Response.Write("有错误");
//这样写就可以了
}
}
静态里的方法不能引用外面的类的方法
5---------------------------------------------页面刷新时,如何防止复选框保留上次操作选中的值?
protected void Page_Load(object sender, EventArgs e)
{
//CheckBox1.EnableViewState = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/CheckBox.aspx");
}
6--------------------------------------------gridview 在 Panel 里滚动定位
protected void Page_Load(object sender, EventArgs e)
{
//Panel1.Page.MaintainScrollPositionOnPostBack = true;
Panel1.Page.SmartNavigation = true;
}
就行了,能智能了
7---------------sizeID = Request.QueryString["sizeid"] ?? String.Empty; 两个问号什么意思?
是和sizeID = Request.QueryString["sizeid"] == null ? String.Empty :Request.QueryString["sizeid"].ToString();类似的,只是表示不一样吧,功能是一样的我试过了
8----------------------------------asp.net 文件上传出错 提示 找不到文件
string Directorypath = Server.MapPath("~/" + f.Name.ToString() );//
FileUpload1.SaveAs(Directorypath);
9---------------------------------为什么页面里的HTML元素都显示出来了
他说的大概是IE页面中查看源文件把HTML元素都显示出来了
10--------------------------------自定义查询的sql 语句怎么写啊
select 数据库字段 from 表 where 条件
group by 分组
order by 排序 desc
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="listbox.aspx.cs" Inherits="listbox" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页 </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource1" DataTextField="name"
DataValueField="id" EnableViewState="False" Height="77px" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
Width="291px"> </asp:ListBox> <asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString=" <%$ ConnectionStrings:bookConnectionString %>" SelectCommand="SELECT * FROM [book]">
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" OnClientClick='return confirm("你确定")'
Text="Button" /> </div>
</form>
</body>
</html>
代码CS