昨天我问了一个关于使用在表单文本框中显示单行的下一个/上一个按钮来分页数据集的问题。从那时起,我设法得到下一个按钮,但只进行一次。每次后续点击都不起作用。这是一些代码。
在用户选择搜索值并按下值后执行的代码:
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”]; INC ++;
if (inc != dataSetH.Tables[0].Rows.Count)
{
NavigateCopies();
}
else
{
Label1.Text = "No More Rows";
}
}
}
因此,当按下Next按钮时,它会选择下一行。每次后续点击都不起作用。页面状态栏符号旋转但文本框未填充下一条记录。我不确定这是否与表单重新加载有关,但我看到的每个例子都有一个表或网格加载,而我的情况要求我在用户输入后填充文本框。任何有关这方面的帮助将非常感激。
我已经编辑了代码。它现在正在工作,允许下一个按钮翻阅整个数据集。我将在按钮上创建一个catch以防止用户现在超过最大行。
答案 0 :(得分:3)
因为您在私有范围内定义inc变量,所以只能访问第一行 你应该公共范围与静态
public static int inc = 0 ;
并再次设置DataSet:
// put Session["ds"] need to put inc value as well your assign
答案 1 :(得分: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();
}
}
}
}