将Gridview值传递给新WebPage - 无法在新WebPage中编辑值

时间:2015-03-10 13:12:23

标签: asp.net vb.net gridview

我正在使用Visual Studio 2010和Visual Basic来设计Web应用程序。

我有一个gridview,每行都有一个按钮,用于打开一个新网页,并使用点击的行按钮填充字段。工作良好。问题是 - 我希望用户能够编辑新网页上的字段 - 没有运气。改变它,它从gridview变回值。这些字段如何变得可编辑?我当时认为它可能是一个TextChanged事件,但我不能让它工作。

这是我的代码的一部分。提前谢谢!

ASPX
    <%@ Page Title="" Language="vb" AutoEventWireup="true" MasterPageFile="~/Site.Master" CodeBehind="TimeOffNoRequestEdit.aspx.vb" Inherits="timework.TimeOffNoRequestEdit" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<asp:ToolkitScriptManager ID="toolkitScriptManager" runat="server" />
<asp:Label ID="Label2" runat="server" 
     style="color: #990033; font-weight: 700"></asp:Label>
<asp:Label ID="Label1" runat="server" 
     style="color: #000099; font-weight: 700"></asp:Label>
<br />
<label>Employee Name:</label><asp:TextBox ID="TextBox3" runat="server" BorderStyle="None" 
     style="margin-left: 0px; font-size: medium; font-weight: 700;" 
     Width="150px" Height="22px" AutoPostBack="True"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server" Width="129px" 
     AutoPostBack="True" Visible="False"></asp:TextBox>
<br />
<hr align="left" style="width: 791px; margin-left: 0px" />
<label>Date Requested:</label><asp:TextBox ID="DateRequested" runat="server" style="margin-left: 0px; font-size: medium; font-weight: 500;" 
     Width="150px" Height="22px" AutoPostBack="True" BorderStyle="None"></asp:TextBox>
<br />
<p />
<label>Begin Date Off:</label><asp:TextBox ID="TextBox1" runat="server" style="margin-left: 10px; font-size: medium; font-weight: 500;" 
     Width="150px" Height="22px" AutoPostBack="True" BorderStyle="None"></asp:TextBox>
<br />
<label>&nbsp; End Date Off:</label><asp:TextBox ID="TextBox2" runat="server" style="margin-left: 15px; font-size: medium; font-weight: 500;" 
     Width="150px" Height="22px" AutoPostBack="True" BorderStyle="None"></asp:TextBox>
<p />
<label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; All Day?</label>&nbsp;<asp:CheckBox ID=Checkbox1 runat="server" />
<br />
<label>Begin Time Off:</label>&nbsp;<asp:TextBox ID="TextBox6" runat="server" style="margin-left: 12px; font-size: medium; font-weight: 500;" 
     Width="150px" Height="22px" AutoPostBack="True" BorderStyle="None"></asp:TextBox>
<br /> 
<label>&nbsp;&nbsp; End Time Off:</label>&nbsp;<asp:TextBox ID="TextBox7" 
     runat="server" style="margin-left: 12px; font-size: medium; font-weight: 500;" 
     Width="148px" Height="22px" AutoPostBack="True" BorderStyle="None"></asp:TextBox>
<br />
<br />
<label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Reason:<asp:TextBox 
     ID="TextBox5" runat="server" style="margin-left:10px; font-size: medium; font-weight: 500;" 
     Width="150px" Height="22px" AutoPostBack="True" BorderStyle="None"></asp:TextBox></label>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Save" />

</asp:Content>

Visual Basic    

   Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim windowsLoginName As System.String = HttpContext.Current.User.Identity.Name   'System.Security.Principal.WindowsIdentity.GetCurrent().Name

    Dim split As String() = Nothing
    Dim vname As String

    Dim sqlConnection As New SqlConnection("Data Source=janetdev;Initial Catalog=TimeSQL;Persist Security Info=True;User ID=sa;Password=password")
    Dim cmd, cmd1, cmd2 As New SqlCommand
    Dim returnValue, returnValue1, returnValue2 As Object
    Dim dt As Date = Today

    'Get network login name (name only)
    split = windowsLoginName.Split("\".ToCharArray)
    vname = split(1)

    'Get firstname from tblEmployees that matches login name
    cmd.CommandText = "SELECT FirstName FROM tblEmployees where login = '" & vname & "'"
    cmd.CommandType = CommandType.Text
    cmd.Connection = sqlConnection

    'Get lastname from tblEmployees that matches login name
    cmd1.CommandText = "SELECT LastName FROM tblEmployees where login = '" & vname & "'"
    cmd1.CommandType = CommandType.Text
    cmd1.Connection = sqlConnection

    'Get employeeid from tblEmployees that matches login name 
    cmd2.CommandText = "SELECT EmployeeID FROM tblEmployees where login = '" & vname & "'"
    cmd2.CommandType = CommandType.Text
    cmd2.Connection = sqlConnection

    sqlConnection.Open()

    'firstname
    returnValue = cmd.ExecuteScalar()

    'lastname
    returnValue1 = cmd1.ExecuteScalar()

    'employeeid
    returnValue2 = cmd2.ExecuteScalar()

    sqlConnection.Close()

    'display firstname and lastname on screen
    TextBox3.Text = returnValue & " " & returnValue1
    TextBox4.Text = returnValue2

    'display today's date
    DateRequested.Text = dt

    'Get value of other fields 

    If Not [String].IsNullOrEmpty(Request.QueryString("BeginDateOff").ToString()) Then
        'Retrieving the BeginDateOff Value
        TextBox1.Text = Request.QueryString("BeginDateOff").ToString()
    End If

    If Not [String].IsNullOrEmpty(Request.QueryString("EndDateOff").ToString()) Then
        'Retrieving the EndDateOff Value
        TextBox2.Text = Request.QueryString("EndDateOff").ToString()
    End If

    If Not [String].IsNullOrEmpty(Request.QueryString("BeginTimeOff").ToString()) Then
        'Retrieving the BeginTimeOff Value
        TextBox6.Text = Request.QueryString("BeginTimeOff").ToString()
    End If

    If Not [String].IsNullOrEmpty(Request.QueryString("EndTimeOff").ToString()) Then
        'Retrieving the EndTimeOff Value
        TextBox7.Text = Request.QueryString("EndTimeOff").ToString()
    End If
    'Retrieving the All Day Value
    Checkbox1.Checked = Boolean.Parse(Request.QueryString("AllDay_YesNo"))

    If Not [String].IsNullOrEmpty(Request.QueryString("Description").ToString()) Then
        'Retrieving the Description Value
        TextBox5.Text = Request.QueryString("Description").ToString()
    End If

    DateRequested.Focus()

End Sub

1 个答案:

答案 0 :(得分:1)

您需要在填充文本框之前使用if(!PostBack) - 否则您将在每次回发时使用原始值重写它们。

<强>更新

Page_Load会在每次回发,自动(AutoPostBack)或手动(按钮点击)上运行,并且在文本框接收到新值后执行,但之前 / strong>它们由事件处理程序处理。

所以,简短回答,只需将if(PostBack) return;添加到Page_Load的最顶层。

长期的答案是,在做其他事情之前,你真的需要明白这一点:https://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx