Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Hey all I'm new here.

 

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.

 

edit:

 

Just a thought is there a way I could check on the postback if the button has been pressed, and if so raise the event that way? I really don't know why it's not triggering the onlick event on the first click.

 

e.g in pseudocode

 

Sub Page_Load(Sender As Object, E As EventArgs)

 

if "subcatagory_button" was pressed then

raise the onclick event

end if

 

End sub

Edited by Ferox
  • Administrators
Posted

you could check if the page load is being called as a result of a PostBack to prevent it firing the code every time, and put the button click code itno the relevant event handler

 

Sub Page_Load(Sender As Object, E As EventArgs)

if Page.IsPostBack = false then
   'non-button click code goes here
end if

End sub

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Yes I know the page_load procedure is being called as the flow of a linkbutton goes:

 

actual click > page_load > linkbutton_onlickevent

 

my application flow is:

 

Page_Load
    buildTopTabs
            <<   *user clicks on tab* < causes postback
    buildSideNav
            <<   *user clicks on side menu item*  < causes postback
    showRelatedData

 

The building of the tabs etc needs to be done as part of the pageload & postback.

 

This is whats happening:

 

Page_Load
    buildTopTabs
            <<   *I click on tab* < causes postback
    buildSideNav
            <<   *I click on an item* < causes postback
            ' nothing happens except pageload, tab and nav build
            <<   *2nd click on an item* < causes postback
    showRelatedData

Posted

Right 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!

 

---------------------------------------------------------------------------------------------------------

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

Posted

Just to let you know I got the solutions for this. I simply needed to assign ID's for my sidenavigation controls to stop auto-assign on page postback.

 

lblSideLink.ID = "ID_" & someUniqueValue

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...