如何制作上一行和下一行按钮 [英] how to make previous and next rows button

查看:127
本文介绍了如何制作上一行和下一行按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用c#将SQL 2008DB与asp.net 2010 Web应用程序连接起来了,我无法使下一个和上一个按钮工作,

我希望在label1中显示所选的行号,

只有最后一个按钮正在工作 - 请帮助我

我在defualt.aspx.cs写这些代码

In i have connected SQL 2008DB with asp.net 2010 web application using c# and I can not make next and previous buttons work,
and i want to show selected row number in label1 ,
only last and first buttons are working - Help me please
and i write these codes in defualt.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        int i = 0;
        DataTable dt = new DataTable();
        DataSet ds = new DataSet();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(@"Data Source=HARB\SQLEXPRESS;Initial Catalog=daily_log_golf;Integrated Security=True");
            conn.Open();
            SqlDataAdapter da =new SqlDataAdapter  ("select * from school", conn);
            da.Fill(ds, "school");
            dt = ds.Tables["school"];         
        }
        public void Display()
        {
            DataRow dr = dt.Rows[i];
            TextBox1.Text = dr["id"].ToString();
            TextBox2.Text = dr["name"].ToString();
            TextBox3.Text = dr["email"].ToString();
            TextBox4.Text = dr["town"].ToString();
        }
        protected void firstbtn_Click(object sender, EventArgs e)
        {
            {
                i = 0;
                Display();
            }
        }

        protected void nextbtn_Click(object sender, EventArgs e)
        {
            if (i < ds.Tables[0].Rows.Count - 1)
            {
                i++;
                Display();
            }
            else
            {
            }
        }

        protected void previousbtn_Click(object sender, EventArgs e)
        {
            i++;
            if (i >= 0)
            {
               Display();
            }
        }

        protected void lastbtn_Click(object sender , EventArgs e)
        {
            i = dt.Rows.Count - 1;
            Display();
        }

        protected void showrowbtn_Click(object sender, EventArgs e)
        {
            //to show selected row number in label 1
            DataRow row = dt.NewRow();
            dt.Rows.Add(row);
            // get the index now
            Label1.Text = (dt.Rows.IndexOf(row)).ToString();
        }
    }
}

推荐答案

您的代码适用于First,Last和Next按钮。



将上一个按钮中的代码更改为
Your code is working for First, Last and Next buttons.

Change the code in the Previous button to be
private void previousbtn_Click(object sender, EventArgs e)
{
    if (i > 0)
    {
        i--;
        Display();
    }
}



I.e.递减索引不会继续添加它,并在更改之前检查是否要更改它! (否则它会越来越远离范围)



showrowbtn_Click 事件中的代码是没有做错任何事,虽然我无法真正看出它的重点。我怀疑你想在文本框中输入一行并显示该行?



如果是这样,那么只需验证输入的数字是否介于0和0之间rows - 1,设置i和Display()


I.e. decrement the index don't keep adding to it, and check that you want to change it before changing it! (otherwise it just keeps getting further and further out of the range)

The code in your showrowbtn_Click event is doing nothing wrong, although I can't really see the point of it. I suspect that you wanted to enter a row in a textbox perhaps and show that row?

If so then just validate that the number entered is between 0 and the number of rows - 1, set i and Display()


这篇关于如何制作上一行和下一行按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆