find dynamic added panel - asp.net

i'm adding a panel like this to my web application:
For i = 1 To amountOfRechnungspositionen + 1
Dim pnlPositon As Panel = New Panel
pnlPositon.ID = "pnlPositon" & i
pnlRechungsposition.Controls.Add(pnlPositon)
...
Next
Now if i onclick a button, i want to delete this panel.
This is my code.
Private Sub imgDelRechnungsPosition_Click(sender As Object, e As ImageClickEventArgs) Handles imgDelRechnungsPosition.Click
amountOfRechnungspositionen = Convert.ToInt32(hfAmountofRechnungspositionen.Value)
Dim fcpnlPositon As Panel = DirectCast(pnlRechungsposition.FindControl("placeholderID$pnlPositon" + amountOfRechnungspositionen.ToString()), Panel)
fcpnlPositon.Dispose()
hfAmountofRechnungspositionen.Value = Convert.ToString(amountOfRechnungspositionen - 1)
End Sub
But fcpnlPositon is Nothing.
Waht i'm doing wrong?
Thank u, for reading.

Try using:
CType(pnlRechungsposition.FindControl("pnlPositon" +
amountOfRechnungspositionen.ToString()), Panel)
The placeholder text is only for client-side markup and thus you want to use the direct ID property.

Related

VB / ASP page does not add first item to list in click method

I have written a basic shopping page. When a user clicks an item and valid quantity, they add the item to the cart with the add item button. This should grab the session variable which holds the list, add the new item, and then put the updated list back into the session.
EDIT:
It seems the items ARE being added but not shown until the next page reload. Why?
For the most part it works, except the initial click, for which the count of items remains 0 unless I click add again. From there everything works fine.
I am sure I am overlooking something simple.
In the pages Load Method, I create the new list if no session exists., otherwise using the existing list:
If IsNothing(Session("CustomerItems")) Then
CustomerItemsList = New List(Of CustomerProduct)
'Session("CustomerItems") = CustomerItemsList
Response.Write("Session did not exist yet.")
Else
CustomerItemsList = Session("CustomerItems")
Response.Write("Session Loaded.")
End If
Next the values are added to a list box control from an array to show the items.
Finally I placed these lines. I will use the .IsPostBack page to list all items in the list later on:
If Page.IsPostBack Then
Response.Write("<br>Page has been posted back. There Are Currently " & CustomerItemsList.Count & " items.")
Else
Response.Write("<br>Page has NOT been posted back. There Are Currently " & CustomerItemsList.Count & " items.")
End If
And finally, my click event looks like this:
Protected Sub btnAddItem_Click(sender As Object, e As EventArgs) Handles btnAddItem.Click
CustomerItemsList = Session("CustomerItems")
Dim CustomerItem As CustomerProduct
CustomerItem.Description = ProductArray(lstItemDescriptions.SelectedIndex, 0)
CustomerItem.ItemQty = txtItemQty.Text
CustomerItem.BulkFee = ProductArray(lstItemDescriptions.SelectedIndex, 1)
CustomerItem.UnitWeight = ProductArray(lstItemDescriptions.SelectedIndex, 2)
CustomerItem.TotalWeight = Double.Parse(ProductArray(lstItemDescriptions.SelectedIndex, 2)) * Double.Parse(txtItemQty.Text)
CustomerItem.UnitCost = ProductArray(lstItemDescriptions.SelectedIndex, 3)
CustomerItem.TotalCost = CustomerItem.UnitCost * CustomerItem.ItemQty
CustomerItemsList.Add(CustomerItem)
Session("CustomerItems") = CustomerItemsList
End Sub
Now, I've gone over this thing a few times, and its probably simple, but it just doesn't make any sense.

using dropdownlist without postback for filling textboxes

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

VB ASP.NET - Add code to a control that is created by code behind

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

convert querystring to panel control

I have a bunch panels on a page. panel1, panle2 ..., I want to make a panel visible based on a querystring. Ived tried:
Dim s As String
s = Request.QueryString("s")
Dim p As Panel = CType(Me.Controls(s), Panel)
p.Visible = True
This doesn't work. Maybe there is a whole different way to go about it.
Thanks.
If you're passing actual Panel ID (e.g. http://mysite.com/page.aspx?s=panel1), you should use 'FindControl' method:
Dim p As Panel = CType(Me.FindControl(s), Panel)

Checkbox list keep disabled

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.

Resources