الدرس (12) تكويد صفحة التعديل


من خلال هذا الدرس سنعمل على إكمال كود صفحة التعديل وسنقدم فيه شرح مبسط عن الحماية
ملاحظة: الكود الي تم تطبيقه بالفيديو
كود صفحة التعديل المستخدم في الفيديو


Imports System.IO
Imports System.Data.SqlClient
Imports System.Data

Partial Class Admin_EditContent
Inherits System.Web.UI.Page

Dim TxTImage As New TextBox

Protected Sub UpLoadThisFile(ByVal upload As FileUpload)

If FUpload.HasFile Then

Dim TheFileName As String = Path.Combine(Server.MapPath("~/ImageUpload/"), Date.Now.ToString("yyyyMMddhhHHmmss") + FUpload.FileName)
upload.SaveAs(TheFileName)

TxTImage.Text = "~/ImageUpload/" + Date.Now.ToString("yyyyMMddhhHHmmss") + FUpload.FileName

SaveContent()

End If

End Sub

Protected Sub SaveContent()
Try

Dim conn As New SqlConnection
conn.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionStringComAhmad").ConnectionString

Dim Sql As String = "UPDATE TContent SET FIDCategory =@FIDCategory , ContentTitle =@ContentTitle , ContentDaTi =@ContentDaTi , ContentSummary =@ContentSummary , ContentImage =@ContentImage , ContentText =@ContentText WHERE IDContent =@IDContent"

conn.Open()

Dim cmd As SqlCommand = New SqlCommand(Sql, conn)
cmd.Parameters.AddWithValue("IDContent", Request.QueryString("IDContent"))

cmd.Parameters.AddWithValue("FIDCategory", TxTCategory.SelectedItem.Value)
cmd.Parameters.AddWithValue("ContentTitle", TxTTitle.Text.ToString)
cmd.Parameters.AddWithValue("ContentDaTi", TxTDtTi.Text.ToString)
cmd.Parameters.AddWithValue("ContentSummary", TxTSumm.Text.ToString)
cmd.Parameters.AddWithValue("ContentImage", TxTImage.Text.ToString)
cmd.Parameters.AddWithValue("ContentText", TxTMore.Text.ToString)

cmd.ExecuteScalar()
conn.Close()

Lbmsg.ForeColor = Drawing.Color.Blue
Lbmsg.Text = "تم حفظ المحتوى بنجاح"

Catch ex As Exception
Lbmsg.ForeColor = Drawing.Color.Red
Lbmsg.Text = "خطأ لم يتم حفظ المحتوى" + ex.ToString

End Try

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Request.QueryString("IDContent") Is Nothing Then
Response.Redirect("ListContent.aspx")
End If

Try

If Not String.IsNullOrEmpty(Request.QueryString("IDContent")) Then

If Not IsPostBack Then

Dim conn As New SqlConnection
conn.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionStringComAhmad").ConnectionString

Dim sql As String = "SELECT * FROM TContent WHERE IDContent = @IDContent"

conn.Open()
Dim dt As DataTable = New DataTable
Dim adp As Data.SqlClient.SqlDataAdapter = New Data.SqlClient.SqlDataAdapter(sql, conn)
adp.SelectCommand.Parameters.AddWithValue("IDContent", Request.QueryString("IDContent"))
adp.Fill(dt)
conn.Close()

If dt.Rows.Count > 0 Then
TxTCategory.SelectedValue = dt.Rows(0)("FIDCategory").ToString
TxTTitle.Text = dt.Rows(0)("ContentTitle").ToString
TxTDtTi.Text = dt.Rows(0)("ContentDaTi").ToString
TxTSumm.Text = dt.Rows(0)("ContentSummary").ToString
TxTImage.Text = dt.Rows(0)("ContentImage").ToString
TxTMore.Text = dt.Rows(0)("ContentText").ToString

Image1.ImageUrl = TxTImage.Text.ToString

Else
Lbmsg.Text = "المحتوى المطلوب غير موجود"
BuSave.Enabled = False
Exit Sub

End If

Else
Lbmsg.Text = "المحتوى المطلوب غير موجود"
BuSave.Enabled = False
Exit Sub
End If

End If

Catch ex As Exception
Lbmsg.Text = "خطأ في عملية جلب المحتوى"
Exit Sub
End Try
End Sub

Protected Sub BuSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BuSave.Click
If FUpload.HasFile Then

Dim ValidatFileTy As String() = {"bmb", "gif", "png", "jpg", "jpeg"}
Dim Ext As String = System.IO.Path.GetExtension(FUpload.PostedFile.FileName)

Dim isValidFile As Boolean = False
For i As Integer = 0 To ValidatFileTy.Length - 1
If Ext = "." & ValidatFileTy(i) Then
isValidFile = True
End If
Next

If Not isValidFile Then
Lbmsg.ForeColor = Drawing.Color.Red
Lbmsg.Text = " الملفات المسموح بها فقط " & String.Join(",", ValidatFileTy)
Exit Sub
Else

UpLoadThisFile(FUpload)

End If

Else

UpLoadThisFile(FUpload)

End If
End Sub
End Class