当把一个TextBox控件ReadOnly属性设置为True后,这个控件就不回传了。
很奇怪,以前都没有过这样的怪问题,在别人机器上都能正常,想到可能和ASP.NET的版本有关系。
我用的是 ASP.NET 2.0,当改成 ASP.NET 1.0,就正常了,TextBox控件ReadOnly=True,能回传。
在 ASP.NET 1.0 中是ReadOnly=True会回传的。
在 ASP.NET 2.0 中是ReadOnly=True就不会回传了。
解决的方法:
<asp:textbox id="Username" runat="server" ReadOnly="True"></asp:textbox>
去掉ReadOnly="True",改成
<asp:textbox id="Username" runat="server"></asp:textbox>
在后台代码中加入,控制只读属性
Username.Attributes.Add("Readonly", "True");
这样在 ASP.NET 2.0 中只读的TextBox控件也能实现回传。
<asp:datagrid id="DataGrid1" runat="server" BorderWidth="1px" AutoGenerateColumns="False"
Width="95%" BorderColor="#CCDFF4" AllowPaging="True" CssClass="table"
DataKeyField="id" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="Name" HeaderText="名称"></asp:BoundColumn>
<asp:ButtonColumn HeaderText="操作" Text="删除" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
}
需要将 ButtonColumn 绑定事件
private void DataGrid1_ItemCommand(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
switch(((LinkButton)e.CommandSource).CommandName)
{
case "Delete":
DataGrid1_Delete(sender, e);
break;
default:
// Do nothing.
break;
}
}
private void DataGrid1_Over(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int id = (int)DataGrid1.DataKeys[e.Item.ItemIndex]; // 返回 DataKeyField 的行值
}
// 创建表格时,每创建一行,触发一次
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType != ListItemType.Header&&e.Item.ItemIndex>=0)
{
// e.Item.Controls 当前行所控件
LinkButton linkButton = (LinkButton)e.Item.Cells[1].Controls[0];
linkButton.Attributes.Add("onclick", "return confirm('您确定删除么?')");
}
}
IsPostBack只有在页面载入里才为true,
当点击按钮事件时,为false。
dropDownList.DataSource = dataSet.Tables[0].DefaultView;
dropDownList.DataTextField = "Name";
dropDownList.DataValueField = "ID";
dropDownList.DataBind();
dropDownList.Items.Insert(0,new ListItem("选择选择",""));
private void RAR()
{
// 把WinRAR目录复制到网站目录下。
string pathRoot = Server.MapPath("/");
string winRAR = pathRoot + "WinRAR/WinRAR.exe";
// 把Job.aspx和Default.aspx这两个文件添加到压缩包Job.rar中
string arguments = @"a Job.rar Job.aspx Default.aspx";
try
{
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = winRAR; // WinRaR.exe目录
info.Arguments = arguments; // 参数
info.WorkingDirectory = pathRoot;
Process p = new Process();
p.StartInfo = info;
p.Start();//启动
Response.Write("压缩成功。");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
下面代码在ASP.NET中,会因权限问题被拒绝访问。
System.DirectoryServices.DirectoryEntry directoryEntry;
directoryEntry = new System.DirectoryServices.DirectoryEntry("IIS://localhost/w3svc/1");
directoryEntry.Invoke("Start", new object[] { });
错误:拒绝访问。
异常详细信息: System.UnauthorizedAccessException: 拒绝访问。
解决方法
修改web.config文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<identity impersonate="true" userName="administrator" password="777777"/>
</system.web>
</configuration>
在ASP.NET里面使用COM对象的时候,会被拒绝访问。
可以在web.config里面添加下面代码解决
<identity impersonate="true" userName="username" password="password"/>
public class BasePage : System.Web.UI.Page
{
public BasePage () {}
}
public class Test : BasePage
{
public Test()
{
MessageBox.Alert(text);
// 下里还会被执行
// MessageBox.Alert 中的 System.Web.HttpContext.Current.Response.End(); 不起作用
// Response.End() 在构造函数中不起作用
System.IO.File.WriteAllText("C:/1.txt", "1");
}
}
public class MessageBox
{
public static void Alert(string text)
{
System.Web.HttpContext.Current.Response.Write(text);
System.Web.HttpContext.Current.Response.End();
}
}
解决方法
不要在构造函数中执行,在 Page_Load 中执行
public class Test : System.Web.UI.Page
{
public Test()
{
// 停止了输出
System.Web.HttpContext.Current.Response.End();
// 下面的代码还会执行
System.IO.File.WriteAllText("C:/1.txt", "Test");
}
}
public class Test : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
System.Web.HttpContext.Current.Response.End();
// 下面的代码不会执行
System.IO.File.WriteAllText("C:/1.txt", "Page_Load");
}
}
ASP.NET 的配置文件 web.config 中的 customErrors 元素,可以自定义处理错误信息。
<customErrors defaultRedirect="url" mode="On|Off|RemoteOnly">
<error statusCode="500" redirect="url"/>
<error statusCode="404" redirect="url"/>
</customErrors>
customErrors 元素的属性
1.defaultRedirect
发生错误时浏览器指向的默认 URL。
2.mode
1)On 启用自定义错误。
2)Off 禁用自定义错误。这允许显示详细的错误。
3)RemoteOnly 仅向远程客户端显示自定义错误,并向本地主机显示 ASP.NET 错误。
error 元素的属性
statusCode
具体的错误代码。
redirect
发生指定错误时浏览器指向的 URL。
上面当发生500错误时,会跳到一指定的页面。
这比较实用,不用设置Web服务器,就可自定义,处理错误。
经常在基类的构造函数中获取用户的信息,比如Cookie、Session等,但Session在构造时却还不存在。
不过在OnInit时就可读取Session值了。
public partial class BasePage : System.Web.UI.Page
{
public BasePage()
{
// 这里Session对象还未创建
if (System.Web.HttpContext.Current.Session == null)
{
System.Web.HttpContext.Current.Response.Write("Session Is Null");
}
else
{
System.Web.HttpContext.Current.Response.Write("Session Is Exist");
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
// 这里就可以访问Session对象了
if (System.Web.HttpContext.Current.Session == null)
{
System.Web.HttpContext.Current.Response.Write("Session Is Null");
}
else
{
System.Web.HttpContext.Current.Response.Write("Session Is Exist");
}
}
}
今天突然发现Server.UrlEncode和HttpUtility.UrlEncode对字符编码输出不一样。
string str = "皮革";
Response.Write(Server.UrlEncode(str));
Response.Write("<br/>");
Response.Write(System.Web.HttpUtility.UrlEncode(str));
输出显示
%c6%a4%b8%ef
%e7%9a%ae%e9%9d%a9
在网上搜索了下,原来是编码问题。HttpUtility.UrlEncode默认是以UTF8进行编码,而Server.UrlEncode则以默认的编码进行编码。
指定编码后就一样了。
string str = "皮革";
Response.Write(Server.UrlEncode(str));
Response.Write("<br/>");
Response.Write(System.Web.HttpUtility.UrlEncode(str, Encoding.GetEncoding("GB2312")));