Actually i am just learning asp.net. Well i have a little problem with asp.net web page. I have 2 web forms, WebForm1 with just a single DataGrid control, and WebForm2 with a pair of Label and TextBox, and 2 Buttons. Well straight to the point, here is my code for WebForm1:
Imports System.Configuration.ConfigurationManager
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strConn As String = ConnectionStrings("ConnStr").ConnectionString
Dim sqlConn As SqlConnection = New SqlConnection(strConn)
Dim sqlComd As SqlCommand = New SqlCommand
Dim sqlRead As SqlDataReader = Nothing
Dim sqlParm As SqlParameter
sqlParm = New SqlParameter("@Criteria", SqlDbType.NVarChar, 50)
sqlParm.Value = ""
sqlComd.Connection = sqlConn
sqlComd.CommandText = "ShowCategory"
sqlComd.Parameters.Add(sqlParm)
sqlComd.CommandType = CommandType.StoredProcedure
Try
sqlConn.Open()
sqlRead = sqlComd.ExecuteReader()
grdCategory.DataSource = sqlRead
grdCategory.DataBind()
Catch ex As Exception
Throw ex
Finally
sqlConn.Close()
End Try
End Sub
Private Sub grdCategory_SelectedIndexChanged(sender As Object, e As EventArgs) Handles grdCategory.SelectedIndexChanged
Server.Transfer("~/WebForm2.aspx", False)
End Sub
End Class
And here is my code for WebForm2:
Imports System.Configuration.ConfigurationManager
Imports System.Data.SqlClient
Public Class WebForm2
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim prevPage As Page = Me.Page.PreviousPage
If prevPage IsNot Nothing Then
Dim ctn As ContentPlaceHolder = CType(prevPage.Master.FindControl("MainContent"), ContentPlaceHolder)
Dim grd As GridView = CType(ctn.FindControl("grdCategory"), GridView)
ViewState("ID") = grd.SelectedRow.Cells(1).Text
txtName.Text = grd.SelectedRow.Cells(2).Text
txtDesc.Text = grd.SelectedRow.Cells(3).Text
End If
End If
End Sub
Protected Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
Response.Redirect("~/WebForm1.aspx")
End Sub
Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim strConn As String = ConnectionStrings("ConnStr").ConnectionString
Dim sqlConn As SqlConnection = New SqlConnection(strConn)
Dim sqlComd As SqlCommand = New SqlCommand
Dim sqlParm(2) As SqlParameter
sqlParm(0) = New SqlParameter("@CategoryID", SqlDbType.Int, 0)
sqlParm(1) = New SqlParameter("@CategoryName", SqlDbType.NVarChar, 15)
sqlParm(2) = New SqlParameter("@Description", SqlDbType.NText)
sqlParm(0).Value = ViewState("ID").ToString
sqlParm(1).Value = txtName.Text.Trim
sqlParm(2).Value = txtDesc.Text.Trim
sqlComd.Parameters.AddRange(sqlParm)
sqlComd.CommandText = "InsertCategory"
sqlComd.CommandType = CommandType.StoredProcedure
Try
sqlConn.Open()
sqlComd.ExecuteNonQuery()
Catch ex As Exception
Throw ex
Finally
sqlConn.Close()
End Try
End Sub
End Class
The logic is, I select a row in datagrid control and pass the values of the selected row to the textbox control in WebForm2.
The problem is, after I change the value of the textbox and I press save button, the btnSave_Click did not executed and it seem the value of the textbox did not change too. I try debugging, and after I press the save button, it execute the page_load event and did not execute the btnSave_Click event.
The questions, is anyone know the reason why the btnSave_Click event did not executed, and how to fix it, so the btnSave_Click can executed after I press the button. Thank you before and sorry for my bad english.
Aucun commentaire:
Enregistrer un commentaire