I have a dropdown list populated from the database. There is also a button next to the dropdownlist that once clicked on. It takes the item in the dropdownlist and use that to populate the gridview.however though, I would like to add a blank to the dropdownlist
and message that would say, you have selected a blank whenever the user selects the blank from the dropdownlist and clicks on the button.
for an asp dropdown. Then use a javascript function to popup a message.
function blankValue() {
var ddl = document.getElementById('<%=DropDownList1.ClientID%>');
if(ddl.value = " ")
{
alert("You have selected a blank value.");
}
}
Im not sure if it would be ddl.value or ddl.innerText or something like that, you'll have to look through the available values. You can then call this function in the onchange event.
Did you try the same what I wrote ? It is working at my end. My guess is that you would be using script tag above so you need to wrap it in $(function()........ try this then
All you have to do is Add a list item to your dropdown for example considering you are using visual studio to build your page
<asp:DropDownList ID="ID"
runat="server"
DataSourceID="Data source if needed"
DataTextField="Text Value"
DataValueField="Field Value"
AppendDataBoundItems="This could be either set to true or false">
olybobo
Member
401 Points
497 Posts
adding a blank to my dropdown list
Jun 29, 2010 10:09 PM|LINK
Dear all;
I have a dropdown list populated from the database. There is also a button next to the dropdownlist that once clicked on. It takes the item in the dropdownlist and use that to populate the gridview.however though, I would like to add a blank to the dropdownlist and message that would say, you have selected a blank whenever the user selects the blank from the dropdownlist and clicks on the button.
raghav_khung...
All-Star
32835 Points
5563 Posts
MVP
Re: adding a blank to my dropdown list
Jun 29, 2010 10:57 PM|LINK
Hi olybobo,
Try this sample :
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> </head> <body> <form runat="server"> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Text="--Please Select" Value="0"></asp:ListItem> <asp:ListItem Text="Text1" Value="1"></asp:ListItem> <asp:ListItem Text="Text2" Value="2"></asp:ListItem> <asp:ListItem Text="Text3" Value="3"></asp:ListItem> <asp:ListItem Text="Text4" Value="4"></asp:ListItem> </asp:DropDownList> <br /> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <br /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> </form> </body> <script type="text/javascript"> $('#<%= Button1.ClientID %>').click(function (e) { if ($('#<% = DropDownList1.ClientID%>').val() == 0) { $('#<%= Label1.ClientID %>').html('Please select an option'); e.preventDefault(); } else { $('#<%= Label1.ClientID %>').html(''); } }); </script> </html>snagpoonage
Participant
1850 Points
471 Posts
Re: adding a blank to my dropdown list
Jun 29, 2010 11:01 PM|LINK
You could use
for an asp dropdown. Then use a javascript function to popup a message.
function blankValue() { var ddl = document.getElementById('<%=DropDownList1.ClientID%>'); if(ddl.value = " ") { alert("You have selected a blank value."); } }Im not sure if it would be ddl.value or ddl.innerText or something like that, you'll have to look through the available values. You can then call this function in the onchange event.
raghav_khung...
All-Star
32835 Points
5563 Posts
MVP
Re: adding a blank to my dropdown list
Jun 29, 2010 11:17 PM|LINK
Olybobo, but I will not recommend blank value for DDL, you can keep it -1 or 0 which are kept as generally.
olybobo
Member
401 Points
497 Posts
Re: adding a blank to my dropdown list
Jun 30, 2010 03:12 AM|LINK
I have tried the above syntax given to me and none of them works.
see part of my aspx below
< script type="text/javascript">
$('#<%= Button3.ClientID %>').click(function (e) {
if ($('#<% = WorkCellDropDown.ClientID%>').val() == 0) {
$( '#<%= Label2.ClientID %>').html('Please select an option');
e.preventDefault();
}
else {
$('#<%= Label2.ClientID %>').html('');
}
});
</script>
asp:label ID = "Label2" runat="server" text="Label"></asp:label>body>
raghav_khung...
All-Star
32835 Points
5563 Posts
MVP
Re: adding a blank to my dropdown list
Jun 30, 2010 12:56 PM|LINK
Did you try the same what I wrote ? It is working at my end. My guess is that you would be using script tag above so you need to wrap it in $(function()........ try this then
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript"> $(function () { $('#<%= Button1.ClientID %>').click(function (e) { if ($('#<% = DropDownList1.ClientID%>').val() == 0) { $('#<%= Label1.ClientID %>').html('Please select an option'); e.preventDefault(); } else { $('#<%= Label1.ClientID %>').html(''); } }); }); </script> </head> <body> <form runat="server"> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Text="--Please Select" Value="0"></asp:ListItem> <asp:ListItem Text="Text1" Value="1"></asp:ListItem> <asp:ListItem Text="Text2" Value="2"></asp:ListItem> <asp:ListItem Text="Text3" Value="3"></asp:ListItem> <asp:ListItem Text="Text4" Value="4"></asp:ListItem> </asp:DropDownList> <br /> <asp:Button ID="Button1" runat="server" Text="Button" /> <br /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> </form> </body> </html>masterpage1
Member
418 Points
250 Posts
Re: adding a blank to my dropdown list
Jun 30, 2010 01:06 PM|LINK
All you have to do is Add a list item to your dropdown for example considering you are using visual studio to build your page
<asp:DropDownList ID="ID" runat="server" DataSourceID="Data source if needed" DataTextField="Text Value" DataValueField="Field Value" AppendDataBoundItems="This could be either set to true or false">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
Hope this helps
olybobo
Member
401 Points
497 Posts
Re: adding a blank to my dropdown list
Jun 30, 2010 07:08 PM|LINK
Thanks for the help raghav.