I have simple VB.NET that contains two database driven dropdownlists. Each dropdownlist has an autopostback property that will execute a function that takes user to another page.
The problem I have is that if user selects dropdownlist A, is taken to the A page, but then presses back button and selects dropdownlist B, the autopostback will occur for dropdownlist A since dropdownlist A is still selected. Any ideas on how to get around this?
I've tried everything I can think of. I do reset the dropdownlist and comment out the Response.Redirect code to prove that it does actually reset. But then as soon as I put the redirect back in, it loses the reset to unselected ability. I've seen this "bug" posted elsewhere online, but have not found a solution that works.
Protected Sub ddlSearchAward_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlSearchAward.SelectedIndexChanged
Dim strParam As String
strParam = ddlSearchAward.SelectedItem.Value
Response.Redirect("awards_?criteria=" & strParam)
End Sub
Protected Sub ddlAwardList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlAwardList.SelectedIndexChanged
Dim strParam As String
strParam = ddlAwardList.SelectedItem.Value
Response.Redirect("awards_?id=" & strParam)
End Sub
I had to forgo the autopostback and put a button next to each dropdownlist to get it to work.
Related
The page I have pulls information from the database and based off that it will either generate a text box or radio button on the page load. The issue I am running into is that I am not able to utilize it later in the code behind. I am wondering if its possible and how to make these accessible. For example, TextBox1 is created during the page load, then on a button click the code below will throw the error "'TextBox1' is not declared."
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label1.text = TextBox1.text
End Sub
Best practice would be to put the controls in the ASPX markup. That allows you to reference it from code behind everywhere; however, if that is not an option for you and you need to handle everything in code:
Dim control as Control
Protected Sub Load()
If ConditionForRadioButton Then
control = new RadioButton()
End If
End Sub
Protected Sub Button1_Click()
If ConditionForRadioButton Then
Dim radio = CType(control, RadioButton)
' do things with radio
End If
End Sub
Add other cases for the other control types; if this is in some kind of collection you'd have to extrapolate this to work in that case, but the idea would be the same.
I created a simple page with one button, then on the click event have it use FindControl, to get a reference to itself. But..... FindControl is returning nothing.
code
Protected Sub EntryDoor1_Click(sender As Object, e As System.EventArgs) Handles EntryDoor1.Click
Dim control = FindControl("EntryDoor1")
control.Visible = False
End Sub
Because you've said that you want "a reference to itself", i assume that you want a reference to the button that has caused the click-event.
The easiest is to use the sender argument, because that's always the source control:
Dim button = DirectCast(sender, Button)
But when the button is on top of the page(as in this case), the reference to the control is automatically created in the partial designer.vb file:
EntryDoor1.Visible = False
So why using FindControl if you have a direct reference anyway?!
Edit:
Just for the sake of completeness. The behaviour you're describing can only have one reason: You're trying to use FindControl in a ContentPage of a MasterPage. This is a special case, you need to get the reference to the ContentPlaceholder first. Then you can use FindControl for your Button:
Dim button = DirectCast(Page.Master.FindControl("ContentPlaceHolder1").FindControl("EntryDoor1"), Button)
But again, this is pointless since you have the reference in the page directly.
I know the title is vague, so I'll fully explain here my problem..
So, I'm running VB.net 3.5. I have a dynamic list of server names, and I want to put them in a CheckBoxList. The list is populated and, using that same list, I make a graph of the performance for each server listed. I want to be able to check and uncheck the checkboxes representing servers and, when I lick an update button, it'll create a new graph and graph only the servers that are still checked. I noticed that the page still loads before the button click is handled, so the CheckBoxList will repopulate itself before being able to read the current CheckBoxList. Does anybody have any input on this?
This is my load. And I populate my checkboxlist inside ShowView()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
If _myQSVar.Count = 0 Then
Calendar1.SelectedDate = Date.Now.Date.AddMonths(-6)
Calendar2.SelectedDate = Date.Now.Date
End If
tbFromDate.Text = Calendar1.SelectedDate.ToShortDateString()
tbToDate.Text = Calendar2.SelectedDate.ToShortDateString()
lstControls = New List(Of System.Web.UI.Control)
ShowView()
End If
End Sub
I load my checkboxlist with a simple for loop
For each one As String in ServerList
chkboxList.Items.Add(one)
Next
And I wanna try to preserve the checkboxlist values when I do an event handler for an update button.
Private Sub btnUpdateGraph_Click(sender As Object, e As System.EventArgs) Handles btnUpdateGraph.Click
'insert code
End Sub
Are you populating the checkboxlist on form.load? If so make sure you checking for if Page.ispostback. Load the checkboxlist only if its not a postback. This will make sure the page(and the checkboxlist) is not reloaded when you click.
Also, please do not lick update buttons. They dont really like that!
I believe you are binding CheckboxList at PageLoad Event. Just place the code inside the given code block:
if(!IsPostback)
{
// Bind your Checkbox List here
}
This will bind your checkboxlist only in case if the page loads first time, not after any postback.
I hope this will help you out.
An aspx page's Page_PreInit event happens before an component server control's (e.g. a TextBox) Init event. However it is possible to set a TextBox's Text property in Page_PreInit. I suppose this means the TextBox's Text is set before the TextBox is even initiated. How is that possible?
I suppose you need to tell why you want it, what is the scenerio in which you wish to use it since at least I never used textbox's text property in preinit.
But you may put a text box to the page and in the codebehind you may write:
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
TextBox1.Text = "test"
End Sub
At: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox_events.aspx. it says textbox init event occurs when the control is initialized not before it. And it also says that init is the first step of it's lifecycle.
I have written a user control that captures some user input and has a Save button to save it to the DB. I use a repeater to render a number of these controls on the page - imagine a list of multiple choice questions with a Save button by each question.
I am loading the user control inside the repeater's ItemDataBound event like this (code simplified):
Protected Sub rptAssignments_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptAssignments.ItemDataBound
Dim CurrentAssignment As Assignment = DirectCast(e.Item.DataItem, Assignment)
Dim ctl As UA = CType(LoadControl("~\Controls\UA.ascx"), UA)
ctl.AssignmentID = CurrentAssignment.AssignmentID
ctl.Assignment = CurrentAssignment.AssignmentName
ctl.EnableViewState = True
e.Item.Controls.Add(ctl)
End Sub
FYI, I need to load the control at runtime rather than specify it in the ItemTemplate because a different control could be used for each row.
In the user control, there is a linkbutton like this:
<asp:LinkButton ID="lbnUpdate" runat="server" Text="Update" OnClick="lbnUpdate_Click" />
... and a button click handler like this:
Protected Sub lbnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lbnUpdate.Click
' my code to update the DB
End Sub
The problem is that when the Save button is clicked, the page posts back, but lbnUpdate_Click is not called. The Page_Load event of the page itself is called however.
I should mention that the repeater is part of a user control, and that user control is loaded inside another user control (this is a DotNetNuke site which makes heavy use of user controls). The Save button link looks like this:
javascript:__doPostBack('dnn$ctr498$AssignmentsList$rptAssignments$ctl04$ctl00$lbnUpdate','')
This problem exemplifies how webforms outsmarts itself.
You have to reconstitute the Repeater, either by re-binding or from viewstate, to have sub-controls raise events. The price you pay is either another trip to your data source or all that redundant data stored on the client in the viewstate. Shameful!
I had a similar problem once that might be the same thing.
In short, since you are dynamically creating the buttons, after the postback they don't exist. Thus, when ASP.NET Webforms looks for the event, it can't find anything.
When does your repeater get databound? Try rendering the buttons to the page again in the postback (even as a test) to see if that does the trick.
Are the UserControl's IDs same on every postback?