Dropdown list,Databound ,Unable to select an item in list after databinding

Posted by Raja_89 under C# on 9/8/2016 | Points: 10 | Views : 1351 | Status : [Member] | Replies : 4
Hai

I am having three dropdownlist(Type(Domestic,International),Country ,State)

Based on type(Domestic/international) country will be loaded and based on country state will be loaded
Here if i choose type(International/Domestic) country data is loaded by setting datasource as datatable (Declared in class) with a default value

If i try to select an item in list it will the default item is always selected

While try to get the selected value for db it will set as Default value

How can i get the selected value to db for saving

Example:
Type:International

Country
Select
USA
UK
Indonesia

<asp:DropDownList ID ="DrpDownorigincountry" runat ="server"  AutoPostBack ="true"  AppendDataBoundItems ="true"    OnSelectedIndexChanged="DrpDownorigincountry_SelectedIndexChanged" >

</asp:DropDownList>



Always selected value wiil be select eventhough others are selected

Sample Code
DAL.objresdtfordisplay = new System.Data.DataTable();
DAL.objresdtfordisplay = Rateobjdc.Exec_Select("select Distinct(tblgeoloc_country) from tbl_geographiclocation where tblgeoloc_modeoftransport='" + Transmode + "' and tblgeoloc_transportype ='" + Transtype + "'");
if ((DAL.objresdtfordisplay.Rows.Count > 0) && (DAL.objresdtfordisplay != null))
{

DrpDownorigincountry.DataSource = DAL.objresdtfordisplay;
DrpDownorigincountry.DataTextField = "tblgeoloc_country";
DrpDownorigincountry.DataValueField = "tblgeoloc_country";
DrpDownorigincountry.DataBind();

DrpDownorigincountry.Items.Insert(0, "Select");

}



How can i fire the correponding event


Regards




Responses

Posted by: Rajnilari2015 on: 9/8/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Please check if OnSelectedIndexChanged event is properly getting fired or not by setting break point. Hope that helps

--
Thanks & Regards,
RNA Team

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

Posted by: Raja_89 on: 9/8/2016 [Member] Starter | Points: 25

Up
0
Down
Hai Rajnilari

I checked by keeping breakpoing in that place it cannot enter

How can we get the solution

Here Selectedval is global vairable



String str;


protected void DrpDestinationcity_SelectedIndexChanged(object sender, EventArgs e)
{
if (DrpDestinationcity .SelectedIndex !=-1)
{
Selectedval= DrpDestinationcity.SelectedValue;
}
}



Regards

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

Posted by: A2H on: 9/8/2016 [Member] [MVP] Silver | Points: 25

Up
0
Down
If you are loading data in 'DrpDownorigincountry' dropdownlist on pageload event then you need to ensure that you load data on if(!IsPostback) block

Thanks,
A2H
My Blog

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

Posted by: Raja_89 on: 9/9/2016 [Member] Starter | Points: 25

Up
0
Down
Here is my complete code in page_Load

 if (IsPostBack==true  )

{

if ((DDTransportmode .SelectedIndex !=-1)&&(DDlRTrstype .SelectedIndex !=-1)&&(DDLoriginCountry .SelectedIndex !=-1))
{
LoadOriginCity(DDTransportmode.SelectedValue, DDlRTrstype.SelectedValue, DDLoriginCountry.SelectedValue);
}
else
{
lblShowratemsg.Text = "Select necessary details";
}
// LoadCountryInfo();
}
else
{
if (!IsPostBack )
{
DDLoriginCity.AppendDataBoundItems = true;
DAL.objresdtfordisplay = new System.Data.DataTable();
string query="select tblgc from tblemp";
DAL.objresdtfordisplay =Rateobjdc .Exec_Select (query );
DDLGc.Items.Clear();
DDLGc.DataSource = DAL.objresdtfordisplay;
DDGC.DataValueField ="tblgc";
DDGC.DataTextField = "tblgc;
DDGC.DataBind();
}
}
}


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

Login to post response