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.
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.
But 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.
EDIT
Here is the markup for each web form
Webform1
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication2.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView ID="grdCategory" runat="server" AutoGenerateSelectButton="True" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</asp:Content>
Webform2
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="WebForm2.aspx.vb" Inherits="WebApplication2.WebForm2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<br />
<div class="row">
<div class="col-md-2">
<p>Category Name: </p>
</div>
<div class="col-md-10">
<asp:TextBox ID="txtName" Width="300px" runat="server"></asp:TextBox>
</div>
</div>
<div class="row">
<div class="col-md-2">
<p>Description: </p>
</div>
<div class="col-md-10">
<asp:TextBox ID="txtDesc" Width="300px" runat="server" TabIndex="1"></asp:TextBox>
</div>
</div>
<div class="row">
<div class="col-md-2">
</div>
<div class="col-md-10">
<asp:Button ID="btnBack" runat="server" Text="Back" Width="70px" OnClick="btnBack_Click" />
<asp:Button ID="btnSave" Width="70px" Text="Save" runat="server" OnClick="btnSave_Click" />
</div>
</div>
</asp:Content>
Aucun commentaire:
Enregistrer un commentaire