Button.Enabled and checkbox.Checked status erased when redirecting on event ASP.NET VB - asp.net

In my register page I have a checkbox (chkAgree) next to the terms of agreement. The register (btnRegister) button is disabled until chkAgree.Checked = True. Since the registration section is toward the bottom of the page I want to redirect the user to an anchor tag during the event. I can't seem to accomplish this without one of the two objects in question losing their status. I tried creating a Session for each object and then calling them on the page load event with no luck. I am obviously doing it wrong, but could someone please point me in the right direction? btnRegister's default status is set to Enabled=False. Here are the two methods I am using
CheckedChanged:
Protected Sub chkAgree_CheckedChanged(sender As Object, e As EventArgs) Handles chkAgree.CheckedChanged
Session("Agree") = chkAgree.Checked
Response.Redirect("~/ExteriorStudentTestimonials.aspx#register", False)
End Sub
And Page_Load:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Session("Agree") = True Then
btnRegister.Enabled = True
chkAgree.Checked = True
Else
btnRegister.Enabled = False
chkAgree.Checked = False
End If
End Sub
I have tried several different combinations of If statements but cannot figure it out without taking Response.Redirect("~/ExteriorStudentTestimonials.aspx#register", False) out.

This might be related to the fact that you're reloading the page. You might want to take a look at the following question for some ideas of how to jump to an anchor without a full redirect: Programmatically scroll to an Anchor Tag
On a different note, jumping to an anchor when the user clicks a check box doesn't sound like a very good user experience. You might consider making your form shorter.

Got it using this method similar to the ones described in the link you provided
Page.ClientScript.RegisterStartupScript(Page.GetType(),
"navigate",
"document.getElementById('register').scrollIntoView();",
True)

Related

BackgroundWorker task blocks UI thread in my web application

Okay, I've been banging on this problem for close to 10 work hours now with no real progress to speak of. After going through multiple "solutions" offered on the web, I find myself still unable to accomplish what should be a really simple task -- pop up a modal dialog from my web application while I complete a long process I'm forcing the user to wait upon.
I've ripped out all the non-multi-threading-control aspects and am left with this bare bones structure and it still doesn't cut it:
In my ASPX file, I have two "please wait" displays, one is a DIV set up for use with bPopup, the other a DIV runat=server following the examples on BackgroundWorker. The bPopup dialog works great when used independently of any background work. But, when the background worker fires, the UI freezes until it completes, then both opens & closes the bPopup dialog as fast as it can, basically flashing the screen.
This is my code behind: I've cut it to the bare minimum to see a dialog pop up and go away again after the background task completes.
Protected Sub EditBtn_Click(sender As Object, e As EventArgs)
ScriptManager.RegisterStartupScript(Page, Page.GetType, Guid.NewGuid().ToString(), "javascript: bPopup = $('#element_to_pop_up').bPopup({ modal: true, modalClose: false});", True)
MenusBeingGenerated.Visible = True
bw = New BackgroundWorker()
bw.WorkerReportsProgress = True
bw.WorkerSupportsCancellation = True
bw.RunWorkerAsync()
End Sub
The javascript fires the bPopup, while MenusBeingGenerated is the name of the server-side DIV. The background task itself has been ripped down to just a do-nothing time-consumer.
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bw.DoWork
For i = 1 To 40
Threading.Thread.Sleep(250)
bw.ReportProgress(i, "Running...")
If bw.CancellationPending Then
bw.ReportProgress(i, "Cancelling...")
Exit For
End If
Next
' Cleanup
If bw.CancellationPending Then
e.Cancel = True
bw.ReportProgress(100, "Cancelled.")
End If
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles bw.ProgressChanged
StatusLabel.Text = e.ProgressPercentage.ToString & "% complete."
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bw.RunWorkerCompleted
StatusLabel.Text = "Completed successfully"
ScriptManager.RegisterStartupScript(Page, Page.GetType, Guid.NewGuid().ToString(), "javascript: if (bPopup) { bPopup.close() };", True)
End Sub
When I run this code, and click the button that triggers it all there is a long pause then the screen blinks grey (the same behavior can be seen by opening and closing the bPopup from within the same button handler).
Adding breakpoints to walk through the code, I see the background task get launched via the button handler, it cycles through and the BackgroundWorker1_ProgressChanged handler gets called every quarter second, finally the BackgroundWorker1_RunWorkerCompleted method runs, and when that finishes the only page refresh finally happens (with the blink as bPopup fires & clears).
The DIV id=MenusBeingGenerated never displays, despite having been set as visible, nor does the label id=StatusLabel that resides within it ever get rendered.
Any clues what I've missed???
[ BTW, there are several questions along these lines on this and other web sites, none of the current answers apply. I've cookbooked this off several sites, thrown out multiple versions, and this is the closest I've gotten to something that works -- and it's no different than where I started: the user clicks the go button, a lot of nothing visible happens while the server churns out a new file, then the page refreshes when it's all done. ]

Run loop after page load VB.NET

I would like the page to load, then I will display a GIF that's LOADING DATA and then I want this loop (see below) to run until the row count is not 0.
(An SQL job has been triggered in the previous page which takes about 1 - 2 minutes to run and so I want to display the new data automatically when it appears in the database.)
I can't figure out how to programmatically get this to work, I understand that because I have this in the page_load it will run before the page loads but I'm not sure how to run it after the page has loaded
Any help, advice or tips are very welcome
Thank you
Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
Do While GridView1.Rows.Count = 0 And GridView2.Rows.Count = 0
GridView1.DataBind()
GridView2.DataBind()
Loop
End Sub
If using Ajax/JQuery, can you not use a timer control that posts back every seconds and as soon as there is data in both tables then disable the timer.

Having issues with the .Visible property of a text box

I'm working on a web application where a user can order a custom portrait drawings by making choices from a variety of radio button lists. There is an extra charge if they want multiple people in the portrait, so one of the questions is whether or not there will be an additional person. They can select either "Yes" or "No" from a radio button list (the choices are currently in that order, with Yes at index 0 and No at index 1). If the user selects No, nothing happens and they move on to the next question. But if they click yes, a text box whose Visible property is currently set to False appears and they enter the number of additional people.
The problem is that I can't get my text box to become visible. I currently have my TextBox.Visible = True line of code under the radio button list's SelectedIndexChanged event handler. Here's the whole subroutine:
Protected Sub RadioButtonListAdditional_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles RadioButtonListAdditional.SelectedIndexChanged
Dim characters As Integer
If RadioButtonListAdditional.SelectedIndex = 0 Then
txtAdditionalChars.Visible = True
characters = CInt(txtAdditionalChars.Text)
additionalprice = characters * 5
ElseIf RadioButtonListAdditional.SelectedIndex = 1 Then
additionalprice = 0
End If
End Sub
additionalprice was declared earlier. The rest of that just has to do with the pricing scale for the additional characters. Anyway, I feel like I'm really close to having it right and that I'm just missing something small - my VB is a little bit rusty. Any suggestions?
Have you set AutoPostBack = True on your RadioButtonListAdditional object?
Or you may need to check whether you have code on Page_Load event that call
txtAdditionalChars.Visible = False
Please let me know if this helps
In VB Net .. Radiobutton control ..
Do it in your Radiobutton CheckedChanged event .. something like this
if rb.Checked then txtbox.Visible = True

RadDatePicker in RadGrid

I'm using Telerik RadGrids and I have RadDatePickers for columns with type of DateTime. However, when I want to pick a date it doesn't work at all. No errors, but it's simply not working. I've noticed that strangely I could select more days, but I want to be able to select a single day by clicking once on the given day.
I've observed that when clicking on the previous/next month I can select a day exactly as I would like, so there is a hack which might work (in my research it worked perfectly after the first postback, but before the first postback it didn't work at all):
Public Sub PageInit(ByVal sender As Object, ByVal e As
System.EventArgs)
Me.Page.ClientScript.RegisterStartupScript(GetType(TTControls.TTAjaxState),
"Page_Init_" & Me.UniqueID, "if (!(hasEndRequest)) var hasEndRequest =
false; if (!(hasEndRequest)) {var prm =
Sys.WebForms.PageRequestManager.getInstance();prm.add_endRequest(function()
{$('.rcPrev').click();console.log('clicked');});} hasEndRequest =
true;", True)
End Sub
In the code above you can see a method which will be a handler for page init.
However, I would very much like to be able to select a day by clicking on the RadDatePicker and clicking on a day. Here is how my RadDatePicker looks like:
If anybody knows the solution and decides to share it with me (if the solution is not a hack then I would be extremely happy) I would be very gladful.
Thank you in advance,
Lajos Árpád.
Will it demand a lot of work to replace your RadDateTimePicker by a RadCalendar?
The RadCalendar supports the property SelectionMode..
Have you set the SelectionMode property to Single?
Solution:
Sys.Application.add_load(function () {
$(".RadCalendar.RadCalendar_Default .rcPrev").each(function () { $(this).click(); });
});

WEIRDEST ERROR EVER: ASP.NET and Firefox?

I've always thought that server-side code is unaffected by the browser requesting it, but I've just come across an exception:
I have a button that when clicked, changes the button's CSS class (and those relative to it) and rebinds a GridView with new data. The function is this:
Private Sub lbViewUnUsed_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbViewUnUsed.Click
lbViewUsed.CssClass = "button small"
lbViewUnUsed.CssClass = "button small selected"
BindUsage(UsageBinding.UnUsed)
End Sub
In IE, this works perfectly. However, in Firefox, the BindUsage() function stops firing midway and nothing gets accomplished at all! In my frustration, I inserted Debug.Assert() statements after every line of code in the BindUsage() to see what was going on, and sure enough, the function would not go all the way through in Firefox ONLY.
The BindUsage() function is this (with the Debug assertions still in):
Private Sub BindUsage(ByVal bindWhat As UsageBinding)
Debug.Assert(False, 1)
If bindWhat = UsageBinding.Used Then
gvUsage.DataSource = sUser.GetWordsMarkedWithUsage(True)
gvUsage.Columns(0).Visible = False 'hide button column'
Else
Debug.Assert(False, 2)
gvUsage.DataSource = sUser.GetWordsMarkedWithUsage(False)
Debug.Assert(False, 3)
Dim userInfo As UserAccount.LoginInfo = UserAccount.GetUserLoginInfo
Debug.Assert(False, 4)
Dim showUsedButton As Boolean
Debug.Assert(False, 5)
showUsedButton = (userInfo.UserName.ToLowerInvariant = sUser.UserName.ToLowerInvariant)
Debug.Assert(False, 6)
gvUsage.Columns(0).Visible = showUsedButton 'show button column'
Debug.Assert(False, 7)
End If
Debug.Assert(False, 8)
gvUsage.DataBind()
Debug.Assert(False, 9)
End Sub
The above function does not make it past 5 in Firefox. I'm guessing there's some sort of problem with the assignment of the showUsedButton variable, but I can't figure out what it would be. Why would this fail only in Firefox, but not in any other browser especially since this is occurring on the server???
It's happening on the server, but you are retrieving data that was generated by the client:
UserAccount.GetUserLoginInfo
I would review your stored data and see what's different in it based on client application and see how it's handled.
Well, it's a bit of a guess, but...
What exactly does UserAccount.GetUserLoginInfo do? If I had to guess, it tries to do something browser-specific. For example, it could try and read a cookie from the client? If it does, then my next guess is that userInfo or either userInfo.UserName is null, thus throwing a NullReferenceException.
It could be that IE accepts cookies, and firefox does not. Hence no error in IE, but only in Firefox.
Again, this is guesswork, I'm not strong in VB.NET and have no idea what that UserAccount class is. Maybe if you'd give use the errormessage, it could mean some more.
Would you definitely spot if any exceptions were being thrown? If not, fix that before you do anything else.
As for why it might be browser-specific:
If it depends on what data is sent by the browser and how, then clearly the browser can change the behaviour
I believe ASP.NET renders in a browser-specific way; if your code is touching the rendering logic, it may be behaving differently depending on user agent.

Resources