How to keep changed status of CheckBoxList? - asp.net

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.

Related

How to access a control that was created during page load

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.

VB.NET page with two autopostback dropdownlists

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.

ASP.NET Adding new field to existing form

So i have a form and one of the fields may include one item or may require more fields to be created immediately to accommodate the extra input. so something like below:
FieldName:------ +
The plus sign would be clicked to get another field:
FieldName:------ +
FieldName2:------ +
.... so on
Sorry for poor illustration. I am looking to do this in an asp.net page. any idea how? need ajax component?
Thanks,
EDIT:
Protected Sub Button1_Click( ByVal sender As Object , ByVal e As System.EventArgs) _
Handles Button1.Click
Dim t As TextBox
t = New TextBox
PlaceHolder1.Controls.Add(t)
End Sub
This code would add one textfield. But if I want to add more, i am unable to do so...
You could create a Placeholder control and onclick (postback) of the '+' button add a new control (e.g. Textbox) to the Controls collection of the Placeholder.
You would need to remember to re-add any controls to the placeholder upon subsequent postbacks (within the OnInit() preferably). This is so that previously added controls and their values can be retained.

ASP.NET Button click not caught (button in user control which is dynamically loaded in Repeater)

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?

How to bind a function to "Click" event of a dynamically added ImageButton?

I am completely new to Asp.net.
I am currently working on a asp.net page coded via Visual Basic
On the page I have these image buttons that are dynamically generated based on the SQL Data the page retrieves.
Unfortunately I could not figure out how to bind a click function to each of these buttons.
the [Control Name].onClick = "Function Name()" is for clientSide scripts.
and I need to bind
Protected Sub Button_InsertNewTextBox_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button_InsertNewTextBox.Click
to the dynamically allocated buttons.
I couldn't figure out how.
Can anyone help?
I believe in Visual Basic the AddHandler statement is what you're after

Resources