rawilken
asked on
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 GridViewAllSelectedIndexCh anged(send er As Object, e As EventArgs) Handles GridViewAll.SelectedIndexC hanged
Dim row As GridViewRow = GridViewAll.SelectedRow
Dim strfirst As String = GridViewAll.SelectedRow.Ce lls(2).Tex t
Dim strmiddle As String = GridViewAll.SelectedRow.Ce lls(3).Tex t
Dim strlast As String = GridViewAll.SelectedRow.Ce lls(1).Tex t
txtSelectedPerson.Text = strlast & ", " & strfirst & " " & strmiddle
strItemSelected.Text = GridViewAll.SelectedRow.Ce lls(0).Tex t
End Sub
But nothing happens when I click the Select button in the GridView control.
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 GridViewAllSelectedIndexCh
Dim row As GridViewRow = GridViewAll.SelectedRow
Dim strfirst As String = GridViewAll.SelectedRow.Ce
Dim strmiddle As String = GridViewAll.SelectedRow.Ce
Dim strlast As String = GridViewAll.SelectedRow.Ce
txtSelectedPerson.Text = strlast & ", " & strfirst & " " & strmiddle
strItemSelected.Text = GridViewAll.SelectedRow.Ce
End Sub
But nothing happens when I click the Select button in the GridView control.
ASKER
Thanks, but can I get it in VB? I am not familiar with C# yet.
ASKER
Is there an event that fires before the selectedindexchanged event that I need to include?
VB.NET code to get the row:
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
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
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
ASKER
ASKER
This is what I have tried..
Tried both Dim row statements, one at a time...
Protected Sub GridViewAllSelectedIndexCh anged(send er As Object, e As EventArgs) Handles GridViewAll.SelectedIndexC hanged
Dim row As GridViewRow = TabContainer1.tabEmployeeL ist.GridVi ewAll.Sele ctedRow
'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
Tried both Dim row statements, one at a time...
Protected Sub GridViewAllSelectedIndexCh
Dim row As GridViewRow = TabContainer1.tabEmployeeL
'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
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.
- 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.
ASKER
I was on vacation. I am now back and looking to resolve this. Here are the error message, code behind, and markup
Markup.JPG
Markup.JPG
ASKER
markup for html to display cell values...
<asp:Label ID="lblSelectedPerson" runat="server" Text="Details for "
Visible="False" BackColor="Silver"></asp:L abel>
<asp:TextBox ID="txtSelectedPerson" runat="server" BorderStyle="None"
Visible="False" Width="338px" BackColor="Silver"></asp:T extBox>
<asp:Label ID="lblSelectedPerson" runat="server" Text="Details for "
Visible="False" BackColor="Silver"></asp:L
<asp:TextBox ID="txtSelectedPerson" runat="server" BorderStyle="None"
Visible="False" Width="338px" BackColor="Silver"></asp:T
ASKER
Grid
<asp:GridView ID="GridViewAll" runat="server" AllowSorting="True"
AutoGenerateColumns="False " DataSourceID="EmployeesDat aSource1"
AllowPaging="True" DataKeyNames="SSNO" OnRowDataBound="GridViewDa taBoundEve ntHandler" CellPadding="5" Height="160px">
<PagerSettings Mode="NextPreviousFirstLas t" FirstPageText="First" LastPageText="Last" NextPageText="Next" PreviousPageText="Previous "></PagerS ettings>
<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>
<asp:GridView ID="GridViewAll" runat="server" AllowSorting="True"
AutoGenerateColumns="False
AllowPaging="True" DataKeyNames="SSNO" OnRowDataBound="GridViewDa
<PagerSettings Mode="NextPreviousFirstLas
<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>
ASKER
Clicking select button does not display any values of the row selected.
ASKER
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.
Notice that the gridview markup does not define the event.
You should have the following attribute:
onselectedindexchanged="Gr idViewAllS electedInd exChanged"
Let us know where the event is defined. For further info you can check:
http://stackoverflow.com/questions/9166568/gridview-onselectedindexchanged-event-not-firing
You should have the following attribute:
onselectedindexchanged="Gr
Let us know where the event is defined. For further info you can check:
http://stackoverflow.com/questions/9166568/gridview-onselectedindexchanged-event-not-firing
ASKER
It has been defined from the beginning...here is the full function...
Protected Sub GridViewAllSelectedIndexCh anged(send er As Object, e As EventArgs) Handles GridViewAll.SelectedIndexC hanged
'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.
Protected Sub GridViewAllSelectedIndexCh
'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
'lblSelectedPerson.Visible
'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.
ASKER
Here is a screenshot of the GridView control
GridView.JPG
GridView.JPG
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
C# -(GridViewRow)(((Control)s
Open in new window
Note: I use CType but you can use DirectCast too.