◎以上程式範例Profile.aspx,如在頁框下不能操作,請開新視窗操作
◎如果有問題歡迎您提出,dnowba很需要有人和我一起討論
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'=============================
'== 參考網址:http://msdn.microsoft.com/zh-tw/library/bb515342.aspx
'=============================
'=====如果未經過Login的檢查,就不允許進入網頁,跳回Login畫面
If Me.User.Identity.IsAuthenticated = False Then
FormsAuthentication.RedirectToLoginPage()
End If
Me.Label1.Text = "非常感謝您的使用," & Me.Page.User.Identity.Name.ToString & "先生/小姐,以下是您的個人訊息設定:"
'======尚未設定 Profile的人,看不見 Panel1 這一區
If String.IsNullOrEmpty(Profile.u_back) Then
'=====上面的判別式也可以寫成
'=====If Profile.u_back =""
Me.Panel1.Visible = False
Else
'=====這裡一定要加 ispostback 的檢查,不加的話,當按下button時底下這些資料會先load結果資料無法更新
'=====其實是資料有更新,只不過是先load之前存的資料後再update,這樣就犯了邏輯上的錯誤
If Not IsPostBack = True Then
Me.Panel1.Visible = True
'=====以下是讀取儲存好的資料
'=====套用panel的背景色,本來是寫成
'Me.Panel1.BackColor = Profile.u_back
'=====查了一下,web控制項的色彩不能用string的方式來做,所以改成以下
Me.Panel1.BackColor = Drawing.Color.FromName(Profile.u_back)
Me.TextBox1.Text = Profile.u_電話
Me.RadioButtonList1.SelectedValue = Profile.u_sex
Me.Calendar1.SelectedDate = Profile.u_生日
Me.DropDownList1.SelectedValue = Profile.u_back
End If
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'=====在資料儲存前,設計一個錯誤處理「try...catch....end try」的陳述式
Try
'=====以下三個判別式,都是在驗證資料是否有填入,沒有填資料的話會跳出警告視窗
'=====有趣的是驗證的方式真的是五花八門
If Not String.IsNullOrWhiteSpace(RadioButtonList1.SelectedValue.ToString) Then
Profile.u_sex = Me.RadioButtonList1.SelectedValue
Else
'====不填寫資料,則出現警告視窗
Throw New Exception("尚未選擇性別")
End If
If Me.Calendar1.SelectedDates.Count >= 1 Then
Profile.u_生日 = Me.Calendar1.SelectedDate
Else
Throw (New Exception("尚未選擇生日"))
End If
If Not String.IsNullOrWhiteSpace(Me.TextBox1.Text) Then
Profile.u_電話 = Me.TextBox1.Text
Else
Throw New Exception("請填寫電話")
End If
Profile.u_back = Me.DropDownList1.SelectedValue.ToString
'====儲存資料到profile資料表裡,上面的Profile變數,都已經事先設定在 Web.Config檔案裡面了
Profile.Save()
'====在存完資料後馬上套用背景顏色,其他資料則因為ispostback=true而不再向資料庫拿資料
'====,會給人一種以為是即時update的錯覺(當然不像ajax,畫面還是有跳一下)
Me.Panel1.BackColor = Drawing.Color.FromName(Profile.u_back)
Catch ex As Exception
'====不填寫資料,則出現警告視窗(警告視窗,是動態加入網頁裡面)
Dim dnowMsg As New Literal
dnowMsg.Text = "<script>alert('" & ex.Message.ToString() & "')</script>"
Me.Page.Controls.Add(dnowMsg)
End Try
End Sub
End Class
沒有留言:
張貼留言