1. When the list box is displayed iniitally, I would like the selected value to be a literal 'Select Payment Date'. This would
be a value I load in the LoadPaymentDates method.
(The values from the dropdown list box are loaded with the code from LoadPaymentDates method.)
2. Once the user has selected a value from the dropdown list, I would like the dropdown list to display that selection. Basically i
I would like whatever the selects from the dropdown list to become the selected value. TRight now right after the user selects a
value, the dropdown list defaults to displaying the original values.
Code for aspx file:
I would like
Code for aspx.cs file:
protected void Page_Load(object sender, EventArgs e)
{
LoadPaymentDates();
}
protected void LoadPaymentDates()
{
String strPaymentDateSql = "";
strPaymentDateSql = "select payment_date from dbo.ASch order by payment_date desc";
FillAttListBox(ddlPaymentDate, "payment_date", "payment_date", DBConnection.ExecuteQuery(strPaymentDateSql), false);
}
protected void SaveSelectPaymentDate(object sender, EventArgs e)
{
ddlPaymentDate.SelectedValue = ddlPaymentDate.SelectedValue;
}
public static void FillAttListBox(DropDownList AttlistBox, String dataValueField, string dataTextField, DataTable dataTbl, bool bHasBlank)
{
AttlistBox.DataTextField = dataTextField;
AttlistBox.DataValueField = dataValueField;
AttlistBox.DataSource = dataTbl;
AttlistBox.DataBind();
}
public static DataTable ExecuteQuery(string SQLstring)
{
string constr = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(constr);
DataTable dt = new DataTable("tbl");
using (conn)
{
conn.Open();
SqlCommand comm = new SqlCommand(SQLstring, conn);
comm.CommandTimeout = 0;
SqlDataAdapter da = new SqlDataAdapter(comm);
da.Fill(dt);
}
return dt;
}
Satyapriya NayakPosted Dec 9, 2011, 10:39 PM
Note:- Change it as below.
protected void SaveSelectPaymentDate(object sender, EventArgs e)
{
ddlPaymentDate.SelectedIndex = 0;
}
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
if (!IsPostBack)
{
DropDownList_country.Items.Add("Choose country");
con.Open();
str = "select country from area group by country";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DropDownList_country.Items.Add(reader["country"].ToString());
}
reader.Close();
con.Close();
}
DropDownList_state.Items.Clear();
}
protected void DropDownList_country_SelectedIndexChanged1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from area where country='" + DropDownList_country.SelectedItem.Text + "'";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DropDownList_state.Items.Add(reader["state"].ToString());
}
reader.Close();
con.Close();
DropDownList_country.SelectedIndex = 0;
}
}
Thanks
If this post helps you mark it as answer