How to check if button was click in page load - asp.net

I have a button I don't want causing this if command in my page load.
If Page.IsPostBack Then
ViewState(2) = TreeView1.SelectedNode.ValuePath
ViewState(5) = TextBox1.Text
''Where I would like to put another if command or whatever else is possible to check if the buttong was clicked''
If ViewState(2) = ViewState(1) And ViewState(5) = ViewState(4) Then
nodecount = ViewState(3)
nodecount = nodecount + 1
ViewState(3) = nodecount
If nodecount > 0 Then
MsgBox("Please select another option or different number of data points")
End If
Else
nodecount = 0
ViewState(3) = nodecount
End If
End If
I tried setting a public property to change if the button was clicked but page.ispostback gets called first then the sub does.

I'm not sure if I fully understand your question. If you are trying to run a block of code, why not do it at the btnSomeButton.Click event, why do it at the Page_Load event.

Related

Check with adding / removing objects in VB6

I have a query, I have this interface:
The ComboBox are inside a UserControl, pressing the Add this UserControl button with the ComboBox adds the UserControl / ComboBox in a PictureBox:
What I want is that, for example, when the user selects values ​​of the two ComboBox and press the button add these selected values ​​go to the PictureBox (below) and the values ​​that are selected in the UserControl are cleaned.I leave a picture of what I say, suppose the user selected the values ​​of the combos:
Once you have selected them, the add these button is enabled, they pass below and those above are cleaned:
But, if I press the remove button you must remove the last loaded object and move to the top. I leave a descriptive image, press the remove button:
The bottom disappears and goes up:
Currently this is the code I use to add:
Option Explicit
Dim indice As Integer
Public Property Let AddType(ByVal Value As String)
cmbAddExample.Text = Value
End Property
Private Sub btnAñadir_Click()
indice = indice + 1
Picture1.Visible = True
Load uc1(indice)
Set uc1(indice).Container = Picture1
uc1(indice).Visible = True
uc1(indice).Top = IIf(indice = 1, 0, uc1(indice - 1).Top + uc1(indice - 1).Height + 20)
Load cmbAddExample(indice)
Set cmbAddExample(indice).Container = uc1(indice)
cmbAddExample(indice).Visible = True
cmbAddExample(indice).Top = cmbAddExample(indice - 1).Top
CargaIDTipoNumero
uc1(indice).AddType = uc1(0).AddType
uc1(indice).AddType = ""
If indice = 3 Then
Me.btnAñadir.Enabled = False
End If
End Sub
So, does anyone have any idea how I can do what I am looking for? Or yes, is it possible?
Button Remove:
Private Sub btnQuitar_Click()
indice = cmbAddExample().Count - 1
If indice > 0 Then
Unload cmbAddExample(indice)
End If
End Sub
Using this answer as the starting point, you can implement these requirements fairly easily. I have copied relevant code from the other answer and modified it:
Private Sub btnAdd_Click()
index = index + 1
Load uc1(index)
Set uc1(index).Container = Picture1
uc1(index).Visible = True
uc1(index).Top = IIf(index = 1, 0, uc1(index - 1).Top + uc1(index - 1).Height + 20)
'copy the information down
uc1(index).AddType = uc1(0).AddType
uc1(0).AddType = ""
Picture1.Visible = True
End Sub
Private Sub btnRemove_Click()
If index = 0 Then Exit Sub
'copy the information back up and remove the control
uc1(0).AddType = uc1(index).AddType
Unload uc1(index)
index = index - 1
If index = 0 Then Picture1.Visible = False
End Sub
I have only coded for one control to illustrate the concept. You can add other controls as needed.

Checkbox for the VB6 DataGrid

How to add Checkbox for Vb6 data grid like as image.
For an MSFlexGrid control, I use a checked and unchecked image in the column.
You add two PictureBoxcontrols, one for each image, and make the Visible property false. When loading the data for your grid you can set each pic according to whether it needs to be checked or not:
With MSFlexGrid1
.Col = 1
If myCol1Bool Then
Set .CellPicture = picChecked.Picture
Else
Set .CellPicture = picUnChecked.Picture
End If
.Col = 2
If myCol2Bool Then
Set .CellPicture = picChecked.Picture
Else
Set .CellPicture = picUnChecked.Picture
End If
End With
You can toggle the check state on click if it's editable:
Private Sub MSFlexGrid1_Click()
If (MSFlexGrid1.Col <> 1 And MSFlexGrid1.Col <> 2) Or MSFlexGrid1.Row < 1 Then Exit Sub
If MSFlexGrid1.CellPicture = picChecked Then
Set MSFlexGrid1.CellPicture = picUnchecked
Else
Set MSFlexGrid1.CellPicture = picChecked
End If
End Sub
For a full example, check out vb-helper.com.

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.

Make current selection not selectable again with treenodes

I have a menu as a treeview and what I'm trying to do is make whatever treenode the user selects not selectable again unless he goes to a different page. How would I do this in asp.net and vb?
update:
ok so far this is what I have going on in the treenode selection changed sub
ViewState(10) = TreeView1.SelectedNode
TreeView1.SelectedNode.SelectAction = TreeNodeSelectAction.None
If ViewState(10).valuepath <> TreeView1.SelectedNode.ValuePath Then
TreeView1.SelectedNode.SelectAction = TreeNodeSelectAction.Select
End If
im sure the experts out there know exactly why this wouldn't work though I was surprised it complied. Anyways This gives me an error though I can't think of another way to keep that selected node in
UPDATE:
ViewState(10) = TreeView1.SelectedNode.DataItem
If ViewState(10) <> TreeView1.SelectedNode.DataItem Then
TreeView1.SelectedNode.Selected = ViewState(10)
TreeView1.SelectedNode.SelectAction = TreeNodeSelectAction.Select
Else
TreeView1.SelectedNode.SelectAction = TreeNodeSelectAction.None
End If
This works for the selectaction.none which is exactly what I wanted but I'm having troubles enabling it again when the user selects another treenode.
This is the answer for anyone who needs this in the future
For Each n As TreeNode In TreeView1.Nodes
For Each a As TreeNode In n.ChildNodes
If a.Selected = True Then
a.SelectAction = TreeNodeSelectAction.None
Else
a.SelectAction = TreeNodeSelectAction.Select
End If
Next
Next

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