how to show selected text in a dropdownlist? - c#

I have a static drop-down list control in asp.net page and I have two different string (based on a result which I am fetching through database) now I want to make selected text of the the current input and the indexing and values remains same.
<asp:DropDownList ID="ddl_room_type" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_room_type_SelectedIndexChanged">
<asp:ListItem Selected="True">Select Type</asp:ListItem>
<asp:ListItem Text="Deluxe" Value="2500"></asp:ListItem>
<asp:ListItem Text="Super Deluxe" Value="3000"></asp:ListItem>
<asp:ListItem Text="Luxury" Value="5000"></asp:ListItem>
<asp:ListItem Text="Special Room" Value="10000"></asp:ListItem>
</asp:DropDownList>
and I have a string Text from Database : lets say Special Room. Now I want that drop-down list i.e, ddl_room_type should automatically bind with the same text and value.
C# Code which I am using:
string luxury = "Special Room"; //Value coming form Database
ddl_room_type.SelectedItem.Text = luxury;
If I do with this code.. it is just selected the first text as : "Special Room"

Alternately you can try this
string luxury = "Special Room";
ddl_room_type.ClearSelection();
ddl_room_type.Items.FindByText(luxury).Selected = true;
Hope it will be useful

Get your values from the database at first for both Text and Value for loading the dropdownlist. So that you don't have to set it once the user makes the selection.

Related

Can't two ListItem objects have same value property?

My Code looks like
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem Value="100">A</asp:ListItem>
<asp:ListItem Value="100">B</asp:ListItem>
</asp:ListBox>
Now when I try to select B, It by default selects A always. B is never selected. What is the reason for this behavior ?
The <asp:ListBox> renders as a <select> with <option>s in HTML. The value is persisted across postbacks, not the text. So it can't tell which value you wanted when there are multiple options with the same value, so it assumed the first. It's best practice to use unique option values.
It's recommended to use different values for 2 or more items. No two items can have the same values.
Set AutoPostBack="True" and you'll find that no matter how many items you add in the ListBox, it will always select the 0th index of that particular value.
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True">
<asp:ListItem Value="101">D</asp:ListItem>
<asp:ListItem Value="100">A</asp:ListItem>
<asp:ListItem Value="100">B</asp:ListItem>
<asp:ListItem Value="102">E</asp:ListItem>
<asp:ListItem Value="100">C</asp:ListItem>
</asp:ListBox>
Like, selecting B or C will always select A; where A being the 0th index for that particular value 100.

How can I multiply values from 2 dropdown lists in asp.net and display the results in a text box?

I have a lot of dropdown lists I am building for an insurance app. Each list has a number of options given a multiplier value. My question is how can I multiply these values from separate dropdown lists to give myself a final multiplier value in a textbox? I want to use whatever value is picked from each dropdown list.
The lists are formatted as follows
<asp:DropDownList id="points" runat="server">
<asp:ListItem Value="1.00">0</asp:ListItem>
<asp:ListItem Value="1.05">1</asp:ListItem>
<asp:ListItem Value="1.10">2</asp:ListItem>
<asp:ListItem Value="1.18">3</asp:ListItem>
<asp:ListItem Value="1.25">4</asp:ListItem>
<asp:ListItem Value="1.32">5</asp:ListItem>
<asp:ListItem Value="1.40">6</asp:ListItem>
<asp:ListItem Value="1.47">7</asp:ListItem>
<asp:ListItem Value="1.55">8</asp:ListItem>
<asp:ListItem Value="1.62">9</asp:ListItem>
<asp:ListItem Value="1.70">10</asp:ListItem>
<asp:ListItem Value="1.77">11</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList id="OtherPolicy" runat="server">
<asp:ListItem value="1.20">Yes</asp:ListItem>
<asp:ListItem value="1.00">No</asp:ListItem>
</asp:DropDownList>
I have only been coding for about 2 weeks so hopefully somebody can point me in the right direction. I have not found anything similar in the search here. Thanks guys!
It's important to remember that the values that are going to be stored within your DropDownLists will be strings, so the first step will be converting them to numerical values, which can be done in many ways, but for example purposes, you can use the Convert.ToDecimal() method :
// Convert your Points to a decimal
var pointsValue = Convert.ToDecimal(points.SelectedValue);
// Then convert your selection from your other DropDownList
var policyValue = Convert.ToDecimal(OtherPolicy.SelectedValue);
// Now that you have both of these, you can multiply them to retrieve your result
var result = pointsValue * policyValue;
// Store your result in another TextBox
YourResultTextBox.Text = result.ToString();
If you have more DropDownLists or other elements that would need to be part of this calculation, you would just need to ensure that you parse each of their values and then include them in your calculation.

set value of listItem in DropDownList

if (currentMap.EditMap != null)
{
ddlEditMapGroupName.SelectedIndex = ddlEditMapGroupName.Items.IndexOf(ddlEditMapGroupName.Items.FindByText(currentMap.EditMap));
}
sets the value of the drop down box to the stored value...but if currentMap.Edit map is null how can I set the value to the text of the listItem?
<asp:DropDownList ID="ddlEditMapGroupName" AppendDataBoundItems="true" runat="server">
<asp:ListItem Text="Select Group" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
If I understand your question...
It sounds like you've got a dropdown list that already has a bunch of listitems in it (?).
And you're trying to set the selected item to match another control on the page.
One strategy I use (for certain cases) is to put a record into whichever table you're pulling your dropdownlist data from that says, simply 'Not Selected'. Then, make sure that whatever you're using to query data for your dropdownlist pulls that 'Not Selected' record into the list.
With that, you can simply add an 'else' clause to your control statement there to set the selected value to 'Not Selected' when appropriate.
if (currentMap.EditMap != null)
{
ddlEditMapGroupName.SelectedIndex = ddlEditMapGroupName.Items.IndexOf(ddlEditMapGroupName.Items.FindByText(currentMap.EditMap));
}
else
{
ddlEditMapGroupName.SelectedValue = "0";
}
<asp:DropDownList ID="ddlEditMapGroupName" AppendDataBoundItems="true" runat="server">
<asp:ListItem Text="Select Group" Value="0" Selected="True"></asp:ListItem>
</asp:DropDownList>
You could simply set the SelectedValue property to the default value which in your case is Select Group and its value is 0...
This should be done once your DropDownList is already bind to a data source.

checkbox list to generate one textbox

So i have this checkbox list and im trying to generate a textbox when the MISC checkbox is checked.
<asp:CheckBoxList ID="chbxEquipmnt" runat="server"RepeatColumns="4" RepeatDirection="Horizontal" ValidationGroup="Equipment" OnSelectedIndexChanged="chbxEquipmnt_SelectedIndexChanged">
<asp:ListItem Value="Laptop">Laptop</asp:ListItem>
<asp:ListItem Value=" Label Printer">Label Printer</asp:ListItem>
<asp:ListItem Value="Printer">Printer</asp:ListItem>
<asp:ListItem Value="Fax Line">Fax Line</asp:ListItem>
<asp:ListItem Value="PC">PC</asp:ListItem>
<asp:ListItem Value="MFD">MFD</asp:ListItem>
<asp:ListItem Value="Phone Line">Phone Line</asp:ListItem>
<asp:ListItem Value="Misc">Misc</asp:ListItem>
</asp:CheckBoxList>
ON my Cs page I have.........
protected void chbxEquipmnt_SelectedIndexChanged(object sender, EventArgs e)
{
if (chbxEquipmnt.SelectedValue == "Misc")
{
TextBox txt = new TextBox();
txt.ID = "txtMiscCheckBox";
Page.Form.Controls.Add(txt);
}
}
debugging i have tried with Postback which continuously on returns the first check box that was checked...for example i checked laptop instead of Misc first, the value in the debugger always show me laptop. Im not sure if a simple loop to go thru the all the button clicks would help.....I'm fresh out of school and this is a career change for me so thanks for the patience
I would change the approach: don't create the Textbox at run time.
Have it created at design time, hide it by default (Visible property), then show it when Misc option is selected.
You will run into less issues that way. Creating controls in runtime require some extra effort : dealing with ViewState, re-creating the control on PostBack, etc.

TextBox filtering according to RadioButton selection

I've a RadioButtonList and a TextBox. Accordindg to RadioButtonList selection, I want to filtrering textbox.
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem Text="NUMBER" Value="1" Selected="True"></asp:ListItem>
<asp:ListItem Text="TEXT" Value="2"></asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="txtID" runat="server">
When I click listitem with value 1, user can enter number to textbox. value 2, number + text.
also I want to set the length. Can someone show me how to do this?
Write a Javascript function which will get selected value from the RadioButtonList1 control and compare with value 1 or 2. If it is 1, user should be allowed to enter numbers only. This can be enforced from client side key press event of text box, which will check the result of comparison, to allow the pressed key to actually render in the text box.
Refer this to see how to get selected value from RadioButtonList control using Javascript

Categories