2013-07-16 88 views
2

昨天我問了一個關於使用下一個/上一個按鈕在窗體文本框中顯示單個行的數據集進行分頁的問題。從那時起,我設法讓下一個按鈕進入下一行,但只有一次。隨後的每一次點擊均不起作用這裏有一些代碼。數據集按鈕的下一行只執行一次函數

該用戶之後,執行該代碼選擇搜索值並按下一個值:

public static int inc = 0; 

    protected void ExecuteSelectCopies(string sName) 
{ 
    SqlConnection connection = new SqlConnection(GetConnectionString()); 
    try 
    { 
     string sql = 
      "SELECT tblCopies.CopyID, tblSoftware.SoftwareName, tblCopies.AssetName, tblCopies.Location," 
      + " tblCopies.LicenceKey, tblCopies.OEM, tblCopies.State, tblCopies.InstallationDate" 
      + " FROM tblCopies" 
      + " INNER JOIN tblSoftware ON tblCopies.SoftwareID = tblSoftware.SoftwareID" 
      + " WHERE tblSoftware.SoftwareName = @SoftwareName" 
      + " ORDER BY tblCopies.CopyID"; 
     SqlDataAdapter adapterH = new SqlDataAdapter(); 
     SqlCommand select = new SqlCommand(sql, connectionHWM); 
     adapterH.SelectCommand = select; 
     select.Parameters.Add(new SqlParameter("@SoftwareName", sName)); 
     //open the connection 
     connection.Open(); 
     dataSetH = new DataSet(); 
     adapterH.Fill(dataSetH, "Copies"); 
    } 
    catch (Exception ex) 
    { 
     string msg = "fetch Error:"; 
     msg += ex.Message; 
     throw new Exception(msg); 
    } 
    finally 
    { 
     connection.Close(); 
    } 

    NavigateCopies(); 
} 

用於填充的文本框的第一時間代碼:

private void NavigateCopies() 
{ 
    DataRow dRow = dataSetH.Tables[0].Rows[inc]; 
    TextBoxCopyID.Text = dRow.ItemArray.GetValue(0).ToString(); 
    TextBoxSoftwareName.Text = dRow.ItemArray.GetValue(1).ToString(); 
    DropDownListAssetName.SelectedItem.Text = dRow.ItemArray.GetValue(2).ToString(); 
    DropDownListLocation.SelectedItem.Text = dRow.ItemArray.GetValue(3).ToString(); 
    TextBoxLicence.Text = dRow.ItemArray.GetValue(4).ToString(); 
    DropDownListType.SelectedItem.Text = dRow.ItemArray.GetValue(5).ToString(); 
    DropDownListState.SelectedItem.Text = dRow.ItemArray.GetValue(6).ToString(); 
    TextBoxInstall.Text = dRow.ItemArray.GetValue(7).ToString(); 
    Session["ds"] = dataSetH; 
    Session["increment"] = inc; 
} 

下一行按鈕代碼: protected void ButtonNext_Click(object sender,EventArgs e) if(Session [「ds」]!= null) { dataSetH = (數據集)的會話[ 「DS」]; inc =(int)Session [「increment」]; inC++;

 if (inc != dataSetH.Tables[0].Rows.Count) 
     { 
      NavigateCopies(); 
     } 
     else 
     { 
      Label1.Text = "No More Rows"; 
     } 
    } 
} 

所以當按下下一步按鈕時,它會選擇下一行。隨後的每次點擊都不起作用頁面狀態欄符號旋轉,但文本框未填入下一條記錄。我不確定這是否需要重新加載表單,但我看到的每個示例都有一個表格或網格填充,而我的情況要求我在用戶輸入後填充文本框。任何幫助,將不勝感激。

我已經編輯過代碼。它現在正在工作,允許下一個按鈕翻閱整個數據集。我將在按鈕上創建一個catch,以防止用戶現在超過最大行數。

+0

我也很想知道如何在沒有按鈕導航功能的情況下工作的需要。 – Tinydan

回答

2

如你一樣把Session["ds"]必要把inc值以及

protected void ButtonNext_Click(object sender, EventArgs e) 
{ 
    if (Session["ds"] != null) 
    { 
     int inc; 
     if(Session["inc"]==null) 
     { 
      Session["inc"] =0; 
     } 
     if(int.TryParse(Session["inc"], out inc) 
     { 
      inc++; 
      Session["inc"] = inc; 
      DataSet dataSetH = (DataSet)Session["ds"]; 

      if (inc <= dataSetH.Tables[0].Rows.Count) 
      { 
       DataRow dRow = dataSetH.Tables[0].Rows[inc]; 
       TextBoxCopyID.Text = dRow.ItemArray.GetValue(0).ToString(); 
       TextBoxSoftwareName.Text = dRow.ItemArray.GetValue(1).ToString(); 
       DropDownListAssetName.SelectedItem.Text = dRow.ItemArray.GetValue(2).ToString(); 
       DropDownListLocation.SelectedItem.Text = dRow.ItemArray.GetValue(3).ToString(); 
       TextBoxLicence.Text = dRow.ItemArray.GetValue(4).ToString(); 
       DropDownListType.SelectedItem.Text = dRow.ItemArray.GetValue(5).ToString(); 
       DropDownListState.SelectedItem.Text = dRow.ItemArray.GetValue(6).ToString(); 
       TextBoxInstall.Text = dRow.ItemArray.GetValue(7).ToString(); 
      } 
     } 
    } 
} 
+0

嗯,我明白你的意思是採取公司。給我幾分鐘嘗試一下 – Tinydan

+0

好吧,我已經明白了!你對我的公司價值是正確的。爲了簡單起見,我重複使用了相同的導航方法。我現在將用解決方案更新我的問題。 – Tinydan

+0

非常感謝我的朋友。我已經在我的問題中展示了我的解決方案,我似乎讓自己很難。你的例子向我展示了我應該如何接近它。非常感謝你 – Tinydan

3

因爲你定義在私人範圍INC變量,只有你訪問第一行 你應該公開範圍vaiable靜態

  public static int inc = 0 ; 

並且還設置數據集:

  // put Session["ds"] need to put inc value as well your assign 
+0

你是對的我的朋友感謝你的sugegstion – Tinydan

+0

沒有足夠的代表抱歉 – Tinydan