将选定的ASP.net GridView控件行传递给另一个页面 [英] Pass selected row of ASP.net GridView control to another page

查看:61
本文介绍了将选定的ASP.net GridView控件行传递给另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的第一页上,用户从两个文本框中选择到达日期和出发日期,然后可用房间显示在gridview控件中。用户可以选择他们想要的房间选项,然后选择预订按钮移动到确认页面,他们的选项将显示在文本框中。



我有将所选行数据发送到下一页的问题。

这是我的代码:

MakeReservation页面 -



On my first page, users select an arrival date and a departure date from two text boxes and then the available rooms are displayed in a gridview control. A user can select the room option they want and then select the reservation button to move to the confirmation page where their option will be displayed in the textboxes.

I am having problems with sending the selected rows data to the next page.
Here is the code I have:
MakeReservation Page -

public partial class makeReservation : System.Web.UI.Page
    {
        OleDbConnection dbConnection;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["LoggedIn"] == null || //if cookie does not exist
              (Request.Cookies["LoggedIn"] != null && //if cookie is valid
              Request.Cookies["LoggedIn"].Value != "true")) //but user is not logged in
            {
                //goes back to start page of website
                Response.Redirect("Home.aspx");
            }

            //checks if the cookie exists
            //if it does exist, it displays the name of the user currently logged in
            if (Request.Cookies["LoggedIn"] != null)
            {
                lblUsername.Text = Session["Username"] + " is logged in.";
            }
        }

        protected void searchBtn_Click(object sender, EventArgs e)
        {
            //when the search button is selected these controls become visible
            selectRoomLbl.Visible = true; 
            reserveBtn.Visible = true;
            GridView1.Visible = true;
            
            try
            {
                ListView listItem = new ListView();

                //creates a connection to the database
                dbConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" +
                    Request.PhysicalApplicationPath + "App_Data\\Mercure.accdb");

                dbConnection.Open();

                //query retrieves all available rooms for reservation
                string query = "SELECT * FROM RoomsDetails WHERE roomID NOT IN(";
                query += "SELECT roomID FROM ReservationDetails WHERE NOT(";
                query += "dateArrival < " + arrivalTxt.Text + "OR dateDeparture > " + departureTxt.Text + "))";
                query += "ORDER BY RoomsDetails.roomID";

                OleDbCommand cmd = dbConnection.CreateCommand();
                cmd.CommandText = query;
                OleDbDataReader dbReader = cmd.ExecuteReader();

                if (dbReader != null && dbReader.HasRows)
                {
                    while (dbReader.Read())
                    {
                        GridView1.DataSource = dbReader;
                        GridView1.DataBind();
                   
                    }
                }
                 
            }
            finally
            {
                dbConnection.Close();
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowIndex == GridView1.SelectedIndex)
                {
                    row.BackColor = ColorTranslator.FromHtml("#490A0A");
                }
            }
        }

        protected void reserveBtn_Click(object sender, EventArgs e)
        {   
            /*when the reserve button is clicked, it first checks if the gridview has a 
             * selected row or not. If it does it does a Server.Transfer to the 
             * ConfirmReservation page. If the user has not selected a row, it will ask 
             * them to select one 
             * using a JavaScript Alert
             */
            if (GridView1.SelectedRow != null)
            {
                Server.Transfer("~/ConfirmReservation.aspx");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert",
                    "alert('Please select a row.')", true);
            }
        }
    }





确认预订页面 -



Confirm reservation page -

public partial class ConfirmReservation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Page.PreviousPage != null)
            {
                GridView GridView1 = (GridView)this.Page.PreviousPage.FindControl("GridView1");
                GridViewRow selectedRow = GridView1.SelectedRow;
                roomIDTxt.Text = selectedRow.Cells[0].Text;
                roomTypeTxt.Text = selectedRow.Cells[1].Text;
                capacityTxt.Text = selectedRow.Cells[2].Text;
                singleBedsTxt.Text = selectedRow.Cells[3].Text;
                doubleBedsTxt.Text = selectedRow.Cells[4].Text;
                priceTxt.Text = selectedRow.Cells[5].Text;
            }
        }
    }





我在确认预订页面上收到以下错误:

NullReferenceException未被用户代码处理



这行代码:

GridViewRow selectedRow = GridView1。 SelectedRow;



I get the following error on the confirm reservation page:
"NullReferenceException was unhandled by user code"

on this line of code:
GridViewRow selectedRow = GridView1.SelectedRow;

推荐答案

将该行的ID发送到下一页。页面是无状态的。因此,他们无法存储所选行或其他内容的详细信息。您只能在同一页面上找到它,而不能在其他页面中找到。
Send the id of that row to the next page. The pages are stateless. So, they can't store the details like selected row or something. You can find that on the same page only, not in other pages.


这篇关于将选定的ASP.net GridView控件行传递给另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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