2013-07-16 75 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