Access Control From GridView RowCommand

Posted on: Tue Nov 11 15:44:17 -0800 2008. Updated on: Tue Nov 11 15:55:52 -0800 2008.
Category: DotNet

Let's say you have a GridView and you have the RowCommand defined to a function. We often want to get access to the controls in the Row so as to see what values the user selected. Here is an example:

protected void GridView1Items_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "ViewList")
    {
        // Here is the trick to get access to the Row.
        GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
	// Now we can access a dropdown for example and get the selected value.
        DropDownList ddLists = (DropDownList)row.FindControl("DropDownLists");
	int listId = Convert.ToInt32(ddLists.SelectedValue);
	PreviewList1.ListId = listId;
    }

This comes in handy almost any time you want to override the GridView's default behavior for editing / inserting data.

Comments:

Add a Comment:

simple_captcha.jpg
(human authentication)


Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.