使用C#和tableadapter循环结果

时间:2015-08-20 18:20:41

标签: c# asp.net .net loops tableadapter

目前,当我按下我之前的按钮时,它只会将结果返回到我的文本框。我希望它循环结果。因此,第一次单击该按钮时,它将显示第一个记录,循环显示。

protected void btnPrevious_Click1(object sender, EventArgs e)
{
    DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter eobj = new DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter();
    DataTable dt = new DataTable();
    dt = eobj.GetTicketUpdates(txtSupportRef.Text);

    txtNextStep.Text = eobj.GetTicketData(txtSupportRef.Text).Rows[0]["NextStep"].ToString();
}

我想知道如何返回所有记录,然后根据每次点击循环浏览每一条记录?

这个想法是当你选择一个票据时,它会将最新的细节加载到表单中。但后来我的意图是有一个上一个和下一个按钮,这样我就可以循环显示票证的各种更新......

4 个答案:

答案 0 :(得分:1)

您需要跟踪点击次数。这样的事情就是你想要的。

protected void btnPrevious_Click1(object sender, EventArgs e)
{
    if (Session["ClickCount"] == null)
        Session["ClickCount"] = 0;

    int ClickCount = Convert.ToInt32(Session["ClickCount"]) + 1;
    Session["ClickCount"] = ClickCount;

    DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter eobj = new DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter();
    DataTable dt = new DataTable();
    eobj.GetTicketUpdates(txtSupportRef.Text);

    txtNextStep.Text = eobj.GetTicketData(txtSupportRef.Text).Rows[ClickCount - 1]["NextStep"].ToString();
}

答案 1 :(得分:1)

根据当前值更改数据库选择方法以获取下一个和previos票证更新。 期望txtNextStep.Tag包含对当前故障单更新代码的引用可能如下所示:

    protected void btnNext_Click1(object sender, EventArgs e)
    {
        DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter eobj = new DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter();
        DataTable dt = new DataTable();
        if (txtNextStep.Tag == null)//Pick first update.
            dt = eobj.GetTicketFirstUpdate(txtSupportRef.Text);
        else//Getting next ticket update for the ticket based on current displaying value
            dt = eobj.GetNextTicketUpdate(txtSupportRef.Text, (string)txtNextStep.Tag);

        if (dt.Rows.Count != 0)
        {
            txtNextStep.Text = dt.Rows[0]["NextStep"].ToString();
            txtNextStep.Tag = dt.Rows[0]["Id"].ToString();
        }
    }

对于性能考虑因素,读取多个故障单更新ID可能是有意义的,因此在按下按钮的情况下,将执行id读取而不搜索下一个或上一个故障单更新。

答案 2 :(得分:1)

就个人而言,我会有一个跟踪当前“点击”的计数。用户所在,如下:

protected void btnPrevious_Click1(object sender, EventArgs e)
{
    if(stepCount == 0)
        //Ensuring that we never get into minus numbers
        stepCount = 0;

    stepCount--;

    DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter eobj = new DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter();
    DataTable dt = new DataTable();
    dt = eobj.GetTicketUpdates(txtSupportRef.Text);

    txtNextStep.Text = eobj.GetTicketData(txtSupportRef.Text).Rows[0][stepCount].ToString();
}

protected void btnNext_Click1(object sender, EventArgs e)
{
    if(stepCount == 0)
        //Ensuring that we never get into minus numbers
        stepCount = 0;

    stepCount++;

    DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter eobj = new DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter();
    DataTable dt = new DataTable();
    dt = eobj.GetTicketUpdates(txtSupportRef.Text);

    txtNextStep.Text = eobj.GetTicketData(txtSupportRef.Text).Rows[0][stepCount].ToString();
}

您还可以在下一个按钮点击中添加一个检查,以检查用户当前是否在最后一条记录上,如果是,请将它们再次发送到记录列表的开头。

答案 3 :(得分:1)

您不想在视图代码中执行此操作,而是希望缓存查找结果,并且只显示“下一步”按钮,如果还有其他项目。

为此,为它们制作一个容器对象。

假设您的数据是这样的

class Ticket
{
     public string SomeField{get;set;}
}

制作像

这样的容器
class TicketList
{
    private List<Ticket> _tickets;
    private int _currentPosition;
    public TicketList<IEnumerable<Ticket> tickets>
    {
        _tickets = ticket;
        _currentPosition = 0;
    }

    public Ticket GetCurrent()
    {
        return _tickets[_currentPosition];
    }

    public bool CanMoveNext{
        get{
            return _tickets.Any() && _currentTicket != _tickets.Count - 1;
        }
    }


    public Ticket GetNext()
    {
        _currentPosition++;
        return _tickets[_currentPosition];
    }
}

现在,当您加载应用程序时,将票证加载到缓存

var tickets = new List<Ticket>();
var data = eobj.GetTicketData(txtSupportRef.Text);
foreach(var row in data.Rows)
{
    var ticket = new Ticket{ 
        //map over the ticket data here, or use AutoMapper
    };
    tickets.Add(ticket);
}

var ticketList = new TicketList(tickets);
Cache.Store(ticketList);  //cache can be an application var, or better solution

在页面加载上,你可以这样做      var ticketList = Cache.GetTicketList(); btnNext.Visible = ticketList.CanMoveNext; var ticket = ticketList.GetCurrent(); //populate your fields

现在,除了不在每次加载页面时重新加载所有票证,您的代码将来会更容易维护。