Link to home
Start Free TrialLog in
Avatar of Ferox
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(lbLink)
tblTabsCell.width = new unit(100)
tblTabsCell.CssClass = "" 
tblTabsCell.HorizontalAlign = 2
tblTabsRow.Cells.Add(tblTabsCell)
next

tblTabs.Rows.Add(tblTabsRow)

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.CommandArgument = 1
AddHandler lblSideLink.Command, AddressOf lblSideLink_Click

tblRow = New TableRow()
tblCell = new TableCell()
tblCell.Controls.Add(lblSideLink)
tblCell.width = new unit(100)
tblCell.CssClass = "" 
tblRow.Cells.Add(tblCell)
tblInnerList.Rows.Add(tblRow)

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(tblItemsCell)
tblItems.Rows.Add(tblItemsRow)

End Sub

End Class
Avatar of platinum505
platinum505

You can use the itemdatabound event to disable the delete buttons. see the sample code below

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:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>




    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        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.Now.Ticks) Mod 2) = 0 Then
                CType(e.Item.FindControl("Delete"), LinkButton).Enabled = False
            End If
        End If
    End Sub


Enjoy


oops posted in the wrong place
ASKER CERTIFIED SOLUTION
Avatar of edwinugomez
edwinugomez

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ferox

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.