chao's profile清凉水都PhotosBlogListsMore Tools Help

Blog


    September 22

    ASP.NET页面间传递参数的方法

    del.icio.us 标记: ,

      好久没编程序了,看见许多人在网上找页面间传参的方式,就简单的写点,也算留个备份。

      主要来将有两种:Get与Post,至于两者的区别,还是自己找相关的资料吧,多的很,这里具体讲下操作。在ASP.Net中,获取参数一般都是在Page_Load的时候,当然你也可以在其他的地方调用,看情况而定。先拿Get来说,传入的URL是

    ~\food_info.aspx?food_id={0}

    那么对应的接受方法为

    Request.QueryString.GetValues("food_id").GetValue(0)

      剩下的工作就是对接受的数据进行转换了,如果你有好几个food_id的值的话,更改GetValue中的数字就可以了。

      不过这种参数传递安全性是个问题,毕竟参数公开了,即使加密也不安全。所以就要利用到Post的方法,在发送页面中可以用Form的Action属性设置传入页面,也可用每个控件的PostBackUrl来配置,发送后,就是接受了,这个要实现的判断一下,下面结合代码说明。

    if (Page.PreviousPage != null)
            {
                Response.Write(Page.PreviousPage.Form.Method.ToString()+"<br/>");
                Label lab2 = (Label)Page.PreviousPage.FindControl("lab1");
                Response.Write(lab2.Text.ToString());
            }

      首先判断一下是否有传入页面,判断Page.PreviousPage是否为空,不为空在做相应的事情,聪明的你应该想到了,如果判断一下是来自哪个页面的不更好,的确,可以实现一些更复杂的功能,不过也相应的加大了服务器的负担,要根据实际的业务流程来看。下面就是两种获取方法了,可以从Form中获取,也可以直接获取控件的名称,注意这里要类型转换,所以要记住类型。如果是纯控件设计,就直接找控件好了,如果不行就用Form的方法了,根据个人喜好了。

      好了就简单的介绍了一下,如果还有什么问题,留言给我,大家讨论才有趣嘛!!!

    Comments (1)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    No namewrote:
    Welcome to enter (wow gold) and (wow power leveling) trading site, (Rolex) are cheap, (World of Warcraft gold) credibility Very good! Quickly into the next single! Key words directly to the website click on transactions!
    Aug. 12

    Trackbacks

    The trackback URL for this entry is:
    http://ebakeshop.spaces.live.com/blog/cns!1A66D6C80502E805!264.trak
    Weblogs that reference this entry
    • None