Hi there!
I have generated a GridView that looks something like:
SportName| CompanyNameX |CompanyNameY |CompanyNameZ
Hockey.....| Shipping------------ |Accounting-------- |Shipping------------
BaseBall...| Receiving-----------|Shipping------------|Accounting---------
Etc............| Accounting----------|Receiving---------- |Receiving-----------
Where there are an unknown number of Company Names and an unknown number of Sport Names....these are dynamically generated based on a database.
I've added link buttons to each of the Department Names.
When these are clicked I need to be able to pull up details on the department based on the Company Name and the Sport Name and display them in a panel below the GridView.
My problem is that I have no idea how to handle the link button clicks.
I've created a Template Class to convert the data to be displayed in the GridView into Link buttons...here's a quick snippet -
Private Sub DisplayGV()
-
Dim dv As DataView = CreateDataSource()
-
myGV.DataSource= dv
-
For i As Integer = 0 To dv.Table.Columns.Count -1
-
Dim tf As New TemplateField
-
tf.ItemTemplate = New GridViewClickableTemplate(DataControlRowType.DataRow, _
-
_dv.Table.Columns(i).ColumnName)
-
tf.HeaderTemplate = New GridViewClickableTemplate(DataControlRowType.Header, _dv.Table.Columns(i).ColumnName)
-
myGV.Columns.Add(tf)
-
Next
-
myGV.DataBind()
-
-
End Sub
-
-
-
Private Class GridViewClickableTemplate
-
Implements ITemplate
-
Private _templateType As DataControlRowType
-
Private _columnName As String
-
-
Sub New(ByVal type As DataControlRowType, ByVal colname As String)
-
_templateType = type
-
_columnName = colname
-
-
End Sub
-
-
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
-
Select Case _templateType
-
Case DataControlRowType.Header
-
Dim headerText As New Label
-
headerText.Text = _columnName
-
container.Controls.Add(headerText)
-
Case DataControlRowType.DataRow
-
Dim lnkBtn As New LinkButton
-
AddHandler lnkBtn.DataBinding, AddressOf Me.BindLinkButtons
-
-
container.Controls.Add(lnkBtn)
-
Case Else
-
End Select
-
End Sub
-
-
Private Sub BindLinkButtons(ByVal sender As Object, ByVal e As EventArgs)
-
Dim lnkBtn As LinkButton = CType(sender, LinkButton)
-
Dim row As GridViewRow = CType(lnkBtn.NamingContainer, GridViewRow)
-
Dim content As String = DataBinder.Eval(row.DataItem, _columnName).ToString
-
lnkBtn.Text = content
-
If String.Compare(content, "Shipping") = 0 Then
-
lnkBtn.CssClass = "red"
-
lnkBtn.CommandName = "DisplayInfo"
-
lnkBtn.CommandArgument = 1
-
ElseIf String.Compare(content, "Accounting") = 0 Then
-
lnkBtn.CssClass = "green"
-
lnkBtn.CommandName = "DisplayInfo"
-
lnkBtn.CommandArgument = 2
-
ElseIf content.StartsWith("Receiving") Then
-
lnkBtn.CssClass = "blue"
-
lnkBtn.CommandName = "DisplayInfo"
-
lnkBtn.CommandArgument = 3
-
End If
-
End Sub
-
End Class
-
I have a sub named DisplayInfo and would have assumed that the CommandName would link the link buttons to this command: -
Protected Sub DisplayInfo (ByVal sender As Object, ByVal e As EventArgs)
-
Dim lnkBtn As LinkButton = CType(sender, LinkButton)
-
DisplayTheStuff(lnkBtn.CommandArgument)
-
End Sub
-
However DisplayInfo is never reached...
How do you handle dynamically created link buttons in a GridView through one event handler?
Thanks
-LilOlMe
9 11864
I think you will need to use the RowCommand event of the GridView to accomplish what you are trying to do. In this event you should be able to check the value of CommandName using GridViewCommandEventArgs e parameter. Give this a try and let us know if you get stuck on something.
Nathan
I think you will need to use the RowCommand event of the GridView to accomplish what you are trying to do. In this event you should be able to check the value of CommandName using GridViewCommandEventArgs e parameter. Give this a try and let us know if you get stuck on something.
Nathan
The RowCommand event did not fire.
The RowCommand event did not fire.
Do you have the RowCommand event wired up for the GridView? You can right click the GridView in Design View and click Properties then click the lightning bolt at the top of the properties window. Now double click on the RowCommand event. This will create the event in your code behind file. Set a breakpoint in the Page_Load event and also in the RowCommand event. Tell me where your breakpoint hits after the Page_Load event.
Nathan
Do you have the RowCommand event wired up for the GridView? You can right click the GridView in Design View and click Properties then click the lightning bolt at the top of the properties window. Now double click on the RowCommand event. This will create the event in your code behind file. Set a breakpoint in the Page_Load event and also in the RowCommand event. Tell me where your breakpoint hits after the Page_Load event.
Nathan
It doesn't hit a break point after the Page_Load break point. -
<asp:GridView ID="MyGV" runat="server" CellPadding="4"
-
BorderColor="Black" BorderWidth="1" GridLines="both"
-
ForeColor="#333333" PagerSettings-Position=top
-
allowpaging=true PageSize=10 PagerSettings-Mode=NumericFirstLast
-
autogeneratecolumns="false"
-
onrowcommand="MyGV_RowCommand">
-
-
<FooterStyle BackColor="#104E8B" Font-Bold="True" ForeColor="White" />
-
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
-
<PagerStyle BackColor="#5D7B9D" ForeColor="White" HorizontalAlign="Center" />
-
<HeaderStyle BackColor="#104E8B" Font-Bold="True" ForeColor="White" />
-
</asp:GridView>
-
-
Protected Sub MyGV_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles MyGV.RowCommand
-
Dim lnkBtn As LinkButton = CType(sender, LinkButton)
-
ShowDetails(lnkBtn.CommandArgument)
-
End Sub
-
I've had something similar happen once before...when I had given more than one ListItem the same Value property in a DropDownList....the SelectedIndexChanged never fired in that case.
I don't know why it would seem to be the same problem with LinkButtons though, each of them has a unique name, the CommandName and the Arguments may be duplicated...but the are individual LinkButtons...so I'm not sure it's the same problem.
Thanks a lot for your help...it's greatly appreciated.
-LilOlMe
Try using the e parameter of the RowCommand event to assign the parameter value of ShowDetails.
ShowDetails(e.CommandArgument);
Nathan
- <LI style="FONT-SIZE: 8pt; BACKGROUND: #fcfcfc">[font='Courier New', Courier, monospace] [/font]
[font='Courier New', Courier, monospace]Try changing your RowCommand event to use the GridViewCommandEventArgs parameter.[/font]
[font='Courier New', Courier, monospace] [/font]- <LI style="FONT-SIZE: 8pt; BACKGROUND: #fcfcfc">[font='Courier New', Courier, monospace]Protected [color=#0600ff]Sub[/color] MyGV_RowCommand[color=#000000]([/color][color=#ff8000]ByVal[/color] sender [color=#ff8000]As[/color] [color=#ff0000]Object[/color], [color=#ff8000]ByVal[/color] e [color=#ff8000]As[/color] System.[color=#0000ff]Web[/color].[color=#0000ff]UI[/color].[color=#0000ff]WebControls[/color].[color=#0000ff]GridViewCommandEventArgs[/color] [color=#000000])[/color] [color=#ff8000]Handles[/color] MyGV.[color=#0000ff]RowCommand[/color][/font]
<LI style="FONT-SIZE: 8pt; BACKGROUND: #fcfcfc">[font='Courier New', Courier, monospace] ShowDetails[color=#000000](e[/color].[color=#0000ff]CommandArgument[/color][color=#000000])[/color][/font] - [font='Courier New', Courier, monospace] [color=#0600ff]End[/color] [color=#0600ff]Sub[/color][/font]
[font='Courier New', Courier, monospace][color=#0600ff]Nathan[/color][/font]
I don't understand....
Could you please repost?
This really isn't a "Row Command" that I'm issuing
I'm issuing a LinkButton to mimic what I would call a "Cell Command"......
Each Cell contains a LinkButton (not ButtonFields...)
Seeing as the RowCommand Event is not fired when I click these link buttons, I'm still wondering how to capture the click events for each of these link buttons.
There's a Lot of them:
There can be 2 - 40 columns, and 1-511 rows....that's a lot of link buttons...
up to (40 X 511) 20440 link buttons.
<edit> correction: there can only be 40X10 at one time because the GridView pages...so 400 link buttons...still a lot</edit>
I just need to check the value of the cell that's been selected...Just need to check the Command Argument of the specific link button clicked.
I solved the problem...though it feels like somewhat of a workaround.
I never did get the RowCommand event to fire.
When I converted the GridView's content into LinkButtons I added a JavaScript call: -
Private Sub BindLinkButtons(ByVal sender As Object, ByVal e As EventArgs)
-
Dim lnkBtn As LinkButton = CType(sender, LinkButton)
-
Dim row As GridViewRow = CType(lnkBtn.NamingContainer, GridViewRow)
-
Dim content As String = DataBinder.Eval(row.DataItem, _columnName).ToString
-
lnkBtn.Text = content
-
If String.Compare(content, "Shipping") = 0 Then
-
lnkBtn.CssClass = "red"
-
lnkBtn.Attributes.Add("onclick", "javascript:SetDisplayCode('0');")
-
ElseIf String.Compare(content, "Accounting") = 0 Then
-
lnkBtn.CssClass = "green"
-
lnkBtn.Attributes.Add("onclick", "javascript:SetDisplayCode('255');")
-
ElseIf content.StartsWith("Receiving") Then
-
lnkBtn.CssClass = "blue"
-
lnkBtn.Attributes.Add("onclick", "javascript:SetDisplayCode('2');")
-
End If
-
End Sub
-
The JavaScript function sets a hidden field with the "display code" and I check that in the PageLoad and automatically display the thing that needs to be displayed.
Thanks again for your help!
-LilOlMe
Sign in to post your reply or Sign up for a free account.
Similar topics
by: brian |
last post by:
'visual studio 2002 pro 1.0 framwork'
I am creating dynamic Link buttons on my intranet site
and assigning a click_event to them. A user clicks a
link and I call the method to recreate the...
|
by: WebBuilder451 |
last post by:
I have a series of dynamic link buttons created based upon a datareader. I've
added a click event and it calls the sub ok:
example: "while loop through the reader"
Dim ltrCtrl As New...
|
by: NateDawg |
last post by:
Ok, I’ve noticed a few gridview problems floating around the forum. Everyone
wants to do a java confirmation box when a user clicks the delete button.
Fair enough, basic user design rules state...
|
by: Miguel Dias Moura |
last post by:
Hello,
I have a GridView in my page which is created in runtime. It works fine.
My page has 2 Asp Buttons:
- The HIDE button makes GridView.Visible = False;
- The SHOW button makes...
|
by: Achim Domma (SyynX Solutions GmbH) |
last post by:
Hi,
in a repeater control I can create a postback link like this:
<a href=<%# DataBinder.Eval(Container.DataItem, "EditLink") %>></a>
The EditLink property of the data item uses...
|
by: Bishop |
last post by:
I have a number of simple select queries that a user needs to be able to
execute and display the data on the screen in a gridview. My thought was
that I could use a Gridview set to dymanically...
|
by: Kevin Attard |
last post by:
I am using a GridView inside a UserControl which has a template column for
deleting the rows. Before databinding the gridview i am attaching the
RowCommand and RowDataBound event.
I am using the...
|
by: Evan M. |
last post by:
Here's my GridView and my SqlDataSource
<asp:GridView ID="ContactHistoryGrid" runat="server"
AutoGenerateColumns="False" DataSourceID="ContactHistoryDS"
DataKeyNames="JobHistoryID"...
|
by: jrnail23 |
last post by:
I have a user control which contains an UpdatePanel, which contains a
MultiView inside, with a GridView in one of the views.
In my GridView, I have a ButtonField which is supposed to trigger a...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |