VB - button click event not working properly - asp.net

In my windows app i have a button which is only visible when a user selects a certain value on a DropDownList.
For some reason the button does not work when i click on it.
I've enabled the button on page load and it works, however when i choose the value on the Dropdownlist the button does not work.
Is there something im missing here? any feedback would be greatly appreciated. Thanks
Code:
Protected Sub DropDownList4_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DropDownList4.SelectedIndexChanged
If DropDownList4.SelectedValue = "Yes" Then
btnInsert.Visible = True
Endif
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnInsert.Click
Response.Redirect("Menu.aspx")
DropDownList4.SelectedValue = "Yes"
txtfirstName.Text = ""
txtSurname.Text = ""
txtJobTitle.Text = ""
txtCountry.Text = ""
txtWork.Text = ""
DropDownList7.SelectedValue = ""
End Sub

From what we can see from your provided code all you are doing is changing visibility. Are you disabling the button anywhere else? Also Check the properties of the button in the design view to ensure that you did not accidentally changed the Enabled property to "False". It has been a while since I have done any web applications but if you are programically changing the enabled value of the button, I would suggest adding an Enabled = True line to your selected index change subroutine:
If DropDownList4.SelectedValue = "Yes"
Then
btnInsert.Visible = True
btnInsert.Enabled = True
Endif
It has been a while for me so I can't remember if it is .Enabled or something else.
There is also the possibility that you have a Panel or some such over your button and that is preventing you from actually clicking on the button.
Also, put a break point on the first line of your button click event and make sure that you are not getting there as opposed to entering the code and it not running the way you expect.

Try SelectedItem.Value instead, like this
Protected Sub DropDownList4_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DropDownList4.SelectedIndexChanged
If DropDownList4.SelectedItem.Value = "Yes" Then
btnInsert.Visible = True
Endif
End Sub

Related

Setting button invisible with event not working

I have a Visual Basic Project where I have a page(Parent.aspx) with a user control inside(ChildForm.ascx), the user control have a checkboxlist where if the selection is changed I cause a postback and send an event
Protected Sub checkBoxList_checked(ByVal sender As Object, ByVal e As EventArgs) Handles checkBoxList.SelectedIndexChanged
RaiseEvent MyEvent(sender, e)
End Sub
to make a button invisible on the Parent view
Protected Sub ChildForm_MyEvent(ByVal sender As Object, ByVal e As EventArgs) Handles ChildForm.MyEvent
If condition Then
btnSave.Visible = False
Else
btnSave.Visible= True
End If
End Sub
When I debug I see the breakpoint hitting all the lines of code and the condition apply as intended, but the button is not hidden, I dont know why if is setting the correct value it never refreshes the Parent page to show the changes, even if it does cause a postback.
Please help
Check your page load events (load, prerender, prerenderComplete) to make sure you are not setting the visibility property in there. if you are, you may need to use
"If not IsPostBack"

If Statement to display Label or Button in aspx

I am new to aspx so can someone help me with the following small bit of code. I am trying to create an If statment in aspx.net using Vb, but I can't get it to work correctly.
I am trying to ask a questions and the user has to select a yes checkbox or no checkbox and depending on the answer either a label will appear saying "Not permitted" or a button will appear to link to main form.
I am having problems making either the button or label appear when the user selects choice.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Main_FormBtn.Visible = False
Refer_Label.Visible = False
If CheckBoxNo.Checked = True Then
Refer_Label.Visible = True
ElseIf CheckBoxYes.Checked = True Then
Main_FormBtn.Visible = True
End If
End Sub
Any help would be greatly appricated
thanks
If it is YES OR NO Category Better you use RadioButton Not Checkbox..
Try like below it will help you...
YESRadio_CheckedChanged EVENT
Main_FormBtn.Visible = True
Refer_Label.Visible = False
NORadio_CheckedChanged EVENT
Refer_Label.Visible = True
Main_FormBtn.Visible = False
PAGE_LOAD EVENT
Main_FormBtn.Visible = False
Refer_Label.Visible = False
Before that Set AutoPostBack="true" for RadioButtons
If this is meant to be a form where users go through and answer a number of questions and sub-options appear accordingly there's a number of issues you need to consider. If you want the boxes/labels to show hide when a user clicks something you might need to use javascript/jquery unless you do a postback, in which case you might need to consider that a user might already have selected a number of options/filled out some part of the form.
If you want to share the overall structure of the page it will be easier to give proper advice.
Well as this is on page load, why would you even need this IF statement here? I'm thinking that you actually want this statement within the Click event of the checkbox, not on the page load. This code will only run on the load, not the click.
Double click your ASP button and put this code within the event handler there. Point both of the two checkbox click events to the same method like so;
Page load - You only want to set these to false on the first load.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Main_FormBtn.Visible = False
Refer_Label.Visible = False
End If
End Sub
Click event
Private Sub Checkbox_Click(sender As System.Object, e As System.EventArgs) Handles CheckBoxNo.Click, CheckBoxYes.Click
Main_FormBtn.Visible = False
Refer_Label.Visible = False
If CheckBoxNo.Checked = True Then
Refer_Label.Visible = True
ElseIf CheckBoxYes.Checked = True Then
Main_FormBtn.Visible = True
End If
End Sub
For this to work, it will require a postback. Unless you fancy going into Javascript land.

VB.Net ListView Select Item with Button

Note: There was not any question with this kind of problem here or anywhere...
Ok, so I made my listview, and it's delete and edit events are working properly, now I want to implement a possibility for user to mark an element as "default".
D, E and Def are buttons
Reference
----------------------------------------------------------------
- ref1 - somevalue - somevalue - somevalue - [D] - [E] - [Def]
----------------------------------------------------------------
so that would be a row from a table, I made delete and edit work by handling events from listview,
Private Sub lvMain_ItemDelete(ByVal sender As Object, ByVal e As ListViewDeleteEventArgs) Handles lvMain.ItemDeleting
Dim refFac As new ReferenceFactory
refFac.Delete(e.Keys(0))
EndSub
similar for Editing. But now when I try to get values from Default button, the button wont even do anything...
This is the code:
<asp:ImageButton ID="ibtDefault" runat="server" ImageUrl="~/Images/Default16.png" CommandName="Default" />
and for my logic:
Public Sub ibtDefault_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs )
SelectedRef.Name = "Test"
End Sub
I just wanted to test it whether it will run or not by changing the value of my global string that will show which Reference is made default. But it wont even do that...
Then I tried with Commands.
Private Sub lvMain_ItemCommand(ByVal sender As Object, ByVal e As ListViewCommandEventArgs) Handles lvMain.ItemCommand
If e.CommandName = "Default" Then
'Dim refID As New Integer
'Dim refer As ListViewItem
'refer = e.Item
SelectedRef.Name = "Test"
End If
End Sub
But this wont run either... What am I doing wrong here :S
Basically what I want to is that on click i save Reference Name and ID in two global variables i prepared.
Thanks
Default is a reserved word and may be messing with your code. When you run through your ItemCommand event, you can rebind the data at the end of it and it should refresh your page.

session variable set by checkbox not being stored/shared from page to page

I have a listview that's showing a long list. Each item has the ability to be 'hidden' or not. I want a checkbox to either show all the list, or not to show the hidden ones. The idea is that users will hide the older items they don't want to see any more, but may want to see at some point. I want to store the value of this decision in a session variable so if the user navigates to another page, then comes back, the ShowAllCheckbox will pre-populate to what the user has previously decided. Everything is working good, except i can't get the session variable to keep. It keeps going back to False. This is what I have:
aspx page:
Show Hidden: <asp:Checkbox ID="ShowHiddenCheckbox" runat="server" AutoPostBack="True" OnCheckedChanged="ShowHiddenCheckboxChange" />
...
<asp:ListView ...>
<!-- this list works fine, and pulls the correct records -->
aspx.vb page:
Protected Sub ShowHiddenCheckBoxChange(ByVal sender As Object, ByVal e As EventArgs)
' toggle the values
Dim CheckBoxField As CheckBox = TryCast(sender, CheckBox)
If CheckBoxField.Checked Then
Session("ShowHiddenRotations") = False
Else
Session("ShowHiddenRotations") = True
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'if i navigate to another page, and come back to this one, this comes back as "False". I don't understand how it could be reset to False.
Response.Write( "Session: " & Session("ShowHiddenRotations") )
'when page loads check if session variable has been set to show/hide the hidden rotations
If Session("ShowHiddenRotations") Then
If Session("ShowHiddenRotations") = True Then
'update sql query on select statement to show the hidden rotations
'update checkbox to show it as checked
ShowHiddenCheckBox.Checked = True
Else
ShowHiddenCheckBox.Checked = False
End If
Else
'not checked by default (ie don't show hidden)
ShowHiddenCheckBox.Checked = False
End If
End Sub
The Session variable always reverts back to False when i navigate to another page and come back to this one. My understanding of session variables was that they would pass their values from one page to another until the user closes the browser. Maybe there's another way of doing this, or something simple I'm missing. Any help is much appreciated! Thanks!
Is session-state enabled on your site? It can be disabled in a couple of different way, on a page level or even in web.config.
You should also be aware the Page_Load event fires for every request, before the check-box auto-postback happens.
I'm also a little confused as to what you're trying to store: I assume every row has a check-box, but it seems you're trying to store the set/not-set value in a single session variable. How do you differentiate which have been selected, and which ones hasn't? :)
Update:
Okay, let's try a clean the code up a little bit. First create a property to access the session value:
Private Property ShowHiddenRotations As Boolean
Get
If Not Session("ShowHiddenRotations") Is Nothing Then
Return CType(Session("ShowHiddenRotations"), Boolean)
Else
Return False
End If
End Get
Set(value As Boolean)
Session("ShowHiddenRotations") = value
End Set
End Property
If you're using that value on other pages, I would recommend moving it to a seperate class.
Then we can reduce your other code to something closer to this:
Protected Sub ShowHiddenCheckBoxChange(ByVal sender As Object, ByVal e As EventArgs)
ShowHiddenRotations = ShowHiddenCheckbox.Checked
End Sub
And ...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If not Page.IsPostBack
' Load your data, better stick it in a seperate sub...
ShowHiddenCheckBox.Checked = ShowHiddenRotations
else
' This section is executed BEFORE any control methods are run, i.e. ShowHiddenCheckBoxChange
end if
End Sub
I'm guessing your problem is really just the order of how things are called in your page. What happens when you debug through it?

ASP.NET, VB: how to access controls inside a FormView from the code behind?

I have a checkbox and a panel inside of a FormView control, and I need to access them from the code behind in order to use the checkbox to determine whether or not the panel is visible. This is the code that I originally used, but since I put the controls inside of the FormView, it no longer works.
Protected Sub checkGenEd_CheckedChanged(ByVal sender As Object, _
ByVal e As System.EventArgs)
If checkGenEd.Checked = True Then
panelOutcome.Visible = True
Else
panelOutcome.Visible = False
End If
End Sub
I've started to figure this out based on other questions I looked up on here, but all of them were in C# instead of VB, so this is as far as I got:
Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
If FormView1.CurrentMode = FormViewMode.Edit Then
End If
End Sub
So yeah I'm not sure exactly how to finish it. I'm sorry, this might be pretty basic, but I'm new at this and any help would be appreciated!
EDIT: here's my code now:
Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
If FormView1.CurrentMode = FormViewMode.Edit Then
CheckBox checkGenEd = formview1.FindControl("checkGenEd");
Panel panelOutcome = formview1.FindControl("panelOutcome");
End If
End Sub
It's also saying that checkGenEd and panelOutcome are not declared.
EDIT: I changed my code to this but it still doesn't work:
Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
If FormView1.CurrentMode = FormViewMode.Edit Then
Dim checkGenEd As CheckBox = FormView1.FindControl("checkGenEd")
Dim panelOutcome As Panel = FormView1.FindControl("panelOutcome")
If checkGenEd.Checked = True Then
panelOutcome.Visible = True
Else
panelOutcome.Visible = False
End If
End If
End Sub
There aren't any errors anymore, but nothing happens when I click the checkbox. I think there needs to be some kind of event to trigger it but I don't know how you can put an event handler inside of an event handler.
With FormView, you have to use find control, as in:
CheckBox checkGenEd = (CheckBox)formview1.FindControl("checkGenEd");
Panel panelOutcome = (Panel)formview1.FindControl("panelOutcome");
You cannot reference a control directly by ID.
HTH.
In VB you need use Directcast
Dim chk As Checkbox = DirectCast(Me.FormView1.FindControl("checkgen"), Checkbox)
FormView has its own event framework. A normal control within a FormView won't generate the postback events you are looking for. I initially made the same mistake. I wanted, like you, to generate some kind of postback that could be intercepted at the server end. Once we get back to the server we can look at the values in checkboxes etc depending on whatever business rules apply. This is what I did.
First of all put all relevant controls within an
<EditItemTemplate>
section within the FormView. (There are other Template tags that may be more appropriate). To generate the postback have a button (for example) like the one below. (This has to be within the EditItemTemplate section as well):
<asp:linkbutton id="UpdateButton"
text="Update"
commandname="Update"
runat="server"/>
You can intercept this at the server with the FormView event ItemCommand. For example:
Private Sub FormView1_ItemCommand(sender As Object, e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FormView1.ItemCommand
'your code here
End Sub
Once you are back at the server you can then start looking at the various controls to see what they hold, using findControl if necessary. The button command shown above is an example so you might want to use another control.

Resources