Using GridView Selected Row in ASP.Net AJAX

by top54u.com 09 May, 2008
Spotlight.....

In the previous article we learnt how to generate GridView Select command using AJAX controls in ASP.Net web page with C# code sample. Now we will learn the use of Selected Row of GridView Control using AJAX methodology.

You can use SelectedDataKey of GridView control to define the unique id/ relational key of the relational database so that you could retrieve the related data of selected row.

SelectedDataKey: It returns the DataKey object associated with the selected row of the GridView Control. It is also very useful for handling multiple row selection in GridView.

 

C# Sample Code for GridView Selected Row using AJAX

In this C# sample for GridView Select we will continue the previous example of GridView Select command by adding another GridView control for getting related products corresponding to the selected categories in the first GridView onSelectedRowChanging. To code for this functionality to fill the second gridview control on row select command event of first gridview control call the bind GridView function for second gridview by passing the Value of SelectedDataKey to the SQL Query.

 

C# code for using GridView Select by passing SelectedDataKey:

    protected void grdCategories_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        grdCategories.SelectedIndex = e.NewSelectedIndex;
        bindGridViewCategories();
        bindGridViewProducts(Convert.ToInt32(grdCategories.SelectedDataKey["categoryid"].ToString()));
    }

protected void bindGridViewProducts(int categoryID)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ToString();
        SqlConnection sqlCon = new SqlConnection(connectionString);

        try
        {
            SqlCommand sqlCmd = new SqlCommand("select * from products where "mailto:categoryid=@categoryid">categoryid=@categoryid", sqlCon);
            sqlCmd.CommandType = CommandType.Text;
            sqlCmd.Parameters.Add("@categoryid", SqlDbType.Int).Value = categoryID;
            SqlDataAdapter sqlAdp = new SqlDataAdapter(sqlCmd);
            DataSet myDataSet = new DataSet();
            sqlAdp.Fill(myDataSet);

            // Delay the current Thread to display
            // the UpdateProgress status
            // Not required in real projects
            System.Threading.Thread.Sleep(4000);

            grdProducts.DataSource = myDataSet;
            grdProducts.DataBind();
        }
        catch (Exception ex)
        {
            lblErrorMsg.Text = ex.Message;
        }
    }

Download the free sample code for Using GridView Selected Row in ASP.Net AJAX:

 

ajax gridview selected.zip (3.34 kb)

Spotlight.....

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , , , , , , ,

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

7/24/2008 5:25:57 PM

OUR SPONSORS[+ advertise here]
top54u ezines.....
related videos.....
recent posts.....