我只是在学习asp.net。好吧,我在ASP.NET网页上有一个小问题。我有2个Web表单,只有一个DataGrId控件的WebForm1,以及一对Label
和TextBox
和2个按钮的WebForm2。
直到点,这是我的WebForm 1:
的代码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
这是我的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
逻辑是,我在datagrid
控件中选择一行,然后将所选行的值传递给WebForm2
中的文本框控件。
但是,在更改文本框的值并按"保存"按钮之后,BTNSAVE_CLICK没有执行,看来TextBox的值也没有更改。我尝试调试,然后按"保存"按钮后,它执行page_load事件,并且没有执行btnsave_click事件。
问题,任何人都知道btnsave_click事件未执行以及如何修复它的原因,因此在我按下按钮后可以执行BTNSAVE_CLICK。
edit
这是每个Web表单的标记
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>
在您的grd_selectedIndex更改呼叫中,您需要从单元格中获取数据并将其放在可以在页面之间传递的东西中查询字符串,具体取决于您需要做什么。
在单独的音符上,我不会使用2个单独的页面开发它,只需将第二页的内容放入第一页的面板中,那么您就不必从网格中拉出它容器,来回走动。