-- 精品下载 | 实用查询 | 词典查询 | 桌面壁纸 | 网址 | 笑话 | 小游戏 | 天气文章资讯 | 收录查询 | 证件办理 | 闪字生成 | 广告代码 | 在线手册 | 有问必答
您现在的位置: 蓝派网 >> 文章中心 >> 网络编程 >> .NET >> ASP.NET >> 正文
站内文章搜索:           

asp.net实用的一些代码

作者:佚名    文章来源:网络    更新时间 :2007-5-30 15:38:41
如何用Response.Redirect方法传递汉字 :
页面1:String str=Server.UrlEncode("dddddd");
            Response.Redirect("页面2?msg="+str);
页面2:String str=Server.UrlDecode(Request.QueryString["msg"]);
--------------------------------------------------------------------
如何处理多页面重定向到同一页面后的返回问题 :
页面1:Response.Redirect(页面2);
页面2:方法1:Button2.Attributes.Add("onclick","history.go(-1);return false;");
      方法2:private void Button1_Click(object sender, System.EventArgs e)
        {
            Response.Write("<script>history.go(-2);</script>");
        }
-----------------------------------------------------------------------------------------------------------------------------
IsStartupScriptRegistered("函数名")和IsClientScriptBlockRegistered("函数名")   :用于标志关键字为函数名的脚本块是否已添加的页面中
-------------------------------------------------------------------------------
<body onload="closeit()">
<script language="JavaScript">
function closeit() {
setTimeout("self.close()",3)}
</script>
------------------------------------------------------------------
onbeforeunload="return ' 真的转向啊'">
function closeIt()
{
if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey)
{alert('我要退出啦!')}
else
{return false;}
}
----------------------------------------------------------------------------------------
<script>
if (document.all("hidd").value !="")
{
alert(document.all("hidd").value);
document.all("hidd").value="";
window.parent.location="index.aspx";
}
</script>
加在</form>
----------------------------------------------
../文件名
-------------------------------------
target="_parent"//子窗体从父窗体打开

function confirmDelete (frm)
{
for (i=0; i<frm.length; i++)
{
if (frm.elements[i].name.indexOf('CheckBox1') !=-1)
{
if(frm.elements[i].checked)
{
return confirm ('您是否要删除选择的记录?');
}
}
    }
}
<BODY onUnload="window.alert('一路走好')"><br>
--------------------------------------------------------------------------------------------------------------
<object id="id" ruant=server latebing=true/flase class="class name" || progid="com progid" || classid="com classid">
   /id属性用来设置所建立的com或.net对象的唯一名称
/class属性用来指定您要根据哪一个.net framework类来建立.net对象.
/progid属性用来指定您据以建立的com组件的程序控制识别码.
/classid属性用来指定你据以建立的com组件的类识别码
/latebinding属性指示是否要将后期绑定的api用于T1bimp.exe已处理过的com组件.
  ★-class,progid,classid属性是彼此互斥,不能同时存在一个服务器端对象标记中
DataTable d=new DataTable();  d.Columns.Add("fsdf3");  DataRow t=d.NewRow();  t[0]=45645;d.Rows.Add(t);

<meta http-equiv="refresh" content="10;url=top.aspx">自动刷新

ImageButton1.Attributes.Add("onclick","javascript:window.open('test.aspx');return false;");
//禁止网页刷新
Response.Write("<script>parent.document.frames['topIndex'].location.href='top.aspx'</script>");

using System.Web.Security;
加密 密码!~
FormsAuthentication.HashPasswordForStoringInConfigFile(Text,"MD5")

---------------------------自定义控件-------------------------------------------------------------
Control:定义由所有 ASP.NET 服务器控件共享的属性、方法和事件。

<%@ Register Tagprefix="LoginControl" TagName="Login" Src="WebUserControl1.ascx" %>
Tagprefix :决定用户控件的唯一的命令空间。TagName:用户控件的唯一名称。
---------------读取xml---------------------------------
DataSet ds=new DataSet();
ds.ReadXml(Server.MapPath("客户.xml"));
System.Data.DataView dv=new DataView(ds.Tables["客户"]);
DataGrid1.DataSource=dv;
DataGrid1.DataBind();
---------------------------------------
用户资料保存于web.config时
private void butlogin_Click(object sender, System.EventArgs e)
{
   if(FormsAuthentication.Authenticate(boxid.Text,boxpd.Text))
     FormsAuthentication.RedirectFromLoginPage(boxid.Text,false);
   else
     Response.Write("error");

---------------------------------------------
cookie-----------------------------------------------------------------
HttpCookie MyCookie;
MyCookie = Request.Cookies["UserLayout"];//获得客服端发送的Cookie的集合
HttpCookie MyCookie = new HttpCookie("UserLayout");
MyCookie.Values.Add("ForeColor","rty");// 设定 Cookie 中各项目(索引键) 的值
MyCookie.Expires = DateTime.MaxValue;// 设定 Cookie 永远不会过期
Response.AppendCookie(MyCookie);
// DateTime dt = DateTime.Now;
// TimeSpan ts = new TimeSpan(0, 0, 10, 0);
// MyCookie.Expires = dt.Add(ts);
------------------datagrid中得到行--------------------------------------------------------------------------
private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.SelectedItem) || (e.Item.ItemType == ListItemType.EditItem))
{System.Data.Common.DbDataRecord drv = (System.Data.Common.DbDataRecord)e.Item.DataItem;
     if (Convert.ToInt32(drv["min_lvl"]) > 60)
e.Item.BackColor = Color.Red;}}

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//foreach(DataGridItem i in this.DataGrid1.Items)
//{i.Cells[2].Attributes.Add("onmouseover","this.style.background='red'");
//i.Cells[2].Attributes.Add("onmouseout","this.style.background='white'");}
e.Item.Attributes.Add("onmouseover","this.style.background='red'");
e.Item.Attributes.Add("onmouseout","this.style.background='white'");}
代理:
this.DataGrid1.ItemDataBound+= new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
onmouseover="this.style.background='red'" onmouseout="this.style.background='blue
-------------------javascript----------------------------------------------------
Response.Write(@"<script language=javascript>alert('请输入正确信息!')</script>");
Page.RegisterStartupScript("Startup",@"<script>alert('gfhfh!')</script>");
    alert("确定");
    document.write("谢谢!");
    if(confirm("是否要关闭当前窗口?")) {window.close();}
数字型 (Number)
逻辑型或布尔型 (Boolean)
字符串型(String)
空型 (null)
eval 函数
isNaN 函数
onClick  onChange  onFocus  onBlur  onMouseOver onMouseOut  onLoad  onSubmit  onMouseDown  onMouseUp
text事件: onFocus="接受焦点处理例程; "  onBlur="失去焦点处理例程; "  onChange="改变内容处理例程; "
●得到单选按钮var the_box = window.document.form_1.radio_1;
        if (the_box.checked == true){
             window.document.form_1.radio_2.checked = false
●组合框与列表框事件onFocus  onBlur  onChange
function show(txt)
{
  alert("选中的是"+ txt);
}
<select name="choose_category"    
onChange="show(the_form.choose_category.options[selectedIndex].text);">
 <option selected>狗</option>
 <option>鱼</option>
 <option>鸟</option>
</select>
Window 对象  属性document   event   history   location    name   screen  
             方法alert   blur   close    focus   navigate   open
Document 对象表示给定浏览器窗口中的 HTML 文档,用于检索有关该文档的信息。  
         属性alinkColor bgColor body fgColor linkColor location title URL vlinkColor   
         方法clear   close     open     write   writeln  
History对象  方法 back  forward   go
Location 此对象包含有关当前 URL 的信息,它提供了重新加载窗口的当前 URL 的方法。
         属性hash   host   hostname   href  
         方法assign   reload   replace

-------------------连接数据库-----------------------------------------------------------
DataBinder 类 :提供对 RAD 设计器的支持,以生成和分析数据绑定表达式语法
SqlConnection 表示 SQL Server 数据库的一个打开的连接。不能继承此类。
                ("server=.;uid=sa;pwd=;database=pubs")
SqlCommand: 表示要对 SQL Server 数据库执行的一个 Transact-SQL 语句或存储过程。
            CommandTimeout 属性: 获取或设置在终止执行命令的尝试并生成错误之前的等待时间。
            CommandText 属性: 获取或设置要对数据源执行的 Transact-SQL 语句或存储过程。
            CommandType 属性: 获取或设置一个值,该值指示如何解释 CommandText 属性.
                        取值: StoredProcedure 存储过程
            Parameters  集合

SqlDataReader 类:  提供一种从数据库读取只进的行流的一种方式。不能继承此类。
           -CommandBehavior 枚举:  提供对查询结果和查询对数据库的影响的说明
            --成员: SingleRow  查询应返回一行。执行查询可能会影响数据库状态。一些 .NET Framework 数据提供程序可能(但不要求)使用此信息
                    来优化命令的性能。在用 OleDbCommand 对象的 ExecuteReader 方法指定 SingleRow 时,用于 OLE DB 的 .NET Framework 数据提
                    供程序使用 OLE DB IRow 接口(如果可用)执行绑定。否则,它使用 IRowset 接口。如果您的 SQL 语句应该只返回一行,
                    则指定 SingleRow 还可以提高应用程序性能.在执行返回多个结果集的查询时,可以指定 SingleRow。在这种情况下,仍返回多个结果集,
                    但每个结果集只有一行。

SqlDataAdapter 类 :表示用于填充 DataSet 和更新 SQL Server 数据库的一组数据命令和一个数据库连接.
                   SqlDataAdapter 与 SqlConnection 和 SqlCommand 一起使用,以便在连接到 Microsoft SQL Server 数据库时提高性能。
绑定: sql ★方法一:    用sqlDataAdapter连接
                    this.sqlDataAdapter1.Fill(dataSet11);
              this.DataGrid1.DataBind();
          ★方法二:    用sqlConnection连接---先sqlConnection(设置ConnectionString连),再SqlCommand(设置Connection);
            写代码 this.sqlConnection1.Open();
SqlDataReader dr=this.sqlCommand1.ExecuteReader();//ExecuteReader将CommandText发送到Connection并生成一个 SqlDataReader。
DataGrid1.DataSource=dr;
DataGrid1.DataBind();
dr.Close();
this.sqlConnection1.Close();
数据对象与界面工具更新实际演练  ★方法一:  用sqlConnection连接

●用sqlCommand连接:
MyCommand.Parameters.Add("@name",SqlDbType.NVarChar,20);
MyCommand.Parameters["@name"].Value=name.Text;
●用sqlDataAdapter连接:
MyCommand.Parameters.Add(new SqlPArameter("@name",SqlDbType.NVarChar,20));
MyCommand.Parameters["@name"].Value=name.Text;
DataSet ds=new DataSet();
DataView dv;
dataView 表示用于排序、筛选、搜索、编辑和导航的 DataTable 的可绑定数据的自定义视图
      dv =ds.Tables["Table-name"].DefaultView;
----------------------控件-------------------------------------------------------------------------------------------------
●RadioButton 控件 :控件组GroupName ;
●CustomValidator 控件 :计算输入控件的值以确定它是否通过自定义的验证逻辑。
●Label :标记 说明
        <a> 将内部文本转换为超级链接。超级链接文本不能包含其他格式标记。
        <b> 将内部文本转换为粗体样式。
        <br> 换行。
        <i> 将内部文本转换为斜体样式。
        <p> 开始新段落;或者,在与结束标记一起使用时,将内部文本放置于单独的段落中。
●Literal 控件: 在 Web 页上保留显示静态文本的位置。(位置:可将控件加至Panel容器中)
  Literal 控件与 Label 控件类似,但 Literal 控件不允许对所显示的文本应用样式。可以通过设置 Text 属性,以编程方式控制在控件中显示的文本。
●TextBox 控件 :TextMode属性 文本框行为模式(Password 密码).  Wrap 属性 自动换行. MaxLength 属性 限制输入的字符数(TextMode设为MultiLine无效).
                ReadOnlys属性 是否只读.  
●CheckBoxList 控件: selected 属性 设置起始勾选状态.
●DataGrid 控件 :AutoGenerateColumns属性设为true,表示它会自动替数据源中的每一个字段创建BoundColumn对象。
                 AutoGenerateColumns属性设为false,html--<Columns>标记之间列出想要内含的字段。
                 --DataField 设置要绑定至BoundColumn的字段。
                 --DataFormatString 设置列中项的显示格式。
                 --FooterText 设置要显示于列页脚节中的文本。
                 --HeaderImageUrl 设置图,此图会取代HeaderText.
                 --HeaderText 设置要显示于列页眉节中的文本。
                 --ReadOnly 设置是否可以编辑列中的项。
                 --SortExpreession 在选取列来排序时,要传递至OnSortCommand方法的字段或表达名称。(DataField)
                 --Visible 设置列是否会显示于DataGrid服务器控件中。
                 --FooterStyle-property 设置列页脚节的样式属性。
                 --HeaderStyle-property 设置列页眉节的样式属性。
                 --ItemStyle-property 设置列中项的样式属性。
                 <ItemStyle BackColor="#ccffff"></ItemStyle>项目风格
●HyperLink 控件 :显示到其他 Web 页的链接的控件。
-------样式-------------------------------------------------------------------------------------------------------------
DataGrid服务器控件:样式对象属性的优先顺序.1.EditItemStyle 2.SelectedItrmStyle 3.AlternatingItemStyle 4.ItemStyle 5.ControlStyle
<LINK REL="stylesheet" TYPE="text/css" HREF="MyStyles.css">

----------------------函数----------------------------------------------------------------------------------------------
Convert.ToSingle 方法 :将指定的值转换为单精度浮点数字。
  IsPostBack 获取一个值,该值指示该页是否正为响应客户端回发而加载,或者它是否正被首次加载和访问。
String.Trim() 从此实例的开始位置和末尾移除一组指定字符的所有匹配项。

★HttpBrowserCapabilities 类 :使服务器能够收集有关在客户端运行的浏览器功能的信息。
★/当您要在运行时动态添加、移除或依次通过控件时,向页上添加 PlaceHolder Web 服务器控件。
★/Int16.Parse 方法 :将数字的字符串表示形式转换为它的等效 16 位有符号整数。
★LiteralControl类 :表示 HTML 元素、文本和 ASP.NET 页中不需要在服务器上处理的任何其他字符串。
★UnitType枚举 :指定度量单位。(% Percentage 度量单位是父元素的相对百分比) Image1.Width=new Unit(string );

★Context 提供对整个当前上下文(包括请求对象)的访问。您可以使用此类共享页之间的信息。继承HttpContext类 ;
       ---(类名--(网页名))Context.Handler 属性 :为当前 HTTP 请求获取或设置 IHttpHandler 对象。
★网页传值:
  方法一\ WebForm1--    public string str=TextBox1.Text;
                       Server.Transfer("WebForm2.aspx");//终止当前页的执行,并为当前请求开始执行新页。
                       //Server.Execute 它将从 writer 接收的 HTML 流写入 HTTP 输出流。使用另一页执行当前请求。
         WebForm2--接收 // <%@ Reference Page="WebForm1.aspx"%>//需在WebForm2开头处加入
                        WebForm1 w;
                  w = (WebForm1)Context.Handler;  //为当前 HTTP 请求获取或设置 IHttpHandler 对象。
            this.Label1.Text = w.str;
  方法二\ WebForm1--    Session["t"]=TextBox1.Text; Response.Redirect("WebForm2.aspx");
          WebForm2--   Label1.Text=(String)Session["t"];
  方法三\ WebForm1--   Response.Redirect("WebForm2.

[1] [2] 下一页


发表评论】【打印此文】【关闭窗口】【点击数:
  • 上一篇文章: 没有了
  • 下一篇文章:
  • 网友评论: