ASP.NET Adding new field to existing form - asp.net

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.

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.

.NET Active Server Page FindControl is always returning nothing

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.

How to keep changed status of CheckBoxList?

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.

Populating a textbox in ASP.NET without reloading the page?

I have a listbox, a textbox, and a button. The button populates the textbox with the selected item & value of the listbox. As below:
Protected Sub GetVariables_Click(ByVal sender As Object, ByVal e As EventArgs) Handles GetVariables.Click
Me.txtLetter.Text = lstNames.SelectedItem.ToString & lstNames.SelectedValue.ToString
End Sub
The problem I have, is that when doing this it reloads the page each time. Any way around this?
Thanks,
Jason
There are two options.
Set the value in the textbox using javascript on the client.
Use ajax.
For this type of extremely simple thing, use javascript.
You can use Javascript to do this. Textboxes are given a Client ID by ASP.NET when they are outputted to the web browser. You'll need that ID to be able to make changes to the textbox - the Client ID translates to the id property of the HTML element.
There's a number of ways of doing it, one of which is to create a hidden HTML field containing the [yourtextboxname].ClientID property. You can then use Javascript to read in the value of the hidden field, and select the element and do stuff with it.

ASP.NET AjaxControlToolkit change Combobox content dynamically per Ajax

If I understand it right the new ACT ComboBox Control is Bound once to a given Datasource.
But the count of the records I want to bind is very large.
So I want to load the content of the ComboBox List via Ajax after the user typed in a few charachters.
So at page load the combobox list should be empty and if something is typed in the list is loaded with the typed text as search text.
I tried this:
<asp:ComboBox ID="cbxCompany" DropDownStyle="DropDownList" runat="server" AutoCompleteMode="Append" />
Protected Sub cbxCompany_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxCompany.TextChanged
Dim dt As DataTable = GetCompanies(cbxCompany.Text)
cbxCompany.DataSource = dt
cbxCompany.DataTextField = "nameout"
cbxCompany.DataValueField = "cid"
cbxCompany.DataBind()
End Sub
GetCompanies is my method for getting data from the database, the parameters filters the select statement. But this doesn't work.
Is there a way to reload the combobox content per Ajax?
The functionality you're describing could be done easily with the ACT AutoComplete. See examples at http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/AutoComplete/AutoComplete.aspx
you need to set autopostback=true

Resources