Link to home
Start Free TrialLog in
Avatar of DanielBorson
DanielBorsonFlag for United States of America

asked on

How to programmatically select GridView row so that "OnSelectedIndexChanged" event fires

I have a GridView that represents a list of Users. There's also a DetailsView to display a User's details. Both controls use the same ObjectDataSource control for data.

 In normal mode, when a user selects "Details", it selects the row, fires off the "OnSelectedIndexChanging" and "OnSelectedIndexChanged" events, and the details for a user are displayed in the DetailsView.

I want to be able to come into the page with a User's ID stored in Session, and to select that User's row in the GridView and display the DetailsView record for that user. This must all happen on Page Load.

I've searched online, but can find no solutions. I've attached my ASP.NET Code.

Thanks!


<asp:ObjectDataSource ID="odsUsers" runat="server" 
        DataObjectTypeName="RandomLengths.MyPrintCodeBlocks.BusinessObjects.CUser" 
        SelectMethod="FetchUserSearch" UpdateMethod="UpdateUser" SortParameterName="argSortExpression" 
        TypeName="RandomLengths.MyPrintCodeBlocks.BusinessObjects.CUserCollection">
        <SelectParameters>
            <asp:ControlParameter Name="argEmail" ControlID="SearchEmail" Type="String" PropertyName="Text" />
            <asp:ControlParameter Name="argEmailContains" ControlID="SearchEmailContains" Type="Boolean" PropertyName="Checked" />
            <asp:ControlParameter Name="argFirstName" ControlID="SearchFirstName" Type="String" PropertyName="Text" />
            <asp:ControlParameter Name="argFirstNameContains" ControlID="SearchFirstNameContains" Type="Boolean" 
 
PropertyName="Checked" />
            <asp:ControlParameter Name="argLastName" ControlID="SearchLastName" Type="String" PropertyName="Text" />
            <asp:ControlParameter Name="argLastNameContains" ControlID="SearchLastNameContains" Type="Boolean" PropertyName="Checked" 
 
/>
            <asp:ControlParameter Name="argCompanyName" ControlID="SearchCompanyName" Type="String" PropertyName="Text" />
            <asp:ControlParameter Name="argCompanyNameContains" ControlID="SearchCompanyNameContains" Type="Boolean" 
 
PropertyName="Checked" />
            <asp:ControlParameter Name="argSTProv" ControlID="SearchSTProv" Type="String" PropertyName="SelectedValue" />
            <asp:ControlParameter Name="argZip" ControlID="SearchZip" Type="String" PropertyName="Text" />
            <asp:ControlParameter Name="argCountry" ControlID="SearchCountry" Type="String" PropertyName="SelectedValue" />
            <asp:ControlParameter Name="argPhoneNumber" ControlID="SearchPhoneNumber" Type="String" PropertyName="Text" />
            <asp:ControlParameter Name="argPhoneNumberContains" ControlID="SearchPhoneNumberContains" Type="Boolean" 
 
PropertyName="Checked" />
            <asp:ControlParameter Name="argUserStatusPKSEQ" ControlID="SearchUserStatus" Type="Int32" PropertyName="SelectedValue" />
        </SelectParameters>
        </asp:ObjectDataSource>
        
    <asp:GridView ID="gvUsers" runat="server" AllowPaging="true" PageSize="50" AllowSorting="true" AutoGenerateColumns="false" 
        DataSourceID="odsUsers" DataKeyNames="PKSEQ" EnableViewState="false" Width="850" OnRowCommand="gvUsers_RowCommand" 
        OnSelectedIndexChanged="gvUsers_SelectedIndexChanged" OnSelectedIndexChanging="gvUsers_SelectedIndexChanging" 
 
OnSorted="gvUsers_Sorted" 
        >
        <HeaderStyle BackColor="#191970" ForeColor="#FFFFFF" Font-Names="Verdana,Arial" Font-Bold="true" HorizontalAlign="center" />
        <RowStyle HorizontalAlign="center" Font-Names="Verdana,Arial" />
        <AlternatingRowStyle HorizontalAlign="center" Font-Names="Verdana,Arial" BackColor="#FFE4C4" />
        <Columns>
            <asp:CommandField ButtonType="Button" SelectText="Details" ShowSelectButton="true" ControlStyle-CssClass="Button" />
            <asp:BoundField ItemStyle-Width="200" HeaderText="Email" DataField="Email" SortExpression="Email" />
            <asp:BoundField ItemStyle-Width="160" HeaderText="First Name" DataField="FirstName" SortExpression="FirstName" />
            <asp:BoundField ItemStyle-Width="160" HeaderText="Last Name" DataField="LastName" SortExpression="LastName" />
            <asp:BoundField ItemStyle-Width="160" HeaderText="Company Name" DataField="CompanyName" SortExpression="CompanyName" />
            <asp:BoundField ItemStyle-Width="100" HeaderText="Zip Code" DataField="Zip" SortExpression="Zip" />
            <asp:BoundField ItemStyle-Width="120" HeaderText="Phone Number" DataField="PhoneNumber" SortExpression="PhoneNumber" />
            <asp:BoundField ItemStyle-Width="100" HeaderText="User Status" DataField="UserStatusText" SortExpression="UserStatusPKSEQ" 
 
/>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="btnResetPwd" runat="server" Text="Reset Pwd" CommandName="ResetPwd" 
 
CommandArgument='<%#Bind("PKSEQ") %>' CssClass="Button" OnClientClick="return confirm('Do you really want to reset this user's 
 
password?');" />
                    <asp:HiddenField ID="txtPKSEQ" runat="server" Value='<%#Bind("PKSEQ") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    
    </asp:GridView>
    
 
 
    <asp:ObjectDataSource ID="odsUserDetail" runat="server" 
        SelectMethod="fnFetchSingleUser" EnableViewState="false"  
        TypeName="RandomLengths.MyPrintCodeBlocks.BusinessObjects.CUserCollection" 
        DataObjectTypeName="RandomLengths.MyPrintCodeBlocks.BusinessObjects.CUser"
        OldValuesParameterFormatString="original_{0}" OnUpdating="odsUserDetail_Updating" 
        UpdateMethod="UpdateUser" >
        <SelectParameters>
            <asp:ControlParameter ControlID="gvUsers" Name="argUserPKSEQ"
                PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="Email" Type="String" />
            <asp:Parameter Name="FirstName" Type="String" />
            <asp:Parameter Name="MiddleName" Type="String" />
            <asp:Parameter Name="LastName" Type="String" />
            <asp:Parameter Name="Address1" Type="String" />
            <asp:Parameter Name="Address2" Type="String" />
            <asp:Parameter Name="City" Type="String" />
            <asp:ControlParameter Name="STProv" ControlID="ddlSTProv" PropertyName="SelectedValue" Type="Char" Size="2" />
            <asp:Parameter Name="Zip" Type="String" />
            <asp:ControlParameter Name="Country" ControlID="ddlCountry" PropertyName="SelectedValue" Type="Char" Size="2" />
            <asp:Parameter Name="PhoneNumber" Type="String" />
            <asp:Parameter Name="FaxNumber" Type="String" />
            <asp:Parameter Name="CompanyName" Type="String" />
            <asp:Parameter Name="Title" Type="String" />
            <asp:ControlParameter Name="FavoriteQuestion" ControlID="ddlFavoriteQuestion" PropertyName="SelectedValue" Type="String" 
 
/>
            <asp:Parameter Name="FavoriteAnswer" Type="String" />
            <asp:Parameter Name="IsPromoByEmail" Type="Boolean" />
            <asp:Parameter Name="IsPromoByPost" Type="Boolean" />
            <asp:Parameter Name="IsPromoByFax" Type="Boolean" />
        </UpdateParameters>
        </asp:ObjectDataSource>
 
    <asp:DetailsView ID="dvUser" runat="server" AutoGenerateRows="False" CellPadding="5"  
        DataKeyNames="PKSEQ" DataSourceID="odsUserDetail" EnableViewState="false" 
        HeaderText="User Details" OnItemUpdating="dvUser_ItemUpdating" OnItemUpdated="dvUser_ItemUpdated" Width="350"
        OnDataBound="dvUser_DataBound" OnItemCommand="dvUser_ItemCommand" >
        <HeaderStyle BackColor="#191970" ForeColor="#FFFFFF" Font-Names="Verdana,Arial" Font-Bold="true" HorizontalAlign="center" />
        <RowStyle HorizontalAlign="left" Font-Names="Verdana,Arial" />
        <FieldHeaderStyle HorizontalAlign="Right" Font-Bold="true"  />
        <CommandRowStyle BackColor="#191970" ForeColor="#FFFFFF" />
        <AlternatingRowStyle HorizontalAlign="left" Font-Names="Verdana,Arial" BackColor="#FFE4C4" />
        <EditRowStyle HorizontalAlign="Left" />
        <Fields>
            <asp:CommandField ButtonType="Button" EditText="Edit User" UpdateText="Update User" 
                CancelText="Cancel Changes" Visible="true" ShowEditButton="true" ControlStyle-CssClass="Button" />
            <asp:BoundField HeaderText="Email" DataField="Email" />
            <asp:BoundField HeaderText="First Name" DataField="FirstName" />
            <asp:BoundField HeaderText="Middle Name" DataField="MiddleName" />
            <asp:BoundField HeaderText="Last Name" DataField="LastName" />
            <asp:BoundField HeaderText="User Status" DataField="UserStatusText" ReadOnly="True" />
            <asp:BoundField HeaderText="Address 1" DataField="Address1" />
            <asp:BoundField HeaderText="Address 2" DataField="Address2" />
            <asp:BoundField HeaderText="City" DataField="City" />
            <asp:TemplateField HeaderText="State/Province" >
                <ItemTemplate>
                    <asp:Label ID="STProv" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "STProvName") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlSTProv" runat="server" />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="Zip" DataField="Zip" />
            <asp:TemplateField HeaderText="Country" >
                <ItemTemplate>
                    <asp:Label ID="Country" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CountryName") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlCountry" runat="server" />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="Phone Number" DataField="PhoneNumber" />
            <asp:BoundField HeaderText="Fax Number" DataField="FaxNumber" />
            <asp:BoundField HeaderText="Company Name" DataField="CompanyName" />
            <asp:BoundField HeaderText="Title" DataField="Title" />
            <asp:TemplateField HeaderText="Security Question" >
                <ItemTemplate>
                    <asp:Label ID="FavoriteQuestion" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "FavoriteQuestion") 
 
%>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlFavoriteQuestion" runat="server" >    
                        <asp:ListItem Value="x" Text="Select a question ..." />
                        <asp:ListItem Value="What is my Mother's maiden name?" Text="What is my Mother's maiden name." />
                        <asp:ListItem Value="What is my Father's middle name?" Text="What is my Father's middle name." />
                        <asp:ListItem Value="Who was my favorite teacher in high school?" Text="Who was my favorite teacher in high 
 
school?" />
                        <asp:ListItem Value="What was the name of my elementary school?" Text="What was the name of my elementary 
 
school?" />
                        <asp:ListItem Value="What is my favorite movie?" Text="What is my favorite movie?" />
                        <asp:ListItem Value="What is my favorite song?" Text="What is my favorite song?" />
                        <asp:ListItem Value="What make/model was my first car?" Text="What make/model was my first car?" />
                        <asp:ListItem Value="In what city was I born?" Text="In what city was I born?" />
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="Security Answer" DataField="FavoriteAnswer" />
            <asp:CheckBoxField HeaderText="Promo By Email" DataField="IsPromoByEmail" />
            <asp:CheckBoxField HeaderText="Promo By Post" DataField="IsPromoByPost" />
            <asp:CheckBoxField HeaderText="Promo By Fax" DataField="IsPromoByFax" />
            <asp:BoundField HeaderText="Last Login Date" DataField="LastLoginDate" ReadOnly="True" />
            <asp:TemplateField Visible="false" >
                <EditItemTemplate>
                    <asp:Label ID="lblModifiedBy" runat="server" />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField Visible="true">
                <ItemTemplate>
                    <asp:HiddenField ID="txtSelectedUserPKSEQ" runat="server" />
                    <asp:Button ID="btnRemoveUser" runat="server" Text="Remove User" OnClientClick="return confirm('Do you really want 
 
to permanently remove this User from My Print?');"
                        CommandName="RemoveUser" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "PKSEQ") %>' 
 
CausesValidation="false" />
                </ItemTemplate>
            </asp:TemplateField>
        </Fields>
    </asp:DetailsView>

Open in new window

Avatar of Velio
Velio
Flag of South Africa image

couldn't you just manually invoke the event handling method after you've set the appropriate selectedindex?
The problem is that SelectedIndex is the ordinal number of the selected row in the GridView. Ideally we would need to set up the SelectedValue property, which is read-only and contains the primary key of the selected row ("PKSEQ").
ASKER CERTIFIED SOLUTION
Avatar of Edy
Edy
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry for the mistakes, here's again the explanation on the behavior:

- If the session variable is present then the DetailsView will show that record and the GridView will show the record list normally, with no selected record.
- If the user selects a different record in the GridView, then the DetailsView will be updated accordingly (GridView.SelectedValue takes priority over the Session variable).
Code fixed. The previous code seems to work fine with Request.QueryString["variable"] directly, but as Session values are objects then we must check whether the value is present or not.

if (Session["yoursessionvariable"] != null)
   odsUserDetail.SelectParameters["argUserPKSEQ"].DefaultValue = Session["yoursessionvariable"].ToString();

Open in new window