以前犯过的错误又出现了,竟不知道怎么解决,就想做一个这样的错误集(及时更新):
1.asp.net控制上传文件大小:
2.flash8设备自体
3.flash大小控制
4.显示一定字数的带格式的文本
5.弹出窗口
6.在ASP.NET中自动给URL地址加上超链接
7.客户端获取点计的城市名然后传输
8.怎么读取客户端<input type="hidden" name="icon" value="1" id=icon>里面的值。前提是不能加runat=server这句话
9.if(!isPostback)
10.Request.QueryString读取不准确时
11.所有Web相关的项目都无法创建
12.多值传递
13.项目生成提示一些web控件如httpcontext找不到,应引用system.web.dll
14.datalist嵌套问题:总是不执行DataListpro2_ItemDataBound
15.按回车触发按钮click事件的问题
16.给drodownlist加上标题“请选择”
17.ConnectionString 属性尚未初始化
18.下拉菜单总是选择第一项
19.获取当前页面地址<a href='<%=Request.Url.ToString() %>'>Now</a>
20.关于传值的问题
21.如果没有找到页面,如何跳转到指定页面
22.判断datalist里面图片尺寸(控制图片输出尺寸)
23.模态窗口传值
24.如何截取标题的显示长度
25.datagrid只显示第一页数据记录
26.文件下载
27.sql语句怎么判断字段为空
28.在线编辑器中回车换行
29.DropDownList 加载两遍数据
30.float型字段值转换成字符串型
31.返回到主框架
1.asp.net控制上传文件大小:
首先,要在web.config中加下句:
在<system.web>下一行加:
<httpRuntime maxRequestLength="102400" executionTimeout="3600" />
//102400表示102M
然后
在上传操作中:
Int Size = InputFile.PostedFile.ContentLength;
if (Size > 102400)//你设定的大小如(102K)
{
return;
}
2.flash8设备自体:选中该选项时将符号“<>”(将文本呈现为html)选中
3.flash大小控制
_root.dx = 1;
_root._xscale = 55;
_root._yscale = 55;
if(_root.dx==2)
{
_root._xscale = 100;
_root._yscale = 100;
}
else if(_root.dx==1)
{
_root._xscale = 55;
_root._yscale = 55;
}
4.显示一定字数的带格式的文本
处理方法:(超过层高度就隐藏)
<div runat="server" id="mydiv2" style="OVERFLOW-Y:hidden;HEIGHT:190px"></div>
5.弹出窗口
a.window.open()
b.window.showModalDialog()//打开模态窗口
c.response.redrect()
d.this.location.herf=;target=
设置target=blank时,后退始终是本页;不设置时后退到父页,但提示“此页已过期”
6.在ASP.NET中自动给URL地址加上超链接
private void Button1_Click(object sender, System.EventArgs e)
{
string strContent = InputTextBox.Text;
Regex urlregex = new Regex(@"(http:([/w.]+//?)/S*)",
RegexOptions.IgnoreCase| RegexOptions.Compiled);
strContent = urlregex.Replace(strContent,
"<a href=/"/" target=/"_blank/"></a>");
Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+/@[a-zA-Z_0-9.-]+/./w+)",
RegexOptions.IgnoreCase| RegexOptions.Compiled);
strContent = emailregex.Replace(strContent, "<a href=mailto:></a>");
lbContent.Text += "<br>"+strContent;
}
7.客户端获取点计的城市名然后传输
function hit(city)
{
document.location.href="city.aspx?city="+city;
}
接收
string city = HttpContext.Current.Request["city"].ToString();
Label1.Text = city;
如果为传输的为哈尔滨或者石家庄
显示的却为哈尔和石家 为什么会这样啊就3个字符长度的会截取后面一个字!其他长度的就没这个问题!我没
写什么截取字符长度之类的函数啊! IE地址栏显示的也是正常的啊
http://localhost/WTcms/city.aspx?city=石家庄
答:可能和你的页面编码方式不同有关
试试
escape
或
encodeURI
document.location.href="city.aspx?city="+city;
==>
document.location.href="city.aspx?city="+encodeURI(city);
8.怎么读取客户端<input type="hidden" name="icon" value="1"
id=icon>里面的值。前提是不能加runat=server这句话,要怎么读取呢?
<HTML>
<HEAD>
<title>GetWeather</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language>
function OnSumit()
{
document.GetWeather.TextBox1.value=document.GetWeather.test.value;
}
</script>
</HEAD>
<body>
<form id="GetWeather" method="post" runat="server">
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<input type="hidden" id="test" name="test" value="1"> <INPUT type="button" value="Button"
οnclick="OnSumit();">
</form>
</body>
</HTML>
9.if(!isPostback)
isPostback属性获取一个值,该值指示该页是否正为响应客户端回发而加载,或者它是否正被首次加载和访
问。如果是为响应客户端回发而加载该页,则为 true;否则为 false。
所以if(!IsPostBack)的意思就是如果不是了响应客户端回发而加载该页的时候执行{}里的数据。所以你的
以上代码最好放在里面,这样在你使用分页的时候就知道为什么了,你可以为你这个datagird加入分页,然
后分别把代码放在{}里面和外面看看效果
10.Request.QueryString读取不准确时
解决:
string userName = Server.UrlDecode(Request.QueryString["UserName"]);
<a href='Detail.aspx?UserName=<%# Server.UrlEncode(Eval("UserName").ToString()) %>'
target="_blank"><%# Eval("UserName") %></a>
11.所有Web相关的项目都无法创建
“尝试创建 Web 项目或打开位于 URL“http://localhost/WebApplication1”的 Web 项目时,Web 服务器
报告了以下错误。“HTTP/1.0 403 Forbidden”。”
解决:IIS设置 属性-》 目录安全性 -》 集成windows身份验证
再不行将asp.net用户 添加到administrators用户组
12.多值传递
用form 提交
或者 把值 用符号加一起 放入session
例"usrname$userid$22$..."
另一个页面 用
string [] str = session["request"].split('&')
str是数组
username=str[0]
userid = str[1]
13.项目生成提示一些web控件如httpcontext找不到,(添加引用)应引用system.web.dll
14.datelist嵌套问题:总是不执行DataListpro2_ItemDataBound
现象:总是不执行DataListpro2_ItemDataBound,
<asp:datalist id="DataListpro2" runat="server" OnItemDataBound="DataListpro2_ItemDataBound">
提示找不到DataListpro2_ItemDataBound
解决:将
void DataListpro2_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs
e)
改为:
public void DataListpro2_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)
15.按回车触发按钮click事件的问题
在一个页面上存在多个按钮,有的时候希望按回车直接实现点击某按钮的效果,解决方法如下:
<script language="javascript">
function document.onkeydown()
{
var e=event.srcElement;
if(event.keyCode==13)
{
document.getElementById("需要点击的那个按钮的id").click();
return false;
}
}
</script>
如果页面上有多行文本框,在多行文本框内回车仅仅想实现换行而不是提交表单这么修改
if(e!=document.getElementById("多行文本框的id")&&event.keyCode== 13)
(以上代码添加在前台页面任意处即可)
16.给drodownlist加上标题“全部”
public void initdrop(int flag)
...{
string myQuery="";
if(flag==1)myQuery= "SELECT * from T_employee ";
myConnection = new OleDbConnection(CS);
// Open the connection.
myConnection.Open();
myCommand= new OleDbCommand(myQuery);
// Assign the connection property.
myCommand.Connection = myConnection;
objDataReader=myCommand.ExecuteReader();
while(objDataReader.Read())
...{
DropDownList1.Items.Add(new
ListItem(objDataReader["erealname"].ToString(),objDataReader["erealname"].ToString()));
}
this.DropDownList1.Items.Insert(0,new ListItem("全部","全部"));
if(objDataReader!=null)objDataReader.Close();
if(myConnection!=null)myConnection.Close();
}
17.ConnectionString 属性尚未初始化
源程序:manager=ul.getusername();
if(manager!="管理员")
return;
CS=Application.Get("myConnectionString").ToString();
....
private void Button1_Click(object sender, System.EventArgs e)
{
OleDbConnection myConnection = new OleDbConnection(CS);
myConnection.Open();//出错行
....
跟踪发现 CS=Application.Get("myConnectionString").ToString();没执行,cs为空
原因:不为管理员时,return;跳出,"cs=..."没执行
18.下拉菜单总是选择第一项
原因a.赋值之后又重新给dopdownlist加载了一遍数据,应该只在初始化时加载一次数据
b.判断的值不匹配,后面多了一个空格
c.数据有重复,选择重复中的第一个
19.获取当前页面地址<a href='<%=Request.Url.ToString() %>'>Now</a>
20.关于传值的问题
<input type ='button' onclcik="javascript:GoSelect();"
<script>
function GoSelect()
{
var newWin = window.open("bb.aspx");
}
</script>
bb.aspx中
window.opener.document.getElementById("txt1").value = 'aaa'
21.如果没有找到页面,如何跳转到指定页面
a.//webconfig中
<authentication mode="Forms">
<forms name="xxx" loginUrl=跳转的页面.aspx" protection="All" path="/"/>
</authentication>
b.在Webconfig中配置如下:
<customErrors defaultRedirect="ErrorPage.aspx" mode="RemoteOnly"></customErrors>
defaultRedirect表示自定义的错误网页的名称。Mode元素表示:对不在本地Web服务器上运行的用户显示自
定义的信息。
22.判断datalist里面图片尺寸(控制图片输出尺寸)
cs中
protected string GetUrlWH(string url)
...{
string sPath = Server.MapPath(url);
System.Drawing.Image img = System.Drawing.Image.FromFile(sPath);
int width = img.Width;
int height = img.Height;
if (width > 100) width = 90;
if (height > 120) height = 90;
return "style='width:" + width + ";height:" + height + "'";
}
这个写在模板列中:
<img src=<%#Eval("url")%> <%#GetUrlWH(Eval("url").ToSTring())%> >
23.模态窗口传值
---- window对象有一个方法称为showModalDialog,我们可以在页面按钮的onclick中写如下代码:
< BUTTON οnclick="window.showModalDialog('dialog.htm')" >Search< /BUTTON >
---- 系统会在新窗口中打开dialog.htm页面,并且等待用户响应,如果用户不响应该页面,那么主页面将
得不到光标。
---- 在dialog.htm中设置window对象的returnValue属性,就可以让主页面得到返回值。例如,在页面的确
定按钮的onclick中写:
window.returnValue = window. document.all.iptPeopleID.value
---- 将输入框iptPeopleID的值赋给window对象的returnValue属性。在主页面中就可以得到这个值:
var str = showModalDialog("dialog1.htm")
24.如何截取标题的显示长度
a.在DataGrid1_ItemDataBound中写
e.Item.Cells[6].ToolTip=e.Item.Cells[6].Text.ToString().Trim();
e.Item.Cells[6].Text=e.Item.Cells[6].Text.Length>15?e.Item.Cells[6].Text.ToString().Substring(
0,15).ToString():e.Item.Cells[6].Text.ToString().Trim();
b.this.Label2.Text=objDataReader.GetString(1).Substring(0,200)+"...";
c.用方法:public string MyLeftFunction(string str, int Int)
{
if (str.Length >= Int)
{
str = str.Substring(0,Int);
return str;
}
else
{
return str;
}
}
25.datagrid只显示第一页数据记录
解决:将datagrid属性AllowCustomPaging的值设为False
26.文件下载
详细解决参考:http://blog.youkuaiyun.com/lxs5i5j/archive/2007/02/02/1500790.aspx
27.sql语句怎么判断字段为空
in sql:select b from A where b='' or b is null
28.在线编辑器中回车换行
a.回车同时按下‘shift’键
b.这个功能在后台的“参数设置”那里开启或者关闭
29.DropDownList 加载两遍数据
ASP.NET页面实现机制是这样的,Page_Load时执行Page_Load内代码,当按按钮时,继续首先执行Page_Load,再执行Button1事件,由于DropDownList控件与数据库db1.mdb中的产品名绑定代码在Page_Load内,所以数据绑定执行两次.
解决:将动作load属性设置为空,不执行page_load
30.float型字段值转换成字符串型
问题:在SQL数据库中设置字段类型为Float型,读取时用GetFloat,总会提示数据转换出错
解决:用GetDouble读取。因为在浮点型数据有单精度型和双精度型,分别为4位和8位数据,在SQL数据库中Float型字段默认为8位,即为双精度型,所以用GetDouble
31.返回到主框架
this.Response.Write("<script language='javascript'>top.location.href='/index_home.aspx'; target='parent'</script>");