带有next和prev的页面使用数据库中的值 [英] Page with next and prev using the value in database

查看:102
本文介绍了带有next和prev的页面使用数据库中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个页面,它将成为数据库的下一个和前一个基础。

例如:

---------- ----------------------

员工

----------- ---------------------

ID |名字|姓氏|年龄

--------------------------------

111 Kenneth Gulaman 45

222 kyle puerto 30

444 george foo 25



员工表中有详细信息我需要的是雇员创建一个带有next和prev按钮的页面(不使用gridview),因为我喜欢创建一个像biodata形式的视图。怎么能在asp.net?

i want to create a page where their will be a next and previous base on the database.
example:
--------------------------------
EMPLOYEE
--------------------------------
ID|Firstname | Lastname | Age
--------------------------------
111 Kenneth Gulaman 45
222 kyle puerto 30
444 george foo 25

the employee table has a details of the empployee what i need is creating a page with a next and prev button(not using the gridview) because i like to create a view like in the biodata form. how can achive it in asp.net?

推荐答案

中给你一个完整的解决方案,因为你的桌子和数据出现了。



ASPX代码:



I am giving you a complte solution as your table and data appeared.

ASPX code:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <table>
        <tr>
            <td> ID</td>
            <td><asp:TextBox ReadOnly="true" ID="txtID" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td>First Name</td>
            <td><asp:TextBox ID="txtfname" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td>Last Name</td>
            <td><asp:TextBox ID="txtlname" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td> Age</td>
            <td><asp:TextBox ID="txtage" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td><asp:Button OnClick="btnPre_Click" Text="<< Previous"  ID="btnPre" runat="server" /></td>
            <td></td>
            <td></td>
            <td><asp:Button OnClick="btnNext_Click" Text="Next >>" ID="btnNext" runat="server" /></td>
        </tr>
    </table>
   

</asp:Content>







cs(代码落后)








cs(code behind)


public partial class WebForm1 : System.Web.UI.Page
    {
        int MaxID, MinID; 
        int current;
        List<int> lstIDs = new List<int>();
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConn"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            conn.Open();
            SqlCommand cmd_max = new SqlCommand("Select MAX(ID) from tbl_Employee", conn);
            SqlCommand cmd_min = new SqlCommand("Select MIN(ID) from tbl_Employee", conn);
            SqlCommand cmd_ids = new SqlCommand("Select ID from tbl_Employee", conn);

            MaxID = Int16.Parse(cmd_max.ExecuteScalar().ToString());
            MinID = Int16.Parse(cmd_min.ExecuteScalar().ToString());
            current = MinID;
            SqlDataReader reader = cmd_ids.ExecuteReader();
            while (reader.Read())
            {
                lstIDs.Add(Int16.Parse(reader[0].ToString()));

            }
            reader.Close();
            conn.Close();
            if (!IsPostBack)
            {
                using (conn)
                {
                    conn.Open();

                    SqlCommand cmd = new SqlCommand("Select * from tbl_Employee where ID=@ID", conn);
                    SqlParameter param = new SqlParameter("@ID", MinID);
                    cmd.Parameters.Add(param);

                    SqlDataReader reader1 = cmd.ExecuteReader();

                    while (reader1.Read())
                    {
                        txtID.Text = reader1[0].ToString();
                        txtfname.Text = reader1[1].ToString();
                        txtlname.Text = reader1[2].ToString();
                        txtage.Text = reader1[3].ToString();
                    }
                    reader1.Close();

                    conn.Close();
                }

            }
            else
            {
                current = (int)ViewState["current"] ;
            }

           
        }

        private void GetNextRecord()
        {
            using (conn)
            {
                conn.Open();

                SqlCommand cmd = new SqlCommand("Select * from tbl_Employee where ID=@ID", conn);
                SqlParameter param = new SqlParameter("@ID", txtID.Text);
                cmd.Parameters.Add(param);

                SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    txtID.Text = reader[0].ToString();
                    txtfname.Text = reader[1].ToString();
                    txtlname.Text = reader[2].ToString();
                    txtage.Text = reader[3].ToString();
                }
                reader.Close();
                conn.Close();
            }
        }

        protected void btnPre_Click(object sender, EventArgs e)
        {
            try
            {
                int current = Int16.Parse(txtID.Text);
                if (current != MinID)
                {
                    current=lstIDs.IndexOf(current)-1;
                    txtID.Text =lstIDs[current].ToString(); 
                    GetNextRecord();

                  
                }
            }
            catch
            { 
            }
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            try
            {
                int current = Int16.Parse(txtID.Text);
                if (current != MaxID)
                {
                    current = lstIDs.IndexOf(current) + 1;
                    txtID.Text = lstIDs[current].ToString();

                    GetNextRecord();

                    
                }
            }
            catch
            {
            }
        }

        void Page_PreRender(object sender, EventArgs e)
        {
            ViewState["current"] = current;
        }
    }


这篇关于带有next和prev的页面使用数据库中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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