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.
Related
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";
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 as following
<asp:DropDownList ID="ddlRoles" runat="server" AutoPostBack="True" Width="150px">
<asp:ListItem Value="9" Text=""></asp:ListItem>
<asp:ListItem Value="0">None</asp:ListItem>
<asp:ListItem Value="1">Ranu</asp:ListItem>
<asp:ListItem Value="2">Mohit</asp:ListItem>
<asp:ListItem Value="3">Kabeer</asp:ListItem>
<asp:ListItem Value="4">Jems</asp:ListItem>
<asp:ListItem Value="5">Jony</asp:ListItem>
<asp:ListItem Value="6">Vikky</asp:ListItem>
<asp:ListItem Value="7">Satish</asp:ListItem>
<asp:ListItem Value="8">Rony</asp:ListItem>
</asp:DropDownList>
I want to select Multiple name at once suppose I want to select Ranu Mohit or Ranu Kabeer Vikky, How its possible?
asp:DropDownList does not support multiple selects:
VerifyMultiSelect():
Always throws an HttpException exception because multiple selection is not supported for the DropDownList control.
You can use asp:ListBox with SelectionMode="Multiple" instead:
<asp:ListBox SelectionMode="Multiple" ID="lbRoles" runat="server" AutoPostBack="True" Width="150px">
<asp:ListItem Value="9" Text=""></asp:ListItem>
<asp:ListItem Value="0">None</asp:ListItem>
<asp:ListItem Value="1">Ranu</asp:ListItem>
<asp:ListItem Value="2">Mohit</asp:ListItem>
<asp:ListItem Value="3">Kabeer</asp:ListItem>
<asp:ListItem Value="4">Jems</asp:ListItem>
<asp:ListItem Value="5">Jony</asp:ListItem>
<asp:ListItem Value="6">Vikky</asp:ListItem>
<asp:ListItem Value="7">Satish</asp:ListItem>
<asp:ListItem Value="8">Rony</asp:ListItem>
</asp:ListBox>