asp.net Gridview

I want to do one of two things...
A: Use the select button from Gridview control, populate a text box with the DataKeyName of the selected it. Use the text box to control what appears on four other DetailsView controls.

B: Use two text boxes, either of which may be populated or not, to filter the records in the Gridview control. Use the Select button to select a record. The DataKeyName for that selected item will then control what appears on four other DetailsView controls.


I attempted A and got to this...

 Protected Sub GridViewAllSelectedIndexChanged(sender As Object, e As EventArgs) Handles GridViewAll.SelectedIndexChanged
            Dim row As GridViewRow = GridViewAll.SelectedRow
            Dim strfirst As String = GridViewAll.SelectedRow.Cells(2).Text
            Dim strmiddle As String = GridViewAll.SelectedRow.Cells(3).Text
            Dim strlast As String = GridViewAll.SelectedRow.Cells(1).Text
            txtSelectedPerson.Text = strlast & ", " & strfirst & " " & strmiddle
            strItemSelected.Text = GridViewAll.SelectedRow.Cells(0).Text
        End Sub

But nothing happens when I click the Select button in the GridView control.
rawilkenAsked:
Who is Participating?
 
Miguel OzSoftware EngineerCommented:
Ok, you need to define in the markup
Add the following attribute to the grideview: (You can locate after OnRowDataBound attribute)
onselectedindexchanged="GridViewAllSelectedIndexChanged"

and change method declaration in code behind as: (Notice that I do not use Handles)
Protected Sub GridViewAllSelectedIndexChanged(sender As Object, e As EventArgs)

If more help required please include the following info:
- Complete markup  and code behind as text file.
- Quick watch screenshots of sender object contents and CType(sender, Control).NamingContainer.
0
 
Miguel OzSoftware EngineerCommented:
You need to get row from sender as follows:
C# -(GridViewRow)(((Control)sender).NamingContainer);
Dim row As GridViewRow = CType(CType(sender, Control).NamingContainer, GridViewRow)

Open in new window

Note: I use CType but you can use DirectCast too.
0
 
rawilkenAuthor Commented:
Thanks, but can I get it in VB? I am not familiar with C# yet.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
rawilkenAuthor Commented:
Is there an event that fires before the selectedindexchanged event that I need to include?
0
 
Miguel OzSoftware EngineerCommented:
VB.NET code to get the row:
Protected Sub GridViewAllSelectedIndexChanged(sender As Object, e As EventArgs) Handles GridViewAll.SelectedIndexChanged
            Dim row As GridViewRow = CType(CType(sender, Control).NamingContainer, GridViewRow)
            Dim strfirst As String = row.Cells(2).Text
            Dim strmiddle As String = row.Cells(3).Text
            Dim strlast As String = row.Cells(1).Text
            txtSelectedPerson.Text = strlast & ", " & strfirst & " " & strmiddle
            strItemSelected.Text = row.Cells(0).Text
        End Sub

Open in new window

Note: I assume your cells exist.

Regarding post question about selectedindexchanged, have a look at MSDN:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedindexchanged.aspx#Y0

If you need more post your markup and code in a separate question. I thought your issue was only the method in the original question
0
 
rawilkenAuthor Commented:
mas_oz, thanks, but I am getting an error because the GridView control is contained in an ajaxtoolkit:tabcontainer...
error message

Also, I did see MSDN page, but using Dim row As GridViewRow = GridViewAll.SelectedRow failed. Nothing seemed to work.
0
 
rawilkenAuthor Commented:
This is what I have tried..

Tried both Dim row statements, one at a time...

Protected Sub GridViewAllSelectedIndexChanged(sender As Object, e As EventArgs) Handles GridViewAll.SelectedIndexChanged
            Dim row As GridViewRow = TabContainer1.tabEmployeeList.GridViewAll.SelectedRow
            'Dim row As GridViewRow = CType(CType(sender, Control).NamingContainer, GridViewRow)
            Dim strfirst As String = row.Cells(2).Text
            Dim strmiddle As String = row.Cells(3).Text
            Dim strlast As String = row.Cells(1).Text
            txtSelectedPerson.Text = strlast & ", " & strfirst & " " & strmiddle
            txtSelectedPerson.Visible = True
            lblSelectedPerson.Visible = True
            tabEmployeeData.Focus()
        End Sub
0
 
Miguel OzSoftware EngineerCommented:
Please post:
-  your markup
- code behind.
- Quick watch screenshots of sender object contents and CType(sender, Control).NamingContainer.

Note: Sender should contain a reference to the gridview row if the event is associated with the gridview.
0
 
rawilkenAuthor Commented:
I was on vacation. I am now back and looking to resolve this. Here are the error message, code behind, and markup
Markup.JPG
0
 
rawilkenAuthor Commented:
markup for html to display cell values...


<asp:Label ID="lblSelectedPerson" runat="server" Text="Details for " 
        Visible="False" BackColor="Silver"></asp:Label>
    <asp:TextBox ID="txtSelectedPerson" runat="server" BorderStyle="None"
        Visible="False" Width="338px" BackColor="Silver"></asp:TextBox>
0
 
rawilkenAuthor Commented:
Grid

<asp:GridView ID="GridViewAll" runat="server" AllowSorting="True"
                    AutoGenerateColumns="False" DataSourceID="EmployeesDataSource1"
                    AllowPaging="True" DataKeyNames="SSNO" OnRowDataBound="GridViewDataBoundEventHandler" CellPadding="5" Height="160px">
                    <PagerSettings Mode="NextPreviousFirstLast" FirstPageText="First" LastPageText="Last" NextPageText="Next" PreviousPageText="Previous"></PagerSettings>
                    <Columns>
                        <asp:CommandField ShowSelectButton="True"  ButtonType="Button"/>
                        <asp:BoundField DataField="[SSNO]" HeaderText="SSNO" SortExpression="[SSNO]" Visible="False" />
                        <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />
                        <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
                        <asp:BoundField DataField="MName" HeaderText="Middle Name" SortExpression="MName" />
                        <asp:BoundField DataField="PHONE1" HeaderText="Phone 1" SortExpression="PHONE1" />
                        <asp:BoundField DataField="PHONE2" HeaderText="Phone 2" SortExpression="PHONE2" />
                        <asp:BoundField DataField="Pager" HeaderText="Cell/Pager" SortExpression="Pager" />
                        <asp:BoundField DataField="Shift" HeaderText="Shift" SortExpression="Shift" />
                        <asp:BoundField DataField="Super" HeaderText="Supervisor" SortExpression="Supervisor" />
                    </Columns>
                    <SelectedRowStyle BackColor="Gainsboro" />
                </asp:GridView>
0
 
rawilkenAuthor Commented:
Clicking select button does not display any values of the row selected.
0
 
rawilkenAuthor Commented:
What I want to do is that when a record is selected, I want the values for the fields FirstName, LastName and MName to be displayed above the AjaxTabControl so that the user know which record has been selected when they go to the other tabs. The select button is on the first tab.
0
 
Miguel OzSoftware EngineerCommented:
Notice that the gridview markup does not define the event.
You should have the following attribute:
onselectedindexchanged="GridViewAllSelectedIndexChanged"

Let us know where the event is defined. For further info you can check:
http://stackoverflow.com/questions/9166568/gridview-onselectedindexchanged-event-not-firing
0
 
rawilkenAuthor Commented:
It has been defined from the beginning...here is the full function...

Protected Sub GridViewAllSelectedIndexChanged(sender As Object, e As EventArgs) Handles GridViewAll.SelectedIndexChanged
            'Dim row As GridViewRow = GridViewAll.SelectedRow
            'Dim row As GridViewRow = CType(CType(sender, Control).NamingContainer, GridViewRow)
            'Dim strfirst As String = row.Cells(2).Text
            'Dim strmiddle As String = row.Cells(3).Text
            'Dim strlast As String = row.Cells(1).Text
            'txtSelectedPerson.Text = strlast & ", " & strfirst & " " & strmiddle
            'txtSelectedPerson.Visible = True
            'lblSelectedPerson.Visible = True
            'tabEmployeeData.Focus()
        End Sub

I have commented out the code for now, but it just does nothing. I have tried two ways of getting the row number from the selected row, both to no avail.
0
 
rawilkenAuthor Commented:
Here is a screenshot of the GridView control
GridView.JPG
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.