Monday, January 21, 2013

How to pass variables from one page to another with Query String

Suppose in one web page, we have a TextBox and a 'Search' Button. On this page, user can enter any name in the TextBox to search and hits the Search button. On hitting search button, another web page opens that displayed the searched result in the grid.

Let's see, how we could do this using query string:

In the Search Button (First webpage) event handler put the code like:


        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Staff.aspx?Name=" + txtBx_Request.Text);
        }


And on the second form page load, get the query string and send the query string to retrieve the data from database. After that we bind the data into a data grid.

        protected void Page_Load(object sender, EventArgs e)
        {
            string reqName = Request.QueryString["Name"];
          
           if (reqName != null && reqName != "")
           {
               Label1.Text = "You queried for the person having name: " + reqName;
               //Getting the data from database
               DataSet resultDataSet = GetDataFromDatabase(storedProcName, reqName);
 
               if (resultDataSet.Tables[0].Rows.Count > 0)
               {
                   GridView1.DataSource = resultDataSet.Tables[0];
                   GridView1.AutoGenerateColumns = true;
                   GridView1.DataBind();
               }
               else
               {
                   Label1.Text += "<br/><br/>Sorry, No data found!";
               }
           }
        }

No comments:

Post a Comment