我昨天问了一个关于使用在表单文本框中显示单行的下一个/上一个按钮来分页数据集的问题。从那时起,我设法让下一个按钮转到下一行,但只有一次。随后的每次点击都不会执行任何操作。这是一些代码。
用户选择搜索值并按下某个值后执行的代码:
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(对象发送者,EventArgs e) { if ( session ["ds"] != null) { dataSetH = (DataSet)Session["ds"]; inc = (int)Session["增量"]; 公司++;
if (inc != dataSetH.Tables[0].Rows.Count)
{
NavigateCopies();
}
else
{
Label1.Text = "No More Rows";
}
}
}
因此,当按下“下一步”按钮时,它会选择下一行。随后的每次点击都不会执行任何操作。页面状态栏符号旋转,但文本框未填充下一条记录。我不确定这是否与表单正在重新加载这一事实有关,但我见过的每个示例都有一个在加载时填充的表格或网格,而我的情况要求我在用户输入后填充文本框。任何对此的帮助将不胜感激。
我已经编辑了代码。现在它可以工作了,允许下一个按钮翻阅整个数据集。我将在按钮上创建一个捕获器,以防止用户现在超过最大行数。
最佳答案
因为您在私有(private)范围内定义了 inc 变量,所以只有您访问第一行 你应该使用静态的 public 作用域
public static int inc = 0 ;
并再次设置数据集:
// put Session["ds"] need to put inc value as well your assign
关于c# - 数据集按钮的下一行仅执行一次功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17671706/