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 = (DataSet)Session["ds"]; inc = (int)Session["increment"]; 公司++;

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

因此,当按下 Next 按钮时,它会选择下一行。每次后续点击什么都不做。页面状态栏符号旋转,但文本框未填充下一条记录。我不确定这是否与表单正在重新加载的事实有关,但是我看到的每个示例都在加载时填充了表格或网格,而我的情况要求我在用户输入后填充文本框。对此的任何帮助将不胜感激。

我已经编辑了代码。它现在正在工作,允许下一个按钮翻阅整个数据集。我将在按钮上创建一个按钮,以防止用户现在超过最大行数。

4

2 回答 2

3

因为您在私有范围内定义了 inc 变量,所以只有您访问第一行,您才应该使用静态的公共范围可变

          public static  int inc = 0 ;

并再次设置 DataSet :

            // put Session["ds"] need to put inc value as well your assign
于 2013-07-16T08:46:10.307 回答
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();
            }
        }
    }
}
于 2013-07-16T08:45:20.983 回答