I have a Hierarchical Table for Categories in this form NodeID ParentID NodeName
Following is the code i am using generate ASP.NET MENU ITEMS and bind it to MENU CONTROL
Private Sub displayMenu()
Using con As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("HagglerEntLibConStr").ConnectionString)
Using sql As New SqlCommand("Select NodeID,ParentID,NodeName FROM Nodes", con)
con.Open()
Dim r As SqlDataReader = sql.ExecuteReader 'stored procedure that returns the above SqlDataReader
Dim tempMaster As New MenuItem()
Dim bagMaster(700) As MenuItem
Dim j As Integer = 0
Do While r.Read()
tempMaster.Value = r("NodeID").ToString()
tempMaster.ToolTip = r("ParentID").ToString()
tempMaster.Text = r("NodeName").ToString()
tempMaster.NavigateUrl = ""
bagMaster(j) = tempMaster
j += 1
tempMaster = New MenuItem()
Loop
r.Close()
Menu1.Items.Clear()
For i As Integer = 0 To j - 1
If i = 0 Then
Menu1.Items.Add(bagMaster(i))
Else
For x As Integer = 0 To j - 1
If bagMaster(i).ToolTip = bagMaster(x).Value Then
Dim c As MenuItem = bagMaster(x)
'you can change the tool tip here if you want
c.ChildItems.Add(bagMaster(i))
End If
Next x
End If
Next i
'Menu1.Items.RemoveAt(0)
End Using
End Using
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
displayMenu()
End Sub
Everything works fine ...but the main disadvantage with this approach is that the Menu CONTROL creates HTML TABLES...i have around 1000 rows in the table..this creates a multi level menu ...which adds a lot of HTML Tables in the page and increases the page load time drastically...how do i tackle this issue?? This is the way the menu populates.
Have you tried with the RenderingMode property?
Values:
Default. (This is the default value). The rendering mode depends on the RenderingCompatibility control property, if this setting is set to 3.5, the default rendering is a table, if this value is 4.0, the default rendering mode is a list ul. You can change this setting globally in the web.config file:
<system.web>
<pages controlRenderingCompatibilityVersion="4.0"/>
</system.web>
Table. Renders as a table
List. Renders as a list
Update 1
Since you are using the .Net Framework 3.5, probably the best way to accomplish this is to use Control Adapters
Basically the adapters work on a specific control, and in the adapter you can update the rendering of the target control, in this case the Menu control
I have not created a control adapter, so I cannot give you further information other than pointing you in the right direction, googling I found this article which seems promising:
http://msdn.microsoft.com/en-us/magazine/cc163543.aspx
Related
Hello i got a drop down list with post back from worker data base .
I also got a grid view with that data base.
when i choose a worker on the drop down i got 5 text boxes that are being filled from the right row on the grid view.
My question is how can i do that without using the auto post back?
p.s I am using visual studio with vb.net
Thanks in advance!
atm i am using something like this but i am really trying to remove all not needed postbacks from my site.
Protected Sub workerDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles workerDropDownList.SelectedIndexChanged
Dim x As Integer
For x = 0 To workerDataGrid.Rows.Count - 1
If workerDataGrid.Rows.Item(x).Cells(1).Text = workerDropDownList.SelectedValue Then
editWorkerNameBox.Text = workerDataGrid.Rows.Item(x).Cells(0).Text
editWorkerIdBox.Text = workerDataGrid.Rows.Item(x).Cells(1).Text
editPhoneBox.Text = workerDataGrid.Rows.Item(x).Cells(2).Text
editCellPhoneBox.Text = workerDataGrid.Rows.Item(x).Cells(3).Text
editEmailBox.Text = workerDataGrid.Rows.Item(x).Cells(4).Text
editUserNameBox.Text = workerDataGrid.Rows.Item(x).Cells(5).Text
editUserPassBox.Text = workerDataGrid.Rows.Item(x).Cells(6).Text
editIsManagerBox.Text = workerDataGrid.Rows.Item(x).Cells(8).Text
editIsActiveBox.Text = workerDataGrid.Rows.Item(x).Cells(7).Text
End If
Next
End Sub
I'm using VB ASP.NET and would like to know how to add code to a control that is created by code behind?
For example in my Main.aspx.vb file I have the following code:
Connection.Open()
spRetrieveAlbums.ExecuteNonQuery()
ReturnValue = spRetrieveAlbums.Parameters("#ReturnValue").Value
Connection.Close()
For I = 1 To ReturnValue
Dim myAlbum = New ImageButton
myAlbum.Visible = True
myAlbum.Width = 150
myAlbum.Height = 150
myAlbum.BorderStyle = BorderStyle.Solid
myAlbum.BorderColor = Drawing.Color.WhiteSmoke
myAlbum.BorderWidth = 1
AlbumsPanel.Controls.Add(myAlbum)
Next I
ReturnValue stores the number of albums a person has (Using SQL Server stored procedure ##ROWCOUNT) and displays the same amount of ImageButtons within the 'AlbumsPanel' Panel on the web page.
I would like to use response.redirect("Albums.aspx") on a click event on any of the ImageButtons but not sure how I can achieve this. Any suggestions?
you have created dynamically and you can use a delegate for min.VB10 and like that,
AddHandler myAlbum.Click, _
Sub(sender As Object, e As EventArgs)
//To Do for Response
End Sub
Trying to use checkbox lists in (calendar booking system). The checkbox should be disabled and red if there are any data in the database against the date and hour. This all work perfectly here is the code. Using vb.net
OK i found a way how to clear the checkboxes
Dim i As Integer
Dim chboxItem As ListItem
For Each chboxItem In CheckBoxListMon.Items
i += 1
If (i Mod 1 = 0) Then
chboxItem.Enabled = True
End If
Next
Protected Sub Page_LoadComplete(sender As Object, e As EventArgs) Handles Me.LoadComplete
Try
strQuery = "SELECT BookingDate, checkBoxItem, BookRegUserID,Booked FROM bookings INNER JOIN checkboxitems where checkBoxItem = BookingTime"
MySQLCmd = New MySqlCommand(strQuery, dbCon)
dbCon.Open()
DR = MySQLCmd.ExecuteReader
While DR.Read
bookDate = DR.Item("BookingDate")
bookTime = DR.Item("checkBoxItem")
bookRegID = DR.Item("BookRegUserID")
booked = DR.Item("Booked")
Dim test As String = bookTime.ToString()
Select Case True
Case bookDate = lblMonday.Text And CheckBoxListMon.Items.FindByValue(test) IsNot Nothing
CheckBoxListMon.Items.FindByValue(bookTime).Enabled = False
CheckBoxListMon.Items.FindByValue(bookTime).Attributes.Add("Style", "color: red;")
End Select
End While
DR.Close()
dbCon.Close()
Catch ex As Exception
End Try
End Sub
When the page load it would not change the ones from the database. But when i reload the page it will actually work perfect.
Where can i put the check just to be sure that they are already in the memory.
Any help will be much appreciated. Thanks all.
Petr
You need to refresh the data when another week is selected because you are using the same controls for each week. You should be able to do this in whatever control you are using to toggle through the weeks.
Page_LoadComplete can only be expedted to fire each time a page has completed loading, that is why your controls work when going to another page and back.
I have created an VB.NET page to record views for ads and will call page from img src.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim insert_number As Integer = 0
Dim ad_id As Integer = 0
If Request.QueryString("adid") Is Nothing Then
ad_id = 0
Else
If Not Integer.TryParse(Request.QueryString("adid"), ad_id) Then
ad_id = 0
End If
End If
Dim connectStr As String = System.Configuration.ConfigurationManager.AppSettings("connectStr").ToString()
Dim myconnection As SqlConnection = New SqlConnection(connectStr)
Dim mySqlCommand As SqlCommand
myconnection.Open()
Try
mySqlCommand = New SqlCommand("sp_record", myconnection)
mySqlCommand.CommandType = CommandType.StoredProcedure
mySqlCommand.Parameters.AddWithValue("#record_id", ad_id)
insert_number = mySqlCommand.ExecuteNonQuery()
Catch ex As Exception
End Try
myconnection.Close()
Dim oBitmap As Bitmap = New Bitmap(1, 1)
Dim oGraphic As Graphics = Graphics.FromImage(oBitmap)
oGraphic.DrawLine(New Pen(Color.Red), 0, 0, 1, 1)
'Response.Clear()
Response.ContentType = "image/gif"
oBitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif)
'oBitmap.Dispose()
'oGraphic.Dispose()
End Sub
Unless I comment oBitmap.Save line, the code runs twice and it makes two inserts (store prcoedure runs twice) to Database.
I have tried AutoEventWireup = "true" and "false" at #PAGE. "true" runs code twice, "false" did not do anything (no error) and did not give any output as well.
I have also tried following version of creating 1pixel image output but it did run twice as well (it requires aspcompat=true in #PAGE part):
'Response.ContentType = "image/gif"
'Dim objStream As Object
'objStream = Server.CreateObject("ADODB.Stream")
'objStream.open()
'objStream.type = 1
'objStream.loadfromfile("c:\1pixel.gif")
'Response.BinaryWrite(objStream.read)
Any ideas are welcome.
You may want to do an onload function for the image to see why it's being called a second time. I'm guessing that it's getting loaded somewhere in the preload and then being called (.Save) during the page load as well and that's why you're seeing the double entry.
If you are trying to get unique page loads, you may want to try putting the oBitmap.Save line within a check for postback like this within the page load:
If Page.IsPostback = False Then
'Bitmap Code Here
End If
And see if that fixes it for you.
If you're loading data from a database, you'll want to make sure that it also is within that PostBack check (because a. you're loading the data twice and b. it can cause these double postbacks in some circumstances).
Edit: Wanted to edit code section to include all bitmap code, not just the save.
Not sure about the specifics, but that is a lot of code within in Page_Load function.
Generally, the way I would solve this type of problem is to have some sort of page arguments that you can check for in order to do the correct things. Either add some get/post parameters to the call that you can check for or check things like the Page.IsPostBack.
I realize this is an old post but I had an similar issue where the page was firing twice on the postback. I found several posts sugesting what is being dicusses here. However, what corrected my issue was setting the page directive property AutoEventWireup=false at the page level.
Here is a good article How to use the AutoEventWireup attribute in an ASP.NET Web Form by using Visual C# .NET that helped me solve this.
Hope this helps!
Risho
I inherited a site which I am in the middle of updating which has a DropDownList and a RangeValidator assigned to it. The data is bound to the dropdown on Page_Load. The values are retrieved from the database so the value property of each item is set to the unique ID of the item in the DB.
The RangeValidator looks something like:
<asp:rangevalidator id="ddRangeValidator" runat="server" ControlToValidate="ddMenu" ErrorMessage="Please select value in range" MinimumValue="1" MaximumValue="100000" Type="Integer">*</asp:rangevalidator>
I have a method which automatically populates this value in jQuery e.g.
$("#ddMenu").val("An Option");
This works, however, when I try to post the page the range validation fails. Then even if I manually select that value, or select another valid value it still won't validate. The only way to make it validate is to select non-valid value and then re-selecting a valid one.
Any ideas?
Update
Here is the data binding code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
If Not Page.IsPostBack Then
Dim ds As New DataSet()
Dim myDbObject As New myDbObject()
ds = myDbObject.ToDataSet() // retrieves all objects from the database
// store the results in a temporary view to filter
Dim dv As DataView
dv = New DataView(ds.Tables(0), "IsLive = True", "ID", DataViewRowState.CurrentRows)
Dim i As Integer = 0
ddMenu.Items.Add("Select a value")
ddMenu.Items.Item(0).Value = 0
// add all objects from filtered list into drop down menu
For i = 0 To dv.Count - 1
With ddMenu.Items
// add a new item
.Add(dv.Item(i).Item("Name"))
// set the Value property as unique ID of object
.Item(i + 1).Value = dv.Item(i).Item("ID")
End With
Next
End If
End If
Turns out it was an issue with my page loading twice...so now the issue is going to be tracking down why it is loading twice!
I guess the Range validator verifies the actual "value" of the dropdown list. May it be possible that by using jQuery to populate your dropdown the actual entry doesn't get a proper option "value"? Could you maybe post the rendered HTML code of your dropdown list?