zoukankan      html  css  js  c++  java
  • Asp.NET 学习笔记

    Asp.net学习笔记 页面加载时候的事件顺序。

    preinit

    init

     initcomplete

    preload

     load

    loadcomplete

    prerender

    prerendercomplete

     unload 判断是否第一次加载 if (!ispostback)

     

    //生成脚本。

       Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "alert('我不知道')",true);  //这个是在底
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "block", "alert('block里面')", true); //这个是在头
            Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "idonknow", @"http://www.baidu.com"); //这个是一个链接

     

    另外想问下asp.net的回调是不是ajax呢。像是,又不像。

    Asp.net 学习笔记

    Html 控件转换成 服务器控件只需要加 runat=server 再写上id 就OK,因为这个问题自己
    弄了很久,在写事件的时候写onserverclick 但是这里面如果写了onclick 我运行不了。不
    知道是为什么,反正web服务器控件里面有onclientclick 事件了。

    另外就是document.forms[“表单名”],教程上面一直写的是form0,找的例子也是,结果
    VS 里面建的都是form1 ,因为这个问题弄了好久,本来以为form0 相当于是第一个form,
    但是不是这样啊,不晓得当时脑袋是杂写的,要是ID号的话也该是int类型,string 类型
    的就该是名字了。

    自学好恼火。不过还是推荐vs2010,至少有点智能提示,虽然不完全,用vs2008一直以为
    自己是打错了,因为没有提示的。

    ////一个关于回调的例子。

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"
    Trace="false" %>

    <!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>
    <script type="text/javascript">
    function getnumber()
    {
    UseCallBack();
    }
    function GetRandomNumberFormServer(TextBox1,context)
    {
    document.forms["form1"].TextBox1.value=TextBox1;
    }
    </script>
    <body>
        <form id="form1" runat="server">
        <div>
            <input id="Button1" type="button" value="获取一个随机数" onclick=getnumber() />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
       
        </div>
        </form>
    </body>
    </html>

    //.CS文件

    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;

    public partial class _Default : System.Web.UI.Page ,ICallbackEventHandler
    {
        private string _callbackResult = null;
        protected void Page_Load(object sender, EventArgs e)
        {

            string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg",
    "GetRandomNumberFormServer", "context");

            string cbScript = "function UseCallBack(arg,context)" + "{" + cbReference + ";" + "}";

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UseCallBack", cbScript,
    true);

            Response.Write(this.GetType().ToString());

        }

        #region ICallbackEventHandler 成员

        public string GetCallbackResult()
        {
            return _callbackResult;
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
         

            Random rmd = new Random();
            _callbackResult = rmd.Next().ToString();
        }

        #endregion
    }

    ////////string.empty null "" 这三者的区别。 string.empty 就相当于是 "", 但和null 不一样。

     null是个值。 //这话是我说的,以下试出来的。建了一个TextBox1 和 Button1 的控件.

     如果是null 提示的是"字符串不能够为空",换成其它两者是"字符串初始化不能够为空";

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.TextBox1.Text !=null)
            {
                if (this.TextBox1.Text.Trim() != string.Empty)
                {
                    Response.Write(this.TextBox1.Text);
                }
                else
                {
                    Response.Write("字符串不能够为空");
     
                }
            }
            else
            {
                Response.Write("字符串初始化不能够为空");
            }
        }

    ////下面的示例是多个控件调用一个程序处理。

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnCommand="Button_Command"
    CommandName="Button1"/>
            <asp:Button ID="Button2" runat="server" Text="Button" OnCommand="Button_Command"
    CommandName="Button2" />
            <asp:Button ID="Button4" runat="server" Text="Button" OnCommand="Button_Command"
    CommandName="Button3"/>
            <asp:Button ID="Button3" runat="server" Text="Button" OnCommand="Button_Command"
    CommandName="Button4"/>
           
        </div>
        </form>
    </body>

    ///.CS文件


    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;

    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          

        }

        protected void Button_Command(object sender,System.Web.UI.WebControls.CommandEventArgs e)
        {
            switch (e.CommandName)
            {

                case ("Button1"):
                    Response.Write("1.Click");
                    break;
                case ("Button2"):
                    Response.Write("2.Click");
                    break;
                case ("Button3"):
                    Response.Write("3.Click");
                    break;
                case ("Button4"):
                    Response.Write("4.Click");
                    break;
                   
     
            }
        }
      
    }

    ///////下面的示例是一个关于数据绑定的。。绑定的是XML 文件。。这里自己也弄了十多分钟,结果是自己的XML 文件写错了。。

    <?xml version="1.0" encoding="utf-8" ?>
    <Names>
      <Name
        Title="小王"    />
      <Name
     Title="小张"/>
    </Names>

    直接增加数据绑定就OK,XPath 就是设置节点。这里要用Title ,Xpath 就设置成Names/Name

    ////Html 服务器控件和Web服务器控件. 里面如果是serverclick事件就直接就是方法名称,如果是客户端的是 "return  方法名();".比如

      <input id="Button2" type="button" value="客户端"  runat=server onserverclick="ServerDo" onclick="return Button2_onclick();" />
            <asp:Button ID="Button3" runat="server" Text="Button" onclick="Button3_Click" OnClientClick="return Button2_onclick();"/>

    //.cs 文件  ,文件上传的例子.不完善
    protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.FileUpload1.HasFile)
                try
                {
                    FileUpload1.SaveAs(string.Format(@"c:\update\{0}", FileUpload1.FileName));
                    this.Label1.Text = "File name:" + FileUpload1.PostedFile.FileName + "<br>" + "
    长度:" + FileUpload1.PostedFile.ContentLength.ToString() + "属性:" +
    FileUpload1.PostedFile.ContentType.ToString();

                    //System.IO.Stream myStream;
                    //string ok = "sadfasdf";
                    //char[] arrays;
                   
                    //this.Label1.Text = "File name:" + FileUpload1.PostedFile.FileName + "<br>" +
    "长度:" + FileUpload1.PostedFile.ContentLength.ToString() + "属性:" +
    FileUpload1.PostedFile.ContentType.ToString();
                    //FileUpload1.SaveAs(string.Format(@"c:\update\{0}", FileUpload1.FileName));
                    ////this.Label1.Text = "File name:" + FileUpload1.PostedFile.FileName + "<br>"
    + "长度:" + FileUpload1.PostedFile.ContentLength.ToString() + "属性:" +
    FileUpload1.PostedFile.ContentType.ToString();
                    //myStream = FileUpload1.FileContent;

                    //arrays = myStream.ToString().ToCharArray();  //本来在书上是直接就是用的
    ToArray();但是我这不知道怎么没有这个。最后换成CharArray了。

                }
                catch (Exception ex)
                {
                    this.Label1.Text = "Error:" + ex.Message;

                }
            else
            {
                this.Label1.Text = "没有文件上传";
            }
      }
      
    //.aspx 文件
      
        <asp:FileUpload ID="FileUpload1" runat="server" /><br>
            <asp:Label ID="Label1" runat="server"></asp:Label><br>
            <asp:Button ID="Button1"
                  runat="server" Text="提交" onclick="Button1_Click" />
      

  • 相关阅读:
    sql 连表
    Laravel 数据验证
    zend studio 破解、汉化和字体颜色及快捷键相关设置
    关于storm的一些知识点
    storm架构原理及集群部署
    storm使用过程中出现的错误:Caused by: java.net.UnknownHostException: storm: 未知的名称或服务
    ElasticSearch基础知识
    ElasticSearch java客户端更新时出现的错误:NoNodeAvailableException[None of the configured nodes are available
    sublime text3 注册码 (Version 3.0)
    使用HTMLTestRunner生产报告
  • 原文地址:https://www.cnblogs.com/fat_li/p/1891931.html
Copyright © 2011-2022 走看看