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

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.

Related

How to Populate ListBox without PostBack in ASP .NET?

My Web Form consists of two Listboxes lstbx01 and lstbx02. lstbx01 is databound to sqlDataSource01 and it Populates on Page Load Event. lstbx02 is set to populate when the selected value of lstbx01 is passed as a Parameter to sqlDataSource02. Everything works fine if lstbx01 has its AutoPostBack property set to True.
I do not want the page to be Refreshed. I want to set the lstbx02 to populate without page being PostBack.
I have tried the below mentioned Code:
Protected Sub lstbx02_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstbx02.SelectedIndexChanged
lstbx02.DataSource = sqlDataSource02
lstbx02.DataBind()
End Sub
This doesn't help.
I do not think that what you are asking would be possible in WebForms. The code you provided would require a postback to transmit the event back to the server so it can be handled.
Is there a specific reason you cannot do a postback? A possible workaround would be to use AngularJS, Knockout, or some other JS framework to populate inputs on the page. You would then need an event handler that either used AJAX to fetch the relevant data or displayed data that has been cached into the page as JSON.

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.

How to avoid InvalidOperationException when setting the defaultbutton in ASPX content

I am trying to set a default button in my ASPX page. I have a master page so the form is there. I have a panel in the content page that holds a table that organizes a number of textboxes, dropdowns and other inputs. The last row of the table holds some buttons, one of which I want to be the default button. After doing some research, I have tried the following with no success.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
pnlHolder.DefaultButton = cmdSearchJob.ClientID
I have also tried
pnlHolder.DefaultButton = cmdSearchJob.UniqueID
and
Dim cmdDef As Button = pnlHolder.FindControl("cmdSearchJob")
pnlHolder.DefaultButton = cmdDef.UniqueID
but both throw the exception "The DefaultButton of 'pnlHolder' must be the ID of a control of type IButtonControl.".
I have seen some Javascript solutions, but was hoping to just be able to set the defaultButton for the panel.
Try setting the DefaultButton of the parent Form.
C#:
this.Page.Form.DefaultButton = cmdSearchJob.UniqueID;
VB?:
me.Page.Form.DefaultButton = cmdSearchJob.UniqueID
Similar issue here: Allow Enter key to login in asp.net?
Try setting it to:
cmdSearchJob.ID
The panel will call FindControl to get the Client ID itself
Can you set this inside the control on the front-end?
<asp:Panel id="pnlHolder" DefaultButton="cmdSearchJob">
<asp:Button id="cmdSearchJob" runat="server" Text="Search" />
</asp:Panel>
Also, this may worth knowing, what type of object is cmdSearchJob? Is it a standard asp.net button control?
Finally found what it was. Tried adding another button with no CSS, etc. that just popped up a javacsript alert() for testing. Was able to set that as the default button when it was outside the table. As the submit button is one of a series of buttons (not a very pretty UI, but user requirements and all that) in the table, I used this additional button and set its style to display:none and have it call the same subroutine in the code behind.
So, short answer, it wasn't seeing it in the table.
Thanks everyone for your input.
I still have no idea why it wouldn't set the button.

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?

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