Getting a value in a textbox from a dropdown list - c#

I have a textbox named textbox1 and a dropdownlist. Dropdownlist contains the list of branches and i want that when i select a branch from dropdown i want to get the respective branch code to be generated in a textbox from the Database or from c# coding. How will it be possible ?

Am I missing something, is it more complex that just putting an event handler on the dropdown so that when it's value changes it calls your method that can do whatever it needs to do to generate the string for the textbox?

Skipping passed the "database" portion of your question (and assuming your data isn't bound to the DropDownList)..
One way in ASP.NET, to accomplish this, I believe, is to have your code print DropDownList.SelectedValue's value in the textbox during an event.
Here is a link showing how to bind an event to DropDownList in jQuery

Related

To get value dynamically in gridview based on button click

i have one dropdownlist drpSelectCollege and 2 tables College_M and CollegeSubject_M.In dropdown collegeid is been selected from College_M and now on the basis of College Id another field Subject along with corresponding value of Collegeid is to be displayed in gridview on a click of button search in c#.Plz help me out....
I guess you have a list which you use to bind in the DropDownList.
Look for the SelectionChanged event, when the event is fired you can use the SelectedIndex to use in your list and get the value you want.

Can values from dropdownlists in detailsview be updated without autopostback?

I have a details view that is in "insert Mode" so the user just sees blank spaces to enter values. I have two drop down lists and I wanted to have the second ddl change its value by what was selected in the first ddl. I tried setting ddl1 to a label so ddl2 would change when the label changed. The problem I am having now is that I need autopostback to update the value of the label, but selecting "autopostback" on the ddl1 makes my code throw a data binding error.
I was wondering if there was any way I could get around using autopostback and still update values selected in the first ddl to the label.
Thank you.
Try using AjaxControlToolkit. It has a feature to cascade ddlists. Use updatepanel as container for both of ddls so you can omit autopostback.
Your query is not completely clear. But if you want to change the dd2 value on change event of dd1, you can use the following code:
$("#<%= statusDDL.ClientID %>").val($("#<%= dd1.ClientID %>option:selected").text() );
It is not clear whether you want value or text property.
Also I am not 100% that this syntax will work. But obviously it can be done using this concept searching on net for your requirement

Is there a textboxlist control available somewhere?

There are ASP controls such as radiobuttonlist and checkboxlist and you can databind them to a database query. It's great for creating dynamic lists with user interaction. What I'm trying to do is generate a list of textboxes in the same fashion. A list of textboxes that behave the same way.
The object is to have a checkboxlist that is generated via datasource/database. When the user is finished selecting items from this list, they click a button. That list hides (using jquery) and a new list is created based on their selections. However, the new list is now a list of their selections accompanied by an empty textbox. The user fills in the textboxes for each entry and submits again which commits it to a database.
SO:
checkbox - description
checkbox - description
checkbox - description
checkbox - description
Becomes:
Description - Textbox
Description - Textbox
The reason that I'm looking for a list-type control is so that I can ultimately loop through it for submission to the database using linq. Does that make sense? My real question is if there is a control like this yet. I gave the full description in case someone has any other ideas, short of creating a custom control.
There's nothing out of the box that does what you describe no. But you can still loop through controls. I would put your form controls inside of an asp:Panel or a div with runat="server" and use something like the following code to cycle through them as you described.
foreach(Control ctl in myPanel.Controls)
{
//check control type and handle
if (ctl is TextBox)
{
//handle the control and its value here
}
}
asp:ListBox has a property named SelectionMode, which can be set to SelectionMode="Multiple" and allows you to select items you want. This is not exactly what you need but it's a simple solution.
After the selection is made in checkboxlist, make a postback to server. Here create a datatable with two columns (description & text). For every selected item in the checkboxlist add a row to this table and bind it to a gridview control. Here 'text' column will be always empty. Configure the gridview to use template column with a textbox in the itemtemplate

dropdownlist selectedvalue

I've used a webservice to create a dropdownlist of countries and I'm trying to add the dropdownlist selection to the sql database, but when I do that by using ddlCountry.SelectedValue in my insert statement, only the first value in the dropdownlist is showing up in the table. Should I use onselectedindexchanged to somehow store the value? What code should I be using?
Try using ddlCountry.SelectedItem.Value instead.
You need to make sure you're not repopulating your dropdown from the web service on a postback. That will cancel out your selection.
If you are firing some sort of save function on button click's even then you can go ahead with ddl.SelectedItem.Text (if you wan't to store the text in your table) if you want to store the value part of the country list then go ahead with ddl.SelectedItem.Value
SelectedIndexChanged should be only used if you want to perform some action at the time when user selects an item in the drop down

How to retrieve a changed value of databound textbox within datagrid

ASP.NET 1.1 - I have a DataGrid on an ASPX page that is databound and displays a value within a textbox. The user is able to change this value, then click on a button where the code behind basically iterates through each DataGridItem in the grid, does a FindControl for the ID of the textbox then assigns the .Text value to a variable which is then used to update the database. The DataGrid is rebound with the new values.
The issue I'm having is that when assigning the .Text value to the variable, the value being retrieved is the original databound value and not the newly entered user value. Any ideas as to what may be causing this behaviour?
Code sample:
foreach(DataGridItem dgi in exGrid.Items)
{
TextBox Text1 = (TextBox)dgi.FindControl("TextID");
string exValue = Text1.Text; //This is retrieving the original bound value not the newly entered value
// do stuff with the new value
}
So the code sample is from your button click event?
Are you sure you are not rebinding your datasource on postback?
When are you attempting to retrieve the value from the TextBox? i.e. when is the code sample you provided being executed?
If you aren't already, you'll want to set up a handler method for the ItemCommand event of the DataGrid. You should be looking for the new TextBox value within that method. You should also make sure your DataGrid is not being re-databound on postback.
I would also highly recommend reading through Scott Mitchell's excellent article series on using the DataGrid control and all of it's functions:
https://web.archive.org/web/20210608183626/https://aspnet.4guysfromrolla.com/articles/040502-1.aspx

Categories

Resources