2011-08-03 117 views
0

我有一個gridview並使用會話將變量從頁面傳遞到編輯頁面。這完美地工作,直到你搜索一個數字。當您在gridview中搜索正確的記錄顯示時,但是當您單擊編輯時,它會傳遞錯誤的記錄。ASP.net Gridview ..東西很奇怪

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.NewEditIndex).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頁

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頁

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) 

我知道不是所有的代碼粘貼在這裏,因爲它會佔用太多空間。有任何想法嗎?我猜它與e.NewEditIndex有關。

回答

0

問題的解決方案是將頁面加載封裝在if,ispostback語句中。感謝您的幫助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

你的代碼看起來不錯。會話(「wr_id」)是實際的代碼,還是「wr_id」模擬代碼?

我問的原因是因爲會話值可以通過字符串或索引訪問。

  • 會議(「ID」)將與「ID」鍵
  • 會議(8)將在會話訪問第九元素訪問會話變量

如果您在使用代碼Session(selectedID)selectedID是一個人口object變量,你會改變你正在訪問取決於是否selectedID是一個數字或字符串

U中的會話變量的方法更新

查看您是否綁定了代碼中的其他任何地方(例如,你的Page_Load事件)。在控件的編輯事件觸發之前,您很可能會重新綁定網格和新數據

+0

會話(「wr_id」)是實際的代碼。會話中包含的是當前記錄與「〜\ /〜」連接的字符串,用戶單擊「編輯」,然後轉發到編輯頁面,在這裏我將字符串拆分爲「〜\ /〜」。問題是當用戶搜索時,它發送的索引號是不正確的。 – atrueresistance

+0

您可能需要發佈更多的代碼。 GridView_DataBiding(Bound)事件發生了什麼?或者GridView定義的其他事件? – rkw

+0

查看更新以瞭解更多關於查找內容的詳細信息 – rkw