Guy Cothal
Guy Cothal

Reputation: 1348

Server.Transfer Issues

Here is what I am trying for:

I have the first page (ListTraining.aspx) whose job is to display a list of trainings and allow you to select to Edit or View that training. the 2nd page (EditTraining.aspx) is suppose to give you the ability to edit the Training information.

What is happening:

When I click on Edit on ListTraning, it does a server.transfer to Editing training. I am able to edit the Textboxes and Checkboxes without an issue. The issue comes when I click the Save Training button, it goes right back to the ListTraining page without seeing the click event. Upon debugging, I noticed all the Textboxes and Checkboxes had no values, IsPostBack is always false and I can't seem to figure out how to get into the event.

The Code:

ListTraining.aspx

<%@ Page Title="Training List" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="ListTraining.aspx.vb" Inherits="AFN_Personnel_Server.ListTraining" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Header" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
   <div class="card">
       <h1 class="card-header">Training List</h1>
           <asp:GridView ID="gvTrainings" runat="server" HeaderStyle-CssClass="thead-dark" CssClass="table table-striped" DataKeyNames="TrainingId">
               <Columns>
                   <asp:BoundField HeaderText="Training Name" DataField="Name" />
                   <asp:ButtonField ButtonType="Button" CommandName="it" Text="Edit"  />
                   <asp:ButtonField ButtonType="Button" CommandName="vw" Text="View"  />
               </Columns>
           </asp:GridView>
       </div>
   </div>
</asp:Content>

ListTraining.aspx.vb

Public Class ListTraining Inherits System.Web.UI.Page
    Dim _trainId As Integer = 0
    Public Property trainId() As Integer
        Get
        'Get and Set stuff
    End Property
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not PreviousPage Is Nothing Then
            If PreviousPage.Title = "Add Employee" Then
                Dim oPage = DirectCast(PreviousPage, AddEmployee)
                lError.Text = oPage.pageError
            End If
        End If
        'Get Data
        gvTrainings.DataSource = lQ
        gvTrainings.DataBind()
    End Sub
    Private Sub gvTrainings_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gvTrainings.RowCommand
        _trainId = gvTrainings.DataKeys(e.CommandArgument)("StaffId")
        If e.CommandName.ToLower = "it" Then
            _trainId = gvTrainings.DataKeys(e.CommandArgument)("TrainingId")
            Server.Transfer("~/Training/EditTraining.aspx", False)
        End If
        If e.CommandName.ToLower = "vw" Then
            _trainId = gvTrainings.DataKeys(e.CommandArgument)("TrainingId")
            Server.Transfer("~/Training/ViewTraining.aspx", True)
        End If
    End Sub
End Class

EditTraining.aspx

<%@ Page Title="" Language="vb" EnableViewState="True" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="EditTraining.aspx.vb" Inherits="AFN_Personnel_Server.EditTraining" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <span class="input-group-text">Training Name</span>
    <asp:TextBox ID="tbTrainingName" runat="server" CssClass="form-control" placeholder="ex:// CPR" aria-describedby="basic-addon1"></asp:TextBox><br>
    <asp:CheckBox ID="cbInterval" runat="server" /><label for="cbInterval" class="form-control">Done on an interval (ex// yearly or bi yearly)</label><BR>
    <span class="input-group-text">Interval in Years</span><asp:TextBox ID="tbIntervalTime" runat="server" CssClass="form-control" TextMode="Number" aria-describedby="basic-addon1" max="24" min="0" step=".25" value="0"></asp:TextBox><br>
    <asp:CheckBox ID="cbFromHire" runat="server" /><label for="cbFromHire" class="form-control">Interval starts on Hire Date</label><br>
    <span class="input-group-text">Default Training Length (in hours)</span><asp:TextBox ID="tbDefaultHours" runat="server" CssClass="form-control" TextMode="Number" aria-describedby="basic-addon1" max="24" min=".25" step=".25" value="0"></asp:TextBox><br>
    <asp:CheckBox ID="cbCoreTraining" runat="server" /><label for="cbFromHire" class="form-control">Is this a core training.</label><br>
    <asp:HiddenField ID="hfTrainId" runat="server" />
    <asp:Button ID="bSaveTraining" runat="server" Text="Save Training" CssClass="btn btn-outline-success" UseSubmitBehavior="false" />
</asp:Content>

EditTraining.aspx.vb

Public Class EditTraining Inherits System.Web.UI.Page
    Dim _pageError As String
    Dim iTrainId As Decimal
    Dim oTrain As Training
    Dim _trainId As Integer = 0
    Public Property trainId() As Integer
        'Get and Set stuff
    End Property
    Public Property sError() As Integer
        'Get and Set stuff
    End Property
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not PreviousPage Is Nothing Then
            If PreviousPage.Title = "Training List" Then
                Dim oPage = DirectCast(PreviousPage, ListTraining)
                If IsNumeric(oPage.trainId) Then
                    'Pull Data from DB
                    'Fill in the TB's and CB's
                Else
                    Response.Redirect("~\Training\ListTraining.aspx")
                End If
            End If
        Else
            If hfTrainId.Value.Length > 0 or IsPostBack Then
                'Pull Data from DB
            Else
                Response.Redirect("~\Training\ListTraining.aspx")
            End If
        End If
    End Sub
    Private Sub bSaveTraining_Click(sender As Object, e As EventArgs) Handles bSaveTraining.Click
        Try
            'Save to DB
            _pageError = oTraining.Name & " was sucessfully saved on " & Now()
            Response.Redirect("~/Training/ViewTraining.aspx")
        Catch ex As Exception
            lError.Text = ex.Message
        End Try
    End Sub
End Class

Please tell me what I am doing wrong here. I have tried 2-3 dozen things and nothing is working.

Thanks ahead of time.

Upvotes: 0

Views: 175

Answers (0)

Related Questions