Asp.net Dropdownlist selected index in changed

S

Santosh

Dear all ,

i am writting following code.

if(Page.IsPostBack==false)
{
try
{

BindSectionDropDownlist();
BindSubSectionDropDownlist();
BindSectionLineDropDownlist();
}

catch(Exception ex)
{
Response.Write("<script language=Javascript>alert('"+
ex.Message.ToString()+"');</script>");
}
}
}


// Defination of BindSectionDropDownList


private void BindSectionDropDownlist()
{
DataView dv = new DataView();
dv = objSectionMaster.GetSectionRecForReports();
reportCategory_Dropdownlist.DataTextField="SectionDesc";
reportCategory_Dropdownlist.DataValueField ="sectionId";
reportCategory_Dropdownlist.DataSource=dv;
reportCategory_Dropdownlist.DataBind();
reportCategory_Dropdownlist.Items.Insert(0,new
ListItem("--Select--","--Select--"));

//reportCategoty_DropDownList.SelectedIndex=-1;
}


private void BindSubSectionDropDownlist()
{
DataView dv = new DataView();
dv = objSectionMaster.GetSubSectionRecReports();
subSection_Dropdownlist.DataTextField="SectionDesc";
subSection_Dropdownlist.DataValueField ="sectionId";
subSection_Dropdownlist.DataSource=dv;
subSection_Dropdownlist.DataBind();
subSection_Dropdownlist.Items.Insert(0,new
ListItem("--Select--","--Select--"));

//reportCategoty_DropDownList.SelectedIndex=-1;
}


private void BindSectionLineDropDownlist()
{

DataView dv = new DataView();
dv = objSectionMaster.GetSectionLineRecReports();
sectionLineDesc_DropDownList.DataTextField="SectionLineDescription";
sectionLineDesc_DropDownList.DataValueField ="SectionLineId";
sectionLineDesc_DropDownList.DataSource= dv;
sectionLineDesc_DropDownList.DataBind();

sectionLineDesc_DropDownList.Items.Insert(0,new
ListItem("--Select--","-1"));

//reportCategoty_DropDownList.SelectedIndex=-1;
}

private void search_Button_Click(object sender, System.EventArgs e)
{
selectQuery();
}

selectQuery();
{

//Code for further processing of on the basis of
sectionLineDesc_DropDownList selection


}

selection index of first two dropdownlist is not changed but
the third selected index of third dropdownlist is changed
when i am click on search button.


any help will be greatly appreciated.
 
T

Tim_Mac

hi,
first it would be useful to describe what you are trying to do. code is
helpful but it's no good unless you describe what you are trying to do with
it!

you say that the selected index of the first two dropdownlists are not
changed, are they supposed to change? in response to the user? or your
code?
you have this line of code in each bind method:
//reportCategoty_DropDownList.SelectedIndex=-1;
is it supposed to be uncommented?

happy to help if you can provide more info
tim
 
S

Santosh

The description is ,

i am having three dropdownlists when i am selected first item from list
it will display all related items in the next drop down list when i am
choose item from second drop down list in third all items filled
related to second item selection. and in the selection of third
dropdown list item text i will be display result.

eg.i am having first dropdownlist it will contains all country list
when i am choose one country it will display all states related to that
country. if i am selected state from list it will display all districts
related to the selected states. and on the selection of district i will
be search another information.
 
T

Tim_Mac

typically this is done via AutoPostBack dropdownlists. it seems strange
that you bind all 3 lists on the first Page_Load, since you can't know what
to populate lists 2 and 3 with until a selection has been made with list 1.

normally i would do something like this (pseudo-code):
First Page_Load (!PostBack) > Bind List 1 (lists 2 and 3 remain empty)
List1_SelectedIndex_Changed > Bind List 2
List2_SelectedIndex_Changed > Bind List 3
List3_SelectedIndex_Changed > Search whatever

have you got AutoPostBack=true on all the lists? also, you haven't posted
the SelectedIndexChanged events. it's very difficult to piece together your
situation with only a part of the code. if you post all the relevant code i
will be happy to take a look again.

tim
 
S

Santosh

Here is code ..


private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!Page.IsPostBack)
{
try
{
BindSectionDropDownlist();
BindSubSectionDropDownlist();
BindSectionLineDropDownlist();
}

catch(Exception ex)
{
Response.Write("<script language=Javascript>alert('"+
ex.Message.ToString()+"');</script>");
}
}
}


private void BindSectionDropDownlist()
{
DataView dv = new DataView();
dv = objSectionMaster.GetSectionRecForReports();

section_DropDownList.DataTextField="SectionDesc";
section_DropDownList.DataValueField ="sectionId";
section_DropDownList.DataSource=dv;
section_DropDownList.DataBind();
section_DropDownList.Items.Insert(0,new
ListItem("--Select--","--Select--"));

}


private void BindSubSectionDropDownlist()
{
DataView dv = new DataView();
dv = objSectionMaster.GetSubSectionRecReports();
subSection_DropDownList.DataTextField="SectionDesc";
subSection_DropDownList.DataValueField ="sectionId";
subSection_DropDownList.DataSource=dv;
subSection_DropDownList.DataBind();
subSection_DropDownList.Items.Insert(0,new
ListItem("--Select--","--Select--"));

}



private void BindSectionLineDropDownlist()
{

DataView dv = new DataView();
dv = objSectionMaster.GetSectionLineRecReports();
sectionLine_DropDownList.DataTextField="SectionLineDescription";
sectionLine_DropDownList.DataValueField ="SectionLineId";
sectionLine_DropDownList.DataSource= dv;
sectionLine_DropDownList.DataBind();

sectionLine_DropDownList.Items.Insert(0,new
ListItem("--Select--","-1"));


}

private void section_DropDownList_SelectedIndexChanged(object sender,
System.EventArgs e)
{
try
{
DataSet ds = new DataSet();
ds =
objSectionMaster.GetSubSectionRecForReports1(Convert.ToInt32(section_DropDownList.SelectedValue));
subSection_DropDownList.DataTextField="SectionDesc";
subSection_DropDownList.DataValueField ="SectionId";
subSection_DropDownList.DataSource=ds;
subSection_DropDownList.DataBind();


}
catch(Exception ex)
{
Response.Write("<script language=javascript> alert('"+
ex.Message.ToString()+"');</script>");
}



private void subSection_DropDownList_SelectedIndexChanged(object
sender, System.EventArgs e)
{
try
{
if(subSection_DropDownList.SelectedValue=="--Select--")
{
return;
}

DataView dv = new DataView();
dv =
objSectionMaster.GetSectionLinesRecForReports(Convert.ToInt32(subSection_DropDownList.SelectedValue));
sectionLine_DropDownList.DataTextField="SectionLineDescription";
sectionLine_DropDownList.DataValueField ="SectionId";
sectionLine_DropDownList.DataSource=dv;
sectionLine_DropDownList.DataBind();

}
catch(Exception ex)
{
Response.Write("<script language=javascript> alert('"+
ex.Message.ToString()+"');</script>");
}
}


private void search_Button_Click(object sender, System.EventArgs e)
{
SearchCondition();
}


Private void SearchCondition ();
{
Code for Searching according to the selection of third dropdown
list.
}


Regards

Shinde Santosh.
 
T

Tim_Mac

hi Santosh,
your code looks normal. in your original post, you say

"selection index of first two dropdownlist is not changed but
the third selected index of third dropdownlist is changed
when i am click on search button."

i am assuming that you have EnableViewStated = true and AutoPostBack=true
set on all 3 drop down lists, and that the SelectedIndexChanged events are
correctly registered for all 3 menus, and that these events are firing
correctly.
if this is the case then when you change List1, it will postback and the
selectedIndex of List1 should stay on what you selected. It should also
stay for all future postbacks unless you re-bind the list. Does this
happen?

when you click on the search button, the SelectedIndex of all 3 lists should
be the same as you left it. from the description of your steps below, can
you say exactly where your code behaves unexpectedly:

Step 1: change List1
Step 2: change List2
Step 3: change List3
Step 4: click search.

tim
 
S

Santosh

Dear Tim

The selected index of first two lists remain same but only thirds are
change it change on
change List3 and and click search also.

please help me. what exactely wrong happen.


regards
Santosh
 
T

Tim_Mac

hi Santosh,
when you say the index remains the same, do you mean when you change the
index to the 5th item (for example) that it stays on the 5th item after the
SelectedIndexChanged event? or that it stays on the first 'Select...' item
after the event?

tim
 
S

Santosh

hi Tim_Mac

it is not like that say
if i select fifth item from the third list then after page is post
back it will lose selection from fifth to first item in the list.
 
T

Tim_Mac

hi Santosh,
it is difficult to follow your situation. you say that after you change
List3 that it resets the index back to 0 when the page posts back. am i
right in saying that the postback is because List3 has AutoPostBack=true?
the reason i ask is because you don't appear to have a SelectedIndexChanged
event for List3. so i'm not sure why you would want List3 to post back
itself.

i hope you can clarify the above question. also, please post your aspx code
with the 3 lists, and your InitialiseComponent code to make sure you have
wired the events correctly with the menus.

tim
 
S

Santosh

Hii Tim_Mac
I am trying both options if i am set autopostback=false to third list
then also the selection is changed. when page is get postback. and when
i am set autopostback=true and writting code to selectionchanged for
capturing selected value so third list in this case also value is get
changed.

again i am giving total code to you.



private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

if(!Page.IsPostBack)
{
try
{
BindDistrictList();
BindSDOList();
BindTalukaList();
BindCircleList();
BindVillageList();
BindSectionDropDownlist();
BindSubSectionDropDownlist();
BindSectionLineDropDownlist();

}

catch(Exception ex)
{
Response.Write("<script language=Javascript>alert('"+
ex.Message.ToString()+"');</script>");
}
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.village_Res_DropDownList.SelectedIndexChanged += new
System.EventHandler(this.village_Res_DropDownList_SelectedIndexChanged);
this.go_Button.Click += new
System.EventHandler(this.go_Button_Click);
this.section_DropDownList.SelectedIndexChanged += new
System.EventHandler(this.section_DropDownList_SelectedIndexChanged);
this.subSection_DropDownList.SelectedIndexChanged += new
System.EventHandler(this.subSection_DropDownList_SelectedIndexChanged);
this.sectionLine_DropDownList.SelectedIndexChanged += new
System.EventHandler(this.sectionLine_DropDownList_SelectedIndexChanged);
this.district_DropDownList.SelectedIndexChanged += new
System.EventHandler(this.district_DropDownList_SelectedIndexChanged);
this.SDO_DropDownList.SelectedIndexChanged += new
System.EventHandler(this.SDO_DropDownList_SelectedIndexChanged);
this.circle_DropDownList.SelectedIndexChanged += new
System.EventHandler(this.circle_DropDownList_SelectedIndexChanged);
this.village_DropDownList.SelectedIndexChanged += new
System.EventHandler(this.village_DropDownList_SelectedIndexChanged);
this.search_Button.Click += new
System.EventHandler(this.search_Button_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void BindSectionDropDownlist()
{
DataView dv = new DataView();
dv = objSectionMaster.GetSectionRecForReports();

section_DropDownList.DataTextField="SectionDesc";
section_DropDownList.DataValueField ="sectionId";
section_DropDownList.DataSource=dv;
section_DropDownList.DataBind();
section_DropDownList.Items.Insert(0,new
ListItem("--Select--","--Select--"));


}


private void BindSubSectionDropDownlist()
{
DataView dv = new DataView();
dv = objSectionMaster.GetSubSectionRecReports();
subSection_DropDownList.DataTextField="SectionDesc";
subSection_DropDownList.DataValueField ="sectionId";
subSection_DropDownList.DataSource=dv;
subSection_DropDownList.DataBind();
subSection_DropDownList.Items.Insert(0,new
ListItem("--Select--","--Select--"));


}



private void BindSectionLineDropDownlist()
{

DataView dv = new DataView();
dv = objSectionMaster.GetSectionLineRecReports();
sectionLine_DropDownList.DataTextField="SectionLineDescription";
sectionLine_DropDownList.DataValueField ="SectionLineId";
sectionLine_DropDownList.DataSource= dv;
sectionLine_DropDownList.DataBind();

sectionLine_DropDownList.Items.Insert(0,new
ListItem("--Select--","-1"));


}





private void section_DropDownList_SelectedIndexChanged(object sender,
System.EventArgs e)
{
try
{

DataSet ds = new DataSet();
ds =
objSectionMaster.GetSubSectionRecForReports1(Convert.ToInt32(section_DropDownList.SelectedValue));

subSection_DropDownList.DataTextField="SectionDesc";
subSection_DropDownList.DataValueField ="SectionId";
subSection_DropDownList.DataSource=ds;
subSection_DropDownList.DataBind();

}
catch(Exception ex)
{
Response.Write("<script language=javascript> alert('"+
ex.Message.ToString()+"');</script>");
}


}


private void subSection_DropDownList_SelectedIndexChanged(object
sender, System.EventArgs e)
{
try
{
if(subSection_DropDownList.SelectedValue=="--Select--")
{
return;
}

DataView dv = new DataView();
dv =
objSectionMaster.GetSectionLinesRecForReports(Convert.ToInt32(subSection_DropDownList.SelectedValue));
sectionLine_DropDownList.DataTextField="SectionLineDescription";
sectionLine_DropDownList.DataValueField ="SectionId";
sectionLine_DropDownList.DataSource=dv;
sectionLine_DropDownList.DataBind();
//resource_Label.Text = sectionLine_DropDownList.SelectedItem.Text;
//sectionLine_DropDownList.Items.Insert(0,new
ListItem("--Select--","--Select--"));
}
catch(Exception ex)
{
Response.Write("<script language=javascript> alert('"+
ex.Message.ToString()+"');</script>");
}
}
 
T

Tim_Mac

hi Santosh,
what i was interested in was the aspx which i don't think you sent yet, but
no matter, i have written a simple reproduction of your scenario which works
correctly. i will leave it up to you to compare what is different in your
own situation.

aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs"
Inherits="TestDdl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
<HEAD id="Head1">
<title>Untitled Page</title>
</HEAD>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddl1" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ddl1_SelectedIndexChanged"></asp:DropDownList>
<asp:DropDownList ID="ddl2" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ddl2_SelectedIndexChanged"></asp:DropDownList>
<asp:DropDownList ID="ddl3" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ddl3_SelectedIndexChanged"></asp:DropDownList>&nbsp;
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Search" />
<br>
<br>
<asp:Label ID="Label1" runat="server"></asp:Label></div>
</form>
</body>
</HTML>



code behind:

using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class TestDdl : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList ddl1;
protected System.Web.UI.WebControls.DropDownList ddl2;
protected System.Web.UI.WebControls.DropDownList ddl3;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
// bind list 1
this.ddl1.DataSource = GetRandomDataSource();
this.ddl1.DataBind();
this.ddl1.Items.Insert(0, new ListItem("Select...", ""));

// disable the textbox and button until all menus have been selected
this.TextBox1.Enabled = false;
this.Button1.Enabled = false;
}
}
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
// bind list 2
this.ddl2.DataSource = GetRandomDataSource();
this.ddl2.DataBind();
this.ddl2.Items.Insert(0, new ListItem("Select...", ""));
}
protected void ddl2_SelectedIndexChanged(object sender, EventArgs e)
{
// bind list 3
this.ddl3.DataSource = GetRandomDataSource();
this.ddl3.DataBind();
this.ddl3.Items.Insert(0, new ListItem("Select...", ""));
}
protected void ddl3_SelectedIndexChanged(object sender, EventArgs e)
{
// enable textbox + search button
this.TextBox1.Enabled = true;
this.Button1.Enabled = true;
}
protected void Button1_Click(object sender, EventArgs e)
{
this.Label1.Text = String.Format("You searched for '{0}' with {1} in list
1, {2} in list 2, and {3} in list 3", this.TextBox1.Text,
this.ddl1.SelectedValue, this.ddl2.SelectedValue, this.ddl3.SelectedValue);
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

private ArrayList GetRandomDataSource()
{
ArrayList arr = new ArrayList();
Random r = new Random();
for(int i = 0; i < 10; i++)
arr.Add(r.Next(9999));
return arr;
}
}


the most likely explanation is that you have EnableViewState=False on one of
your lists, this is why i wanted to see the aspx.

good luck, and i hope this helps.
tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top