开发者

ASP.net Gridview.. something weird

开发者 https://www.devze.com 2023-03-25 05:25 出处:网络
I have a gridview and use a session to pass the variables from the page to an edit page. This works perfectly, until you search for a number. When you search the correct record displays in gridview, b

I have a gridview and use a session to pass the variables from the page to an edit page. This works perfectly, until you search for a number. When you search the correct record displays in gridview, but when you click edit, it passes the wrong record.

Private Sub gridview1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
    Session("wr_id") = GridView1.Rows(e.NewEditIndex).Cells(2).Text & "~\/~" & _
                GridView1.Rows(e.NewEditIndex).Cells(3).Text & "~\/~" & _
                GridView1.Rows(e.NewEditIndex).Cells(4).Text & "~\/~" & _
                GridView1.Rows(e.NewEditIndex).Cells(5).Text & "~\/~" & _
                GridView1.Rows(e.N开发者_开发问答ewEditIndex).Cells(6).Text & "~\/~" & _
                GridView1.Rows(e.NewEditIndex).Cells(7).Text & "~\/~" & _
                GridView1.Rows(e.NewEditIndex).Cells(8).Text & "~\/~" & _
                GridView1.Rows(e.NewEditIndex).Cells(9).Text & "~\/~" & _
                GridView1.Rows(e.NewEditIndex).Cells(10).Text & "~\/~" 
                Response.Redirect("WorkEdit.aspx")
End Sub

GRIDVIEW Page

Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
    Dim strPost As Boolean = HiddenSearch.Value
    If strPost = True Then
        Dim strNumber As String
        Dim Dropdown As String
        strNumber = Search_text.Text
        Dropdown = Search_Field.SelectedValue
        If Dropdown = "WO#" Then
            Convert.ToInt32(strNumber)
        End If
        Try
            SqlDataSource1.SelectCommand = "SELECT * FROM [WorkOrderLog] WHERE " + Dropdown + " = '" + strNumber + "' ORDER BY [WO#] DESC"
            SqlDataSource1.Select(DataSourceSelectArguments.Empty)
            SqlDataSource1.DataBind()
            GridView1.DataBind()
        Catch
            'output messagebox for debug
            Dim strPrompt As String
            strPrompt = "Something bad happened, check search text and try again."
            Dim strScript As String = "<script language=JavaScript>"
            strScript += "alert(' " & strPrompt & "');"
            strScript += "</script>"
            Search_text.Focus()
        End Try
    Else
        Search_text.Focus()
    End If


End Sub

WORDEDIT Page

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    'change submit button on mouseover events
    Submit_Button.Attributes.Add("onmouseover", "this.src='../images/Submitdown.png'")
    Submit_Button.Attributes.Add("onmouseout", "this.src='../images/Submitup.png'")
    'change clear button on mouseover events
    Clear.Attributes.Add("onmouseover", "this.src='../images/Cancel(down).png'")
    Clear.Attributes.Add("onmouseout", "this.src='../images/Cancel(up).png'")
    Call Keypress_ALegal()
    Call Keypress_ASite()
    errorWOName.Text = HiddenWOName.Value
    errorLegalDesc.Text = HiddenLegalDesc.Value
    errorLocationNumber.Text = HiddenLocationNumber.Value
    errorDesc.Text = HiddenDesc.Value
    errorSiteNumber.Text = HiddenSiteNumber.Value

    If Not Page.IsPostBack Then

        'Get session info
        lblID.Text = "Session Variable Was Lost"
        If (Session("wr_id") <> "") Then
            Dim strSession As String = Session("wr_id")
            Dim sessionArray As Array
            'split session into array at ~\/~ 
            sessionArray = Split(Session("wr_id"), "~\/~")
            'assign textbox/dropdowns values passed from split variables
            lblID.Text = sessionArray(0)
            'WO Name
            If sessionArray(1) = "&nbsp;" Then
                WOName.Text = ""
            Else
                WOName.Text = sessionArray(1)
            End If
            Location.Text = sessionArray(2)
            'LegalDesc
            If sessionArray(3) = "&nbsp;" Then
                LegalDesc.Text = ""
            Else
                LegalDesc.Text = sessionArray(3)
            End If
            'Trans ADDED
            If sessionArray(4) = "&nbsp;" Then
                TransADDED.Text = ""
            Else
                TransADDED.Text = sessionArray(4)
            End If
            'Trans Retired
            If sessionArray(5) = "&nbsp;" Then
                TransRETIRED.Text = ""
            Else
                TransRETIRED.Text = sessionArray(5)
            End If
            If sessionArray(6) = "&nbsp;" Then
                Description.Text = ""
            Else
                Description.Text = sessionArray(6)
            End If
            If sessionArray(7) = "1/1/1900 12:00:00 AM" Or sessionArray(7) = "&nbsp;" Then
                Started.Text = ""
            Else
                Started.Text = (CType((sessionArray(7)), DateTime).ToString("MM/dd/yyyy HH:mm tt"))
            End If
            If sessionArray(8) = "1/1/1900 12:00:00 AM" Or sessionArray(8) = "&nbsp;" Then
                Completed.Text = ""
            ElseIf sessionArray(8) = "&nbsp;" Then
                Completed.Text = ""
            Else
                Completed.Text = (CType((sessionArray(8)), DateTime).ToString("MM/dd/yyyy HH:mm tt"))
            End If
            StakedBy.SelectedValue = sessionArray(9)

I realize not all of the code is pasted in here, because it would take too much space. Any Ideas? I'm guessing it has something to do with e.NewEditIndex


Your code looks fine. Is Session("wr_id") the actual code, or is "wr_id" mock-up code?

The reason I ask is because session values can either be accessed by a string or an index.

  • Session("id") will access the session variable with the "id" key
  • Session(8) will access the 9th element in the session

If you have code using Session(selectedID) and selectedID is a populated object variable, you will change the way you are accessing the session variables depending on whether selectedID is a number or a string

Update:

Look to see if you are binding anywhere else in your code (e.g. your Page_Load event). There is a good chance that you are re-binding the grid with new data before the control's edit event can fire


The solution to the problem was wrapping the pageload in a if, ispostback statement. Thank you for your help rkw.

   Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    If IsPostBack Then
        If Search_text.Text = "" Then
            SqlDataSource1.SelectCommand = "SELECT * FROM [WorkOrderLog] ORDER BY [WO#] DESC"
            SqlDataSource1.Select(DataSourceSelectArguments.Empty)
            SqlDataSource1.DataBind()
            GridView1.DataBind()
        Else
        End If
    End If
        If Not IsPostBack Then
            SqlDataSource1.SelectCommand = "SELECT * FROM [WorkOrderLog] ORDER BY [WO#] DESC"
            SqlDataSource1.Select(DataSourceSelectArguments.Empty)
            SqlDataSource1.DataBind()
            GridView1.DataBind()
        End If
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消