|
 上面的程序中,我们只是利用了资源文件的特性而已,下面,我们将实现让程序"记住"用户每次选择语言,就象google那样,记住用户每次的选择后,下次用户在浏览页面时,就会使用该语言的页面了。 首先,要实现这样的功能,我们必须使用asp.net 2.0中新提供的profile功能。Profile可以利用数据库存储关于用户的个性化信息,有点象session对象,但session对象是有生存期的,在生存期后,session对象自动失效了。而profile不同,除非显式移除它。要实现profile功能,必须先在web.config中进行定义,如下:
<system.web> <profile> <properties> <add name="language" type="string"/> <group name="Info"> <add name="DateSelected" type="System.DateTime"/> <add name="LastModified" type="System.DateTime"/> </group> </properties> </profile> 要使用profile属性,可以这样:
Profile.language = "en-US" Profile.Info.LastModified = Now Profile.Info.DateSelected = Calendar1.SelectedDate 在web.congfig中,将会定义一些属性/值,分别存贮将要保存的变量和值,比如lastmodified属性,定义其值是datatime类型,如此类推。而<group>标签,则是将一些相同或类似功能的变量值放在一起。在beta 1中,profile是利用access数据库来保存这些值的。
我们为了要让用户每次在下拉框中选择要用的语言,并将其保存起来,所以在提交按钮中,写入如下代码:
Sub btnSet_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Profile.language = ddlLanguage.SelectedItem.Value End Sub 而为了记录用户每次选择日历中的日期,我们则利用profile.info.dateselected属性来记录,并且用Profile.Info.LastModified记录下用户每次选择日历中日期时的时间,如下代码:
Sub Calendar1_SelectionChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) Profile.Info.DateSelected = Calendar1.SelectedDate Profile.Info.LastModified = Now End Sub 当页面加载时,我们可以取出预先保存在profile对象中的值,然后再在日历控件中显示上次用户显示的日期,以及用户上次选择日期时的时间。
Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Calendar1.SelectedDate = Profile.Info.DateSelected Response.Write("Date 上一页 [1] [2] [3] [4] 下一页 |
|
|
|
|
|
|
|