Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
i'll bind the data from databse to dropdownlist

like

C#
string query = "SELECT id code,country DESCRIPTION description FROM world";
            OracleDataAdapter adpt = new OracleDataAdapter(query, con);
            DataTable dt = new DataTable();
            adpt.Fill(dt);
            DataRow dr = dt.NewRow();
            dr["code"] = 0;
            dr["description"] = "-Select-";
            dt.Rows.InsertAt(dr, 0);
            ddlcounry.DataSource = dt;
            ddlcounry.DataBind();
            ddlcounry.DataTextField = "description";
            ddlcounry.DataValueField = "code";
            ddlcounry.DataBind();  



and in aspx page

XML
<asp:DropDownList ID="ddlcounry" runat="server" Style="font-size: medium; font-family: 'Times New Roman', Times, serif"
                        TabIndex="1" Width="158px" CssClass="Dropdown">
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="ddlcounry"
                        InitialValue="0" runat="server" ErrorMessage="Please select ddlcounry"></asp:RequiredFieldValidator>


when ever i click on savebtn if dropdownlist have -select- it will show alert message
please tell me thankssss
Posted
Updated 22-Feb-15 21:25pm
v8
Comments
[no name] 23-Feb-15 2:03am    
What u need ? do u wanted to ask "please select the data after the button click"?
Maciej Los 23-Feb-15 2:04am    
Not clear what you want to achieve...
Parazival 23-Feb-15 2:17am    
rajeesh

after button click if dropdownlist had -select- it will show Please select the data

ASP.NET
<asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" errormessage="Please select the data" controltovalidate="ddlcounry" initialvalue="-Select-" ></asp:requiredfieldvalidator>


In ur ddl code add this one :

C#
ddlcountry.Items.Insert(0, "-Select-");
 
Share this answer
 
v3
Comments
Parazival 23-Feb-15 3:13am    
k k when ever i click on save btn it will show alert box please send me the code also in .cs file
[no name] 23-Feb-15 3:34am    
which code? saving? after the save code u just put a lable.
Eg: lbl_msg.text = "saved";
Parazival 23-Feb-15 3:50am    
in my btn_click()
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
}
}

if i click on yes it will check dropdownlist
and if user select -Select- this it will show alert box ...
how to write the code for this
[no name] 23-Feb-15 3:54am    
When u click on the save button u wanted to show a confirm msg alert?
[no name] 23-Feb-15 4:05am    
if (ddlcountry.SelectedItem.ToString() == "-Select-")
{

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
}
Hi,

Try below solution:

ASPX HTML:
XML
<asp:DropDownList runat="server" ID="ddlList" CssClass="form-control"> </asp:DropDownList>
<asp:RequiredFieldValidator Display="Dynamic" ID="RequiredFieldValidator3" ErrorMessage="Please select from the list." InitialValue="" ControlToValidate="ddlList" runat="server" />


Code
SQL
ddlList.DataSource = objSource;
ddlList.DataTextField = "Description";
ddlList.DataValueField = "Code";
ddlList.DataBind();

ListItem liSelect = new ListItem();
liSelect.Text = "--- Please Select ---";
liSelect.Value = "";

ddlList.Items.Insert(0, liSelect);


This is fully working code.
 
Share this answer
 
v2
Comments
Parazival 23-Feb-15 3:31am    
haii dusara
thanksss
what is ddlgener
Dusara Maulik 23-Feb-15 3:34am    
Ohhh, use below instead:

ddlList.DataSource = objSource;
Parazival 23-Feb-15 3:44am    
in my btn_click()
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
}
}

if i click on yes it will check dropdownlist
and if user select -Select- this it will show alert box ...
how to write the code for this
Dusara Maulik 23-Feb-15 3:56am    
pls share what you had done in HTML and code behind.
Parazival 23-Feb-15 4:18am    
k k i got it thank u so much for ur help
set tht initialvalue ='-Select-*'
like below


XML
<asp:DropDownList ID="ddlcounry" runat="server" Style="font-size: medium; font-family: 'Times New Roman', Times, serif"
                        TabIndex="1" Width="158px" CssClass="Dropdown">
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="ddlcounry"
                        InitialValue="-Select-*" runat="server" ErrorMessage="Please select ddlcounry"></asp:RequiredFieldValidator>
 
Share this answer
 
Try this:
C#
<asp:requiredfieldvalidator id="RequiredFieldValidator1" controltovalidate="ddlcounry" xmlns:asp="#unknown">
                      InitialValue="0"  InitialValue="-Select-" runat="server" ErrorMessage="Please select ddlcounry"></asp:requiredfieldvalidator>


Your Initial value must be '-Select-'
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900