Ferox
asked on
Problem with 2 level navigation on postback - need to click link twice
I'm currently developing a dynamic site in asp.net (vb) and it's going great apart from one problem.
I have a 2 tier navigation (categories > sub_cats) fed from a database. Everytime the page loads a tabbed navigation is created of top-level categories.
Clicking one of these (asp:linkbuttons) raises an event to build up a side navigation of subcategories (also asp:linkbuttons)
This is where the problem arises. I have the second-level sub navigation appearing but when I click one of the linkbuttons (which should raise an event to display the related data) I have to click it twice to get the data to display.
I know when a user clicks a linkbutton the even it a postback. But Why do I need to click it twice to get the event to trigger. The page does postback on the first click but the data doesn't display, not until the second click.
Any help with my logic of this would be great as I'm a bit confused to why I would need 2 clicks to raise the event.
Here's a simple version of my main.vb page with all my database stuff taken out.
This has the same problems. it must be my logic. I need the get_CategoryCount() to run each time as it makes up the data for the tabs and side nav each time.
On my aspx page that inherits this there is 3 asp:table controls: tblTabs (tabbed nav), tblInnerList (side nav) and tblItems (holds the final click data)
Again any help would be great! I'm starting to go nuts! :)
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------
Public Class StartCat
Inherits System.Web.UI.Page
Protected mnuMain as dropdownlist
Protected mnuSecond as dropdownlist
Protected tblInnerList as table
Protected tblItems as table
Protected tblTabs as table
Protected lblTester as label
Protected lblTabTest as label
Sub Page_Load(Sender As Object, E As EventArgs)
get_CategoryCount()
response.write ("Postback Occured<br>" & viewstate("iCatCount") & "<br>")
If Not Page.IsPostBack Then
end if
end sub
Sub get_CategoryCount()
viewstate("iCatCount") = 2
build_Tabs()
build_SideNav()
End Sub
Sub build_Tabs()
Dim tblTabsRow As New TableRow()
Dim tblTabsCell As New TableCell()
Dim iCount as Integer
tblTabsRow = New TableRow()
for iCount = 0 to viewstate("iCatCount")
dim lbLink as new LinkButton
lbLink.Text = "Tab_" & iCount
lbLink.CommandName = "ID"
lbLink.CommandArgument = iCount
AddHandler lbLink.Command, AddressOf lbLink_Click
tblTabsCell = new TableCell()
tblTabsCell.Controls.Add(l bLink)
tblTabsCell.width = new unit(100)
tblTabsCell.CssClass = ""
tblTabsCell.HorizontalAlig n = 2
tblTabsRow.Cells.Add(tblTa bsCell)
next
tblTabs.Rows.Add(tblTabsRo w)
End Sub
Sub build_SideNav()
'----Builds up the side nav------
Dim tblRow As New TableRow()
Dim tblCell As New TableCell()
tblInnerList.Rows.Clear()
dim lblSideLink as new LinkButton
lblSideLink.Text = "Side Button"
lblSideLink.CommandName = "sideID"
lblSideLink.CommandArgumen t = 1
AddHandler lblSideLink.Command, AddressOf lblSideLink_Click
tblRow = New TableRow()
tblCell = new TableCell()
tblCell.Controls.Add(lblSi deLink)
tblCell.width = new unit(100)
tblCell.CssClass = ""
tblRow.Cells.Add(tblCell)
tblInnerList.Rows.Add(tblR ow)
End Sub
Sub lbLink_Click(Sender As Object, e As CommandEventArgs)
viewstate("iCurrentCat") = sender.CommandArgument
response.write ("Current Tab: " & viewstate("iCurrentCat"))
build_SideNav()
End Sub
Sub lblSideLink_Click(Sender As Object, e As CommandEventArgs)
viewstate("iCurrentElement ") = sender.CommandArgument
response.write ("Current Tab: " & viewstate("iCurrentCat") & "<br>")
response.write ("Current Side nav: " & viewstate("iCurrentElement ") & "<br>")
writeMainTable()
End Sub
Sub writeMainTable()
'----Builds up the main items table header------
Dim tblItemsRow As New TableRow()
Dim tblItemsCell As New TableCell()
tblItemsRow = New TableRow()
tblItemsCell = new TableCell()
tblItemsCell.Controls.Add( New LiteralControl("Data"))
tblItemsCell.width = new unit(110)
tblItemsCell.CssClass = ""
tblItemsRow.Cells.Add(tblI temsCell)
tblItems.Rows.Add(tblItems Row)
End Sub
End Class
I have a 2 tier navigation (categories > sub_cats) fed from a database. Everytime the page loads a tabbed navigation is created of top-level categories.
Clicking one of these (asp:linkbuttons) raises an event to build up a side navigation of subcategories (also asp:linkbuttons)
This is where the problem arises. I have the second-level sub navigation appearing but when I click one of the linkbuttons (which should raise an event to display the related data) I have to click it twice to get the data to display.
I know when a user clicks a linkbutton the even it a postback. But Why do I need to click it twice to get the event to trigger. The page does postback on the first click but the data doesn't display, not until the second click.
Any help with my logic of this would be great as I'm a bit confused to why I would need 2 clicks to raise the event.
Here's a simple version of my main.vb page with all my database stuff taken out.
This has the same problems. it must be my logic. I need the get_CategoryCount() to run each time as it makes up the data for the tabs and side nav each time.
On my aspx page that inherits this there is 3 asp:table controls: tblTabs (tabbed nav), tblInnerList (side nav) and tblItems (holds the final click data)
Again any help would be great! I'm starting to go nuts! :)
--------------------------
Public Class StartCat
Inherits System.Web.UI.Page
Protected mnuMain as dropdownlist
Protected mnuSecond as dropdownlist
Protected tblInnerList as table
Protected tblItems as table
Protected tblTabs as table
Protected lblTester as label
Protected lblTabTest as label
Sub Page_Load(Sender As Object, E As EventArgs)
get_CategoryCount()
response.write ("Postback Occured<br>" & viewstate("iCatCount") & "<br>")
If Not Page.IsPostBack Then
end if
end sub
Sub get_CategoryCount()
viewstate("iCatCount") = 2
build_Tabs()
build_SideNav()
End Sub
Sub build_Tabs()
Dim tblTabsRow As New TableRow()
Dim tblTabsCell As New TableCell()
Dim iCount as Integer
tblTabsRow = New TableRow()
for iCount = 0 to viewstate("iCatCount")
dim lbLink as new LinkButton
lbLink.Text = "Tab_" & iCount
lbLink.CommandName = "ID"
lbLink.CommandArgument = iCount
AddHandler lbLink.Command, AddressOf lbLink_Click
tblTabsCell = new TableCell()
tblTabsCell.Controls.Add(l
tblTabsCell.width = new unit(100)
tblTabsCell.CssClass = ""
tblTabsCell.HorizontalAlig
tblTabsRow.Cells.Add(tblTa
next
tblTabs.Rows.Add(tblTabsRo
End Sub
Sub build_SideNav()
'----Builds up the side nav------
Dim tblRow As New TableRow()
Dim tblCell As New TableCell()
tblInnerList.Rows.Clear()
dim lblSideLink as new LinkButton
lblSideLink.Text = "Side Button"
lblSideLink.CommandName = "sideID"
lblSideLink.CommandArgumen
AddHandler lblSideLink.Command, AddressOf lblSideLink_Click
tblRow = New TableRow()
tblCell = new TableCell()
tblCell.Controls.Add(lblSi
tblCell.width = new unit(100)
tblCell.CssClass = ""
tblRow.Cells.Add(tblCell)
tblInnerList.Rows.Add(tblR
End Sub
Sub lbLink_Click(Sender As Object, e As CommandEventArgs)
viewstate("iCurrentCat") = sender.CommandArgument
response.write ("Current Tab: " & viewstate("iCurrentCat"))
build_SideNav()
End Sub
Sub lblSideLink_Click(Sender As Object, e As CommandEventArgs)
viewstate("iCurrentElement
response.write ("Current Tab: " & viewstate("iCurrentCat") & "<br>")
response.write ("Current Side nav: " & viewstate("iCurrentElement
writeMainTable()
End Sub
Sub writeMainTable()
'----Builds up the main items table header------
Dim tblItemsRow As New TableRow()
Dim tblItemsCell As New TableCell()
tblItemsRow = New TableRow()
tblItemsCell = new TableCell()
tblItemsCell.Controls.Add(
tblItemsCell.width = new unit(110)
tblItemsCell.CssClass = ""
tblItemsRow.Cells.Add(tblI
tblItems.Rows.Add(tblItems
End Sub
End Class
oops posted in the wrong place
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks so much Edwin! Thats solved my problem perfectly - exactly what I needed. I was going spare trying to solve the problem. Thanks also for the explanation!
Thank you, Edwin!! I had the same problem.
Convert the delete button to a template column and give it an id
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton runat="server" Text="Delete" CommandName="Delete" CausesValidation="false" ID="Delete"></asp:LinkButt
</ItemTemplate>
</asp:TemplateColumn>
Private Sub DataGrid1_ItemDataBound(By
If e.Item.ItemType = ListItemType.Item Then
'Put your condition here and check if the current row has any child rows in the relation or not. If they have then disable that particular link button
If (Convert.ToInt64(DateTime.
CType(e.Item.FindControl("
End If
End If
End Sub
Enjoy