Link to home
Start Free TrialLog in
Avatar of TechQT
TechQT

asked on

ASP.net Datagrid Click event??

Hi I am am trying to find the EASIEST way to have a user click on a populated datagrid (via a programmatic dataset) to populate text boxes and lists external to the datagrid.  Sort of a click the row you want to see detail on kind of functionality.   Is there a way to click any row in the datagrid and simply return the value of my hidden ID field into a variable to use for my control population on the rest of the page????

Thanks All,

Avatar of GoodJun
GoodJun

In the property builder for the grid. Columns-->select the column that you want user to click that will raise the event, type in "Select" in the Command name: textbox.

Then you can code the SelectedIndexChanged event when users clicked an item in this column.
Do something like this

In your asp.net page

<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
                        <Columns>
                              <asp:TemplateColumn Visible="False" HeaderText="HiddenColumn">
                                    <ItemTemplate>
                                          <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.HiddenField") %>'>
                                          </asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                          <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.HiddenField") %>'>
                                          </asp:TextBox>
                                    </EditItemTemplate>
                              </asp:TemplateColumn>
                              <asp:TemplateColumn HeaderText="FirstColumn">
                                    <ItemTemplate>
                                          <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.firstfield") %>'>
                                          </asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                          <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.firstfield") %>'>
                                          </asp:TextBox>
                                    </EditItemTemplate>
                              </asp:TemplateColumn>
                              <asp:TemplateColumn HeaderText="SecondColumn">
                                    <ItemTemplate>
                                          <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.SecondColumn") %>'>
                                          </asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                          <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.SecondColumn") %>'>
                                          </asp:TextBox>
                                    </EditItemTemplate>
                              </asp:TemplateColumn>
                              <asp:TemplateColumn HeaderText="Select">
                                    <ItemTemplate>
                                          <asp:LinkButton runat="server" Text="Button" CommandName="SelectClick" CausesValidation="false"></asp:LinkButton>
                                    </ItemTemplate>
                              </asp:TemplateColumn>
                        </Columns>
                  </asp:DataGrid>


and in your code behind
Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand
        If (e.CommandName = "Select") Then
            Dim hidfield As String
            hidfield = e.Item.Cells(0).Text()
        End If

    End Sub

Hope it helps
Arvind
ASKER CERTIFIED SOLUTION
Avatar of thomasdodds
thomasdodds

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