I have a dropdown it contain all country names.The code is working fine but I need to add a default choice in the dropdown.How to add a default choice?(<--choose-->).
my code is
<%# Control Language="C#" AutoEventWireup="true" CodeFile="ddlCountryofOrgin.ascx.cs" Inherits="UserControls_DataType_ddlCountryofOrgin" %>
<asp:DropDownList ID="ddlCountry" CssClass="ddlCountry" runat="server" DataValueField="english_name"
DataSourceID="sqlCountry" OnPreRender="ddlCountry_PreRender" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" />
<asp:SqlDataSource ID="sqlCountry" runat="server" SelectCommand="SELECT [country_id],[english_name], danish_name FROM [dbo].[nano_country] WHERE [is_active] = 1 ORDER BY [sort_order] DESC, [english_name] ASC" />
Thanks in advance for help...
Set AppendDataBoundItems as True
and add item to dropdownlist:
<asp:ListItem Selected="True" Value="0">--Choose--</asp:ListItem>
Code:
<asp:DropDownList ID="ddlCountry" CssClass="ddlCountry" runat="server" AppendDataBoundItems = "True" DataValueField="english_name"
DataSourceID="sqlCountry" OnPreRender="ddlCountry_PreRender" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" >
<asp:ListItem Selected="True" Value="0">--Choose--</asp:ListItem>
</asp:DropDownList >
protected void Page_Load(object sender, system.EventArgs as e)
{
ddlCountry.Items.Insert(0,"--Choose--");
}
Try this code
ddlCountry.Items.Insert(0, new ListItem("Select","--Choose--")
Related
I have a page with 1 main DropDownList and 2 other. Made it to choose settings for printing report.
The problem is - I use DataSources in these DropDownLists but they haven't an empty value. Where should I put adding a new empty value into DropDownList for correct work?
There is my code (in a comment is a thing that i need to add)
protected void ddlMain_Load(object sender, EventArgs e)
{
switch (ddlMain.SelectedValue)
{
case "1":
dFirst.Visible = true;
dSecond.Visible = false;
break;
case "2":
dFirst.Visible = false;
dSecond.Visible = true;
break;
}
<div>
<asp:DropDownList ID="ddlMain" runat="server" AutoPostBack="true" OnLoad="ddlMain_Load">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2 " Value="2"></asp:ListItem>
</asp:DropDownList>
</div>
<div runat="server" id="dFirst" visible="false">
<asp:DropDownList ID="ddlFirst" runat="server" DataSourceID="sdsFirst"
DataTextField="name" DataValueField="id" OnLoad="ddlFirst_Load">
</asp:DropDownList>
<asp:SqlDataSource ID="sdsFirst" runat="server"
ConnectionString="<%$ ConnectionStrings:conStr %>">
</asp:SqlDataSource>
</div>
//ddlFirst.Items.Add(new ListItem("--Select--", "-1"));
I tried OnLoad secondary element, OnLoad main DropDownList and Page_Load. They don't fit me because new ListItem don't appears when page firstly loading or added more than 1 time when page refreshes.
Use Insert method in page load.
ddlAcademicYear.Items.Insert(0, new ListItem("-- Select --", "-1"));
0 is position at which you want to insert.
Add an optionlabel to your dropdownlist and assign the text "--Select--". Your dropdownlist should now have the desired default value.
Hello and thanks for taking your time to read this.
I'm trying to change the CSS class of a panel thats located inside a Repeater when I select a RadioButton.
<div>
<asp:RadioButtonList OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" AutoPostBack="true" ID="RadioButtonList1" RepeatDirection="Horizontal" runat="server">
<asp:ListItem Selected="True">Show Gallery</asp:ListItem>
<asp:ListItem>Show List</asp:ListItem>
</asp:RadioButtonList>
</div>
<div class="RpOutterFrame" runat="server" id="RpOutterFrame">
<asp:Repeater runat="server" ID="RP">
<ItemTemplate>
<panel class="ShowDiv" runat="server" id="RpInnerFrame">
<img runat="server" style="width: 80px;" id="ModelImg" class="ModelImg" src='<%# string.Format("~/Content/Img/ModelImg/{0}", Eval("Image")) %>' />
<br />
<%# Eval("Model") %>
</panel>
</ItemTemplate>
</asp:Repeater>
</div>
My C#:
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadioButtonList1.Items[0].Selected == true)
{
RpOutterFrame.Attributes["class"] = "RpOutterFrame";
Panel panel = (Panel)this.FindControl("RpInnerFrame");
panel.CssClass = "ShowDiv2";
}
}
As you can see the Panel already has the class ShowDiv and then I would like it to change the class to ShowDiv2 when I select/click the Radiobutton.
Anyone who can help me figuar what I'm doing wrong or fix the code?
A Repeater's purpose is to repeat something. So normally it contains multiple elements. Therefore the RepeaterItem is the NamingContainer which must contain unqiue ID's and where you can find your controls via FindControl(ID).
So this does not work since this is the Page which is not the NamingContainer of the Panel:
Panel panel = (Panel)this.FindControl("RpInnerFrame");
panel.CssClass = "ShowDiv2";
You have to loop all items:
foreach(RepeaterItem item in RP.Items)
{
Panel panel = (Panel)item.FindControl("RpInnerFrame");
panel.CssClass = "ShowDiv2";
}
Apart from that you should use the ASP:Panel instead of Panel.
So change
<panel class="ShowDiv" runat="server" id="RpInnerFrame">
// ...
</panel>
to
<ASP:Panel CssClass="ShowDiv" runat="server" id="RpInnerFrame">
// ...
</ASP:Panel>
I need to add a condition to a DropDownList where a method can be executed by button click only if the user has selected a value different than the listItem (default value).
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
DataSourceID="SqlDataSource5"
DataTextField="proj_name" DataValueField="proj_name">
<asp:ListItem Text="Select a project to clone" Value="" />
</asp:DropDownList>
How can I structure an if condition to validate that the selected value is not the ListItem (default value)?
You can use asp.net delivered validation controls
Ex:
<asp:RequiredFieldValidator id="rfv1"
ControlToValidate="DropDownList1"
Display="Static"
ErrorMessage="* Select a value"
InitialValue="DefaultValueHere"
runat="server"
ValidationGroup="V1"/>
Then edit your button markup to use ValidationGroup
<asp:Button Id="button1" ValidationGroup="V1" .../>
In your codebehind button click code add this
protected void button1_onlick(Object sender, EventArgs e)
{
If(Page.IsValid)
{
// your existing code here
}
}
See sample code below
if (DropDownList1.SelectValue == "")
{
// Write your code here
}
you can also have:
if (DropDownList1.Text == "Select a project to clone")
{
// Write your code here
}
I added a DropDownList from the Toolbox to the Login page on a website I'm working on.
Once I choose a ListItem in the DropDownList (in my case lets say
Gym for example...), when clicked, I want that three bars we'll be opened below my DropDownList(for example, the bars that we'll be opened are Username, Password and ID), I mean three TextBoxes beneath each other.
I think you can either try the SelectedIndexChanged event or Javascript to display the textboxes without a postback.
<select>
<option value="1">Gym 1</option>
<option value="2">Gym 2</option>
<option value="3">Gym 3</option>
<select>
At firest you put your textboxes in a panle then hide this panel and
you should set Autopostback property of you drobdownlist to True and after selecting a item in DropDownList, postback will accur. So you can show that panel include text boxes.
What you might really be needing is dynamic text boxes.
In the html part:
<asp:DropDownList runat="server" ID="DDL1" autopostback = "true" onselectedindexchanged="DDL_SelectChanged" />
<asp:PlaceHolder runat="server" ID="PH1">
</asp:PlaceHolder>
In codebehind:
void DDL_SelectChanged(object sender, EventArgs e)
{
if (DDL1.SelectedIndex == 1)
{
for (int i = 0; i < 3; i++)
{
TextBox newTB = new TextBox();
newTB.ID = "TB" + i;
PH1.Controls.Add(newTB);
}
}
}
You could use MultiveView control. And set active view index in Dropdown's selectedIndexChanged event.
I wrote some example code for you:
ASPx side:
<asp:MultiView ID="multiView" ActiveViewIndex="-1" runat="server">
<asp:View ID="viewGym" runat="server">
<asp:TextBox ID="txtBxUserName" runat="server" />
<asp:TextBox ID="txtBxPassword" runat="server" />
<asp:TextBox ID="txtBxId" runat="server" />
</asp:View>
</asp:MultiView>
<asp:DropDownList ID="Dropdownlist1" runat="server" AutoPostBack="true"
onselectedindexchanged="Dropdownlist1_SelectedIndexChanged">
<asp:ListItem Text="Choose one club" Value="0" />
<asp:ListItem Text="Gym" Value="1" />
<asp:ListItem Text="Shoppers" Value="2" />
</asp:DropDownList>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
if ( IsPostBack ) //don't forget :)
return;
}
protected void Dropdownlist1_SelectedIndexChanged( object sender, EventArgs e )
{
if ( Dropdownlist1.SelectedValue == "1" ) //Gym item selected
{
multiView.ActiveViewIndex = 0; //Gym view active
}
}
If you want to any view not active when first page load then you set the ActiveViewIndex with -1 in aspx code.
I am trying to create a custom web control with a few controls on it, a textbox, label and dropdownlist, what I would like to do is add a property on the custom control to allow add select options on the dropdown list, the same way you would if it was just a normal dropdownlist i.e.
<asp:DropDownList ID="normalddl" runat="server">
<asp:ListItem Text="1st value" Value="0"></asp:ListItem>
<asp:ListItem Text="2nd value" Value="1"></asp:ListItem>
</asp:DropDownList>
I want a custom control that will look something like this (this is a simplified version)
<mycustomControl:ControlNamt ID="customddl" runat="server" >
<asp:ListItem Text="1st value" Value="0"></asp:ListItem> -- how would I go about adding this in the custom control?
<asp:ListItem Text="2nd value" Value="1"></asp:ListItem>
</mycustomControl:ControlNamt>
Not an answer you are looking for. Just a suggestion of how to achieve that by UserControls
UserControl markup:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="UC1.ascx.cs" Inherits="WebFormsScratch.UCTests.UC1" %>
<%-- Other controls here --%>
<asp:DropDownList runat="server" ID="ddl">
</asp:DropDownList>
UserControl code-behind:
public partial class UC1 : System.Web.UI.UserControl
{
public IEnumerable<ListItem> DDLData;
protected void Page_Load(object sender, EventArgs e)
{
}
public override void DataBind()
{
ddl.DataSource = DDLData;
ddl.DataBind();
base.DataBind();
}
}
aspx page using that control (mark up; bare minimum)
<%# Register Src="~/UCTests/UC1.ascx" TagPrefix="uc1" TagName="UC1" %>
...
...
<uc1:UC1 runat="server" ID="uc1" />
Code behind of aspx page using that control
protected void Page_Load(object sender, EventArgs e)
{
uc1.DDLData = new []
{
new ListItem("1st Item", "0"),
new ListItem("2nd Item", "1"),
new ListItem("3rd Item", "2"),
};
uc1.DataBind();
}