Setting Default

G

Guest

Hi experts.

I have an ASP.Net page with a DropDownList called cboYear. This gets filled
with a c# method as follows.

private void FillYearList() {
SqlConnection cn = new SqlConnection();
cn = DatEngine.TimeOffConn;
SqlCommand sc = new SqlCommand("SELECT intYear, Description FROM
dbo.vwYearSelector ",cn);
cn.Open();
SqlDataReader dr =sc.ExecuteReader();

//Set the values to be used from the database for the combo
cboYear.DataSource = dr; //attach Datareader
cboYear.DataValueField = "intYear"; //specify column that contains the
value to use
cboYear.DataTextField= "Description"; //specify column that contains the
value to see
cboYear.DataBind();
cn.Close(); //Must close this!!!!
dr.Close();
}

The data in vwYearSelector is

intYear Description
2004 2004
2005 2005/2006
2006 2006/2007
2007 2007/2008


All this worked fine in 2004, however now in 2005, the cboYear still
initially shows the 2004. How can I set the cboYear to now display 2005/2006
when the page first loads. 2004 could then be selected if required later.

Thanks
 
G

Guest

Hi jez,

Have you tried setting the SelectedIndex property of the DropDownList to 1
after you have done the databinding?
 
S

Shiva

Hi,

After databinding, have this:
cboYear.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = true;

This will select current year in the drop-down.

HTH.

Hi experts.

I have an ASP.Net page with a DropDownList called cboYear. This gets filled
with a c# method as follows.

private void FillYearList() {
SqlConnection cn = new SqlConnection();
cn = DatEngine.TimeOffConn;
SqlCommand sc = new SqlCommand("SELECT intYear, Description FROM
dbo.vwYearSelector ",cn);
cn.Open();
SqlDataReader dr =sc.ExecuteReader();

//Set the values to be used from the database for the combo
cboYear.DataSource = dr; //attach Datareader
cboYear.DataValueField = "intYear"; //specify column that contains the
value to use
cboYear.DataTextField= "Description"; //specify column that contains the
value to see
cboYear.DataBind();
cn.Close(); //Must close this!!!!
dr.Close();
}

The data in vwYearSelector is

intYear Description
2004 2004
2005 2005/2006
2006 2006/2007
2007 2007/2008


All this worked fine in 2004, however now in 2005, the cboYear still
initially shows the 2004. How can I set the cboYear to now display 2005/2006
when the page first loads. 2004 could then be selected if required later.

Thanks
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

IT worked by chance, as it was the first value in the list, use the
SelectedIndex property to select the correct one.

Cheers,
 

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

Top