Labels

Monday, September 3, 2007

DataKeys : AutoGenerateSelectButtion, CommandField with CommandName = 'Select'

DataKeys : AutoGenerateSelectButtion, CommandField with CommandName = ‘Select’ & GridView1_SelectedIndexChanged

e.g.: 11.3

GridView1_SelectedIndexChanged -

You can determine which row is selected in a GridView control by using any of the following properties:

SelectedDataKey

Returns the DataKey object associated with the selected row (useful when there are multiple data keys).

SelectedIndex

Returns the (zero-based) index of the selected row.

SelectedValue

Returns the data key associated with the selected row.

SelectedRow

Returns the actual row (GridViewRow object) associated with the selected row.

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

Response.Write("<b>SelectedDataKey.Value: </b>" & Server.HtmlEncode(GridView1.SelectedDataKey.Value) & "<br />")


Response.Write("<b>DataKey Field 1: </b>" & Server.HtmlEncode(GridView1.SelectedDataKey.Values("au_id")) & "<br />")


Response.Write("<b>DataKey Field 2: </b>" & Server.HtmlEncode(GridView1.SelectedDataKey.Values("title_id")) & "<br />")

End Sub

In General:

A GridView stores data keys (DataKey objects in a collection called the DataKeys collection. This collection is exposed by the GridView control's DataKeys property.

Syntax:

· For Single-Column Datakeys:

DataKey objDataKey = Gridview1.DataKeys[<Row Number>]
Return objDataKey.Value

· For Multi-Column Datakeys:

DataKey objDataKey = Gridview1.DataKeys[<Row Number>]
Return objDataKey.Values[“<Column Name>”]

In most cases, you use the SelectedValue property to determine the value associated with a particular row. The SelectedValue property returns the data key associated with a row. See Below.

Example 1:

    <asp:GridView
        id="grdMovieCategories"
        DataKeyNames="Id"
        DataSourceID="srcMovieCategories"
        AutoGenerateSelectButton="true"
        SelectedRowStyle-CssClass="selectedRow"
        CssClass="gridView"
        Runat="server" />
 
     <asp:SqlDataSource
        id="srcMovieCategories"
        ConnectionString="<%$ ConnectionStrings:Movies %>"
        SelectCommand="SELECT Id, Name FROM MovieCategories"
        Runat="server" />
   
     <asp:GridView
        id="grdMovies"
        DataSourceID="srcMovies"
        CssClass="gridView"
        Runat="server" />
 
    <asp:SqlDataSource
        id="srcMovies"
        ConnectionString="<%$ ConnectionStrings:Movies %>"
        SelectCommand="SELECT Title,Director FROM Movies
            WHERE CategoryId=@CategoryId"
        Runat="server">
        <SelectParameters>
        <asp:ControlParameter
            Name="CategoryId"
            ControlID="grdMovieCategories"
            PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>
 
 

Example 2:

 

    <asp:GridView
        id="grdMovieCategories"
        DataKeyNames=" FistName, LastName "
        DataSourceID="srcMovieCategories"
        AutoGenerateSelectButton="true"
        SelectedRowStyle-CssClass="selectedRow"
        CssClass="gridView"
        Runat="server" />
 
     <asp:SqlDataSource
        id="srcMovieCategories"
        ConnectionString="<%$ ConnectionStrings:Movies %>"
        SelectCommand="SELECT FistName, LastName FROM Employees"
        Runat="server" />
   
     <asp:GridView
        id="grdMovies"
        DataSourceID="srcMovies"
        CssClass="gridView"
        Runat="server" />
 
    <asp:SqlDataSource
        id="srcMovies"
        ConnectionString="<%$ ConnectionStrings:Movies %>"
        SelectCommand="SELECT * FROM Employees
            WHERE FirstName=@FirstName AND LastName=@LastName "
        Runat="server">
        <SelectParameters>
        <asp:ControlParameter
            Name="FirstName"
            ControlID="grdMovieCategories"
            PropertyName='SelectedDataKey("FirstName")’ />
               <asp:ControlParameter
            Name="LastName"
            ControlID="grdMovieCategories"
            PropertyName='SelectedDataKey("LastName")’ />
        </SelectParameters>
    </asp:SqlDataSource>
 

Thanks & Regards,

Arun Manglick || Tech Lead

No comments:

Post a Comment