I have a following code in aspx page
<asp:DropDownList ID="txtSupplierCountry" class="form-control" Width="230px" runat="server">
<asp:ListItem Text="Select gender" Value="0"></asp:ListItem>
<asp:ListItem Text="male" Value="0"></asp:ListItem>
<asp:ListItem Text="female" Value="1"></asp:ListItem>
</asp:DropDownList>
I have a value in database as male/female. I need to set this text using C# code. Basically I am trying to set selected option based on the text from database. I am not getting an idea. I have tried following code but it doesn't work.
String t = "male" // coming from db
txtSupplierCountry.Items.FindByText(t).Selected = true;
when you are saving data Male and Female then why did you keep dropdown values in number like 1,2 etc, change code like this.
<asp:DropDownList ID="txtSupplierCountry" class="form-control" Width="230px" runat="server">
<asp:ListItem Text="Select Country" Value="0"></asp:ListItem>
<asp:ListItem Text="male" Value="male"></asp:ListItem>
<asp:ListItem Text="female" Value="female"></asp:ListItem>
then
txtSupplierCountry.SelectedValue="male";
Related
I want to get the values in a string from a database which i can do and my result is
string strValues = "'value1','value2','value3'";
i know we can split them using the split function. But the problem is how to mark them in the list box.
<asp:ListBox ID="lstValues" runat="server" SelectionMode="Multiple">
<asp:ListItem Value="-1">[``Select]</asp:ListItem>
<asp:ListItem Value="1">value 10</asp:ListItem>
<asp:ListItem Value="2">value 9</asp:ListItem>
<asp:ListItem Value="3">value 3</asp:ListItem>
<asp:ListItem Value="4">value 2</asp:ListItem>
<asp:ListItem Value="5">value 1</asp:ListItem>
</asp:ListBox>
<asp:ListBox ID="lstValues" runat="server" SelectionMode="Multiple">
<asp:ListItem Value="-1">[``Select]</asp:ListItem>
<asp:ListItem Value="1">value 10</asp:ListItem>
<asp:ListItem Value="2">value 9</asp:ListItem>
<asp:ListItem Value="3">value 3</asp:ListItem>
<asp:ListItem Value="4">value 2</asp:ListItem>
<asp:ListItem Value="5">value 1</asp:ListItem>
</asp:ListBox>
add Selected="true" on the Items you want to be checked!
Example: <asp:ListItem Value="5" selected="true">value 1</asp:ListItem>
you can also use Checked="true" for list boxes
im editing one gridview. while clicking edit im redirecting to one page and binding all the fields. but unble to bind month dropdown . i want to bind april month in dropdown but not binding
<asp:DropDownList ID="DDLMonth" runat="server">
<asp:ListItem>Month</asp:ListItem>
<asp:ListItem Text="January" Value="1"></asp:ListItem>
<asp:ListItem Text="February" Value="2"></asp:ListItem>
<asp:ListItem Text="March" Value="3"></asp:ListItem>
<asp:ListItem Text="April" Value="4"></asp:ListItem>
<asp:ListItem Text="May" Value="5"></asp:ListItem>
<asp:ListItem Text="June" Value="6"></asp:ListItem>
<asp:ListItem Text="July" Value="7"></asp:ListItem>
<asp:ListItem Text="August" Value="8"></asp:ListItem>
<asp:ListItem Text="September" Value="9"></asp:ListItem>
<asp:ListItem Text="October" Value="10"></asp:ListItem>
<asp:ListItem Text="November" Value="11"></asp:ListItem>
<asp:ListItem Text="December" Value="12"></asp:ListItem>
code behind
string DDLMonthstr = date.Month.ToString();//4
DDLMonth.SelectedIndex = Convert.ToInt32(DDLMonthstr);
Try
DDLMonth.Items.FindByValue(string val).Selected = true;
You probably have already selected item and need to clear selection using ListControl.ClearSelection before making new selection through code.
string DDLMonthstr = date.Month.ToString();//4
DDLMonth.ClearSelection();
DDLMonth.SelectedIndex = Convert.ToInt32(DDLMonthstr);
Also check if you are getting any exception by using try-catch block if you do not already have, this could help in fixing.
I have a dropdown list in design page as shown below:
<asp:DropDownList ID="ddlArtList" runat="server">
<asp:ListItem Value="95">Select</asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
</asp:DropDownList>
This items above are sometimes overridden by some other values in C#, according to the requirement.
But at the end i want to bind the above default items with the help of C# to get the above listitems.
I want to know is there any In-Built method or attribute to bind the dropdownlist(.aspx) in C#.
Without using this: ddlArtList.Items.Add ("1); etc etc.
Thanks in advance.
Use AppendDataBoundItems
.aspx Code:
<asp:DropDownList ID="ddlArtList" AppendDataBoundItems="True" runat="server">
<asp:ListItem Value="95">Select</asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
</asp:DropDownList>
ServerSide:
ddlArtList.AppendDataBoundItems="True"
You can keep the default list in Session during first Page Load -
if(!isPostback)
{
ListItem[] items = new ListItem[ddlArtList.Items.Count];
ddlArtList.Items.CopyTo(items, 0);
Session["ddlArtList"] = items;
}
Now when you want to reset the list -
if(Session["ddlArtList"] != null)
{
ListItem[] items = Session["ddlArtList"] as ListItem[];
ddlArtList.Items.Clear();
ddlArtList.Items.AddRange(items);
}
If you want to append to a default list, use the markup as you provided to set the default list. You will want to set AppendDataBoundItems to true, as already mentioned.
<asp:DropDownList ID="ddlArtList" runat="server" AppendDataBoundItems="true">
<asp:ListItem Value="95">Select</asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
</asp:DropDownList>
In the code-behind, you can set the DataSource to the appended items and simply call DataBind. That will append those items to your dropdownlist.
ddlArtList.DataSource = new List<int>{ 10, 11, 12 }; // replace with actual data source you are using
ddlArtList.DataBind();
Depending on all of your needs, you can add those appended items on the page's Load event or inside another event handler, such as a button click or selecting from another dropdownlist or whatever.
I have a dropdownlist control on masterpage in asp.net. as follows:
<asp:DropDownList ID="ddladults" runat="server" CssClass="input_bg">
<asp:ListItem Value="0">----Select----</asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
<asp:ListItem Value="7">7</asp:ListItem>
<asp:ListItem Value="8">8</asp:ListItem>
<asp:ListItem Value="9">9</asp:ListItem>
<asp:ListItem Value="10">10</asp:ListItem>
</asp:DropDownList>
Here I want to pass the selected value to the content page named Checkavailablity.aspx.
Please give your ideas.
To get master page control values in content page, use Master.FindControl().
Add the following code in the Content Page.
Code
DropDownList masterDropDown = (DropDownList)Master.FindControl("ddladults");
String selectedValue=masterDropDown.SelectedItem.Text.ToString();
OR
DropDownList masterDropDown = (DropDownList)Master.FindControl("ddladults");
String selectedValue=masterDropDown.SelectedValue.ToString();
Then use that String selectedValue for whatever you want to do with.
I have a web form in which there are some controls along with dropdown list as below:
<asp:DropDownList ID="ddlrange" CssClass="dropdownstate" style="position:absolute;top:224px; left:200px;" runat="server" >
<asp:ListItem >Select Range</asp:ListItem>
<asp:ListItem Value="A" >A</asp:ListItem>
<asp:ListItem Value="B" >B</asp:ListItem>
<asp:ListItem Value="C" >C</asp:ListItem>
<asp:ListItem Value="D" >D</asp:ListItem>
</asp:DropDownList>
and theres a gridview in which data is shown on btninsert click.
and I have a link button EDIT. on clicking edit the corresponding values are displayed in their respective controls.
Now, coming to the point,insert works fine, but when i click on Edit link every thing is fine except the value in dropdown list.
it should be like as shown in screenshot below:
but what I get is shown below:
how can I overcome this???
Try giving value to the Select Range list item and then chcek, may be that helps.
Maybe this is helpful for you
<asp:DropDownList ID="ddlrange" CssClass="dropdownstate" style="position:absolute;top:224px; left:200px;" runat="server" >
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="" >Select Range</asp:ListItem>
<asp:ListItem Value="A" >A</asp:ListItem>
<asp:ListItem Value="B" >B</asp:ListItem>
<asp:ListItem Value="C" >C</asp:ListItem>
<asp:ListItem Value="D" >D</asp:ListItem>
</asp:DropDownList>
In edit click event add a "Select Range" value into the drop down.
ddlRange.Items.Insert (0,"Select Range");
Add <asp:ListItem Value=null>Select Range</asp:ListItem> line.
Try Below:
<asp:DropDownList ID="ddlrange" CssClass="dropdownstate" style="position:absolute;top:224px; left:200px;" runat="server" >
<asp:ListItem Value="Select Range">Select Range</asp:ListItem>
<asp:ListItem Value="A" >A</asp:ListItem>
<asp:ListItem Value="B" >B</asp:ListItem>
<asp:ListItem Value="C" >C</asp:ListItem>
<asp:ListItem Value="D" >D</asp:ListItem>
</asp:DropDownList>
I tried using ddlrange.SelectedValue instead of ddlrange.SelectedItem.Text, and issue is solved.
Thanks everyone for your efforts, I appreciate.