Solved

ASP.net Datagrid Click event??

Posted on 2003-11-11
3
342 Views
Last Modified: 2008-02-01
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,

0
Comment
Question by:TechQT
3 Comments
 
LVL 10

Expert Comment

by:GoodJun
ID: 9725313
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.
0
 
LVL 5

Expert Comment

by:arvindb1
ID: 9728839
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
0
 
LVL 6

Accepted Solution

by:
thomasdodds earned 250 total points
ID: 9734561
along with arvindb1's comment - you can set the DataKeyField for your data grid (instead of using the hidden field approach) in the databindings properties and then access it on the DataGrid_OnSelectedItemChanged Event by using

dim key as string = datagrid.datakeys(e.item.itemindex).tostring

both are acceptable ... just wanted to give you options ...
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now