Dropdownlist Selected Item Appears Twice---Urgent help needed.

Posted by Ogipansrk under ASP.NET on 12/10/2011 | Points: 10 | Views : 8169 | Status : [Member] | Replies : 18
I have a dropdownlist in asp.net , which is populated from database.

If a select an item from dropdown it appears tiwce.

country
---------
chooseone----------selected value
russia
germany
India
chooseone

pls find the attached image.




Responses

Posted by: Hmanjarawala on: 12/10/2011 [Member] Bronze | Points: 25

Up
0
Down
Will you show me your code???

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Varung on: 12/10/2011 [Member] Starter | Points: 25

Up
0
Down
may be the property of dropdownlist AppendDataBoundItems is true

G.Varun

Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Krishna17117 on: 12/10/2011 [Member] Starter | Points: 25

Up
0
Down
in your command simply use DISTINCT
EX:
I have Country State CIty in a Table Address

now use Like Below
SELECT DISTINCT COUNTRY FROM Address

if my suggestion helps say thanks

Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ogipansrk on: 12/10/2011 [Member] Starter | Points: 25

Up
0
Down
This is my asp.net simple code....

<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Select One" Selected="True" />
<asp:ListItem Text="India" />
<asp:ListItem Text="Germany" />
<asp:ListItem Text="Russia" />
</asp:DropDownList>

My requirment is : when I select an item from the dropdownlist , the selected item appears twice.
Is there a way to restrict this kind of display using css or jquery or through code.

example: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_option

on right side , select VOLVO , imediately we see another one.




Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Krishna17117 on: 12/10/2011 [Member] Starter | Points: 25

Up
0
Down
where it displays twice in dropdownlist or some where because from your code you only added the values manually through smarttag in dropdownlist so no question of repitation

Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ogipansrk on: 12/10/2011 [Member] Starter | Points: 25

Up
0
Down
I think confused with my question

Let me reiterate cleary:

<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Select One" Selected="True" />
<asp:ListItem Text="India" />
<asp:ListItem Text="Germany" />
<asp:ListItem Text="Russia" />
</asp:DropDownList>

Please paste this in an empty webform.
Now you will be displayed with a dropdown , with default selecteditem ="Select One";
Now problem starts here : now select "INDIA" and leave it.
Now again try to expand dropdown....here you can see INDIA twice ......Html shows what we selected plus the item in list.
please see the attached doc so u can understand.




Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kamlesh420 on: 12/10/2011 [Member] Starter | Points: 25

Up
0
Down
I think write this code within ispostback = false statement

ispostback = false statement
{
filldropsdwon()
}

kamlesh kuamr saini
Sr. devloper (asp.net)
new delhi

Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 12/10/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi,


i think u set ur drop-down Property . . AutoPostBack="True" . try it . .

Best Regard's
Prabhakar

Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bees7000 on: 12/10/2011 [Member] Starter | Points: 25

Up
0
Down
put your method to papulate dropdownlist in this

if(!isPostPack)
{
// Your Method Or Code
}

Marwan

Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ogipansrk on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
Guys, Pls see the attached doc , so that u can get clear idea.
Its not coding issue.

Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ogipansrk on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
Its an Html rendring issue.

Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Manikattamuri on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
Try this one .

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem Text="Select One" Selected="True" />
<asp:ListItem Text="India" />
<asp:ListItem Text="Germany" />
<asp:ListItem Text="Russia" />
<asp:ListItem Text="USA" />
<asp:ListItem Text="africa" />
<asp:ListItem Text="Australia" />
</asp:DropDownList>

if (!IsPostBack)
{
// Some Method
}

Thanks
ManiDeep.K

Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-innovator on: 12/26/2011 [Member] Bronze | Points: 25

Up
0
Down
clear ur ddl control and then bind it once again..
dropdownlist1.items.clear();
dropdownlist1.databind();

Join Hands Change lives
Thanks & Regards
Straight Edge Society

Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: RohiK on: 12/26/2011 [Member] Starter | Points: 25

Up
0
Down
Check what appears in your datasource wich dropdown is binded.
If there records comes twice then you must forgot to distinctly select rows .
Otherwise check dropdown in aspx page

Happy Programming!!
Rohi

Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saisindura on: 12/26/2011 [Member] Starter | Points: 25

Up
0
Down
if (!IsPostBack)
{
// Some Method
}
This one is correct answer

Thanks&Regards
srisai

Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 12/26/2011 [Member] Silver | Points: 25

Up
0
Down



create table CountryMaster
(CountryID int,CountryName varchar(100))
go
insert into CountryMaster values(1,'India')
insert into CountryMaster values(2,'Germany')
insert into CountryMaster values(3,'Russia')

go

alter procedure [dbo].[CountryComboData]
as

select CountryID ,CountryName from CountryMaster
union
select CountryID=0,CountryName='-- Select --'


-------------
ASPX
---------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dropDownTest.aspx.cs" Inherits="dropDownTest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

</div>
</form>
</body>
</html>
----------------------------
web.config
--------------------
<?xml version="1.0"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
<appSettings>
<add key="RMSConnectionString" value="Data Source=localhost;Database=Forum;uid=sa;pwd=xxxxxx; pooling=false;Connect timeout=60000" />

</appSettings>
<connectionStrings/>
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true">
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
</configuration>


------------
ASPX.CS
---------------

using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class dropDownTest : System.Web.UI.Page
{
public string thisConnectionString = ConfigurationManager.AppSettings["RMSConnectionString"];

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.DropDownList1.Focus();
getCountries();
}
}
protected void getCountries()
{

DataSet ds = new DataSet();
ds = spDataset("[CountryComboData]");
this.DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataTextField = "CountryName";
DropDownList1.DataValueField = "CountryID";
DropDownList1.DataBind();

}
public DataSet spDataset(string sp_name)
{

SqlDataAdapter dAd = new SqlDataAdapter(sp_name, thisConnectionString);
dAd.SelectCommand.CommandType = CommandType.StoredProcedure;


DataSet dSet = new DataSet();
try
{
dAd.Fill(dSet);
return dSet;
}
catch
{
throw;
}
finally
{
dSet.Dispose();
dAd.Dispose();
//thisConnectionString.Close();
//thisConnectionString.Dispose();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.Label1.Text = DropDownList1.SelectedItem.Text;
}
}
------------------------------------------------------------------
Try This.....


Thanks,
Sanjay

Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Feejaz on: 12/27/2011 [Member] Starter | Points: 25

Up
0
Down
Hi All,

Question is too much clear from Ogipansrk
**************************************************************
I think confused with my question

Let me reiterate cleary:

<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Select One" Selected="True" />
<asp:ListItem Text="India" />
<asp:ListItem Text="Germany" />
<asp:ListItem Text="Russia" />
</asp:DropDownList>

Please paste this in an empty webform.
Now you will be displayed with a dropdown , with default selecteditem ="Select One";
Now problem starts here : now select "INDIA" and leave it.
Now again try to expand dropdown....here you can see INDIA twice ......Html shows what we selected plus the item in list.
please see the attached doc so u can understand.
**************************************************************

just do the above and you will get the issue. Infact it is replacing 0 index value with the selected value, therefore it is showing the selected value twice. For example if selected the "India", It is on 1st index, then it will replace the 0 index value "Select One" with the "India" and "India" is also available on the 1st Index, therefore it is showing the selected value twice.

I think it will make more sense.

Awaiting reply,

thanks

feejaz

Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saisindura on: 12/27/2011 [Member] Starter | Points: 25

Up
0
Down
hi
try to attach total application.Iam not getting what exactly you asking.

Thanks&Regards
srisai

Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response