CSharper
CSharper

Reputation: 5580

Fire SelectionIndexChanged with AutoGenerateSelect "False"

I have a datagrid view, and I'd like to capture a datakeyname value on a selected row of a gridview. But in this scenario one of my columns (Event Title) will be a hyperlink, and when this link is clicked it will fire selectedindexchanged and capture DataKeyName which will be passed to populate a form. Here's my code. Currently my hyperlink field is not eve "clickable". Do I need to change my approach?

<asp:GridView ID="gvEventDetails" CssClass="gvEvent" runat="server" DataKeyNames="Event_ID"
                AutoGenerateColumns="false" OnSelectedIndexChanged="gvEventDetails_SelectedIndexChanged" >     
     <Columns>        
         <asp:TemplateField HeaderText="Event Title">
            <ItemTemplate>                                                                                       
               <asp:HyperLink ID="HyperLink1" Text='<%# Eval("Event_Title") %>' runat="server"></asp:HyperLink>
              </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Client Name">
            <ItemTemplate>
               <asp:Label ID="ClientName" runat="server" Text='<%# Eval("Name") %>'>
               </asp:Label>
             </ItemTemplate>
          </asp:TemplateField>

The method:

 protected void gvEventDetails_SelectedIndexChanged(object sender, EventArgs e)
    {
        string DataKey = gvEventDetails.SelectedValue.ToString();
    }  

Upvotes: 0

Views: 83

Answers (2)

CSharper
CSharper

Reputation: 5580

This is what I ended up doing

 protected void gvEventDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {           
          string PK = (e.CommandArgument).ToString();                    
    }
             <asp:TemplateField HeaderText="Event Title">
                <ItemTemplate>                                                                                       
                    <asp:LinkButton ID="HyperLink1" runat="server" Text='<%# Eval("Event_Title") %>'
                    CommandArgument='<%# Eval("Event_ID") %>'
                        ></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>  

Upvotes: 0

Wiktor Zychla
Wiktor Zychla

Reputation: 48279

Instead of a hyperlink, try a linkbutton with command name:

<asp:TemplateField>
   <ItemTemplate>
       <asp:LinkButton runat="server" CommandName="Select" CommandArgument='<%# Eval("Event_ID") %>' Text='<%# Eval("Event_Title") %>'
   ...

The rule is that posting back with common command names (Edit, Select, Delete, Cancel etc.) is correctly recognized by the event engine.

Upvotes: 1

Related Questions