ここでは、次のような値を入力するように ddl をバインドしています: Australis(+61)。別の場所では、別の ddl をバインドして、次のような値を設定したいと考えています: Australia. 同じコードを使用してクエリを変更できますか? 実際には、必要な場所 (別の aspx ページ) でこの関数 (別のクラスで定義されている) を呼び出しています。コードの冗長性を避けることができますか、それともコードを繰り返す必要がありますか
public DataSet BindDropDownList()
{
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
con.Open();
string strQuery = "SELECT CountryName + '(+' + CountryCode + ')' As CountryName,CountryCode from ACountry";
SqlCommand cmd = new SqlCommand(strQuery, con);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
da.Fill(ds, "AUser");
con.Close();
return ds;
}
これは、aspx ページの 1 つで呼び出す方法です。
protected void Page_Load(object sender, EventArgs e)
{
UserFunctions objBindDDL = new UserFunctions();
ddlCountryCode.DataSource = objBindDDL.BindDropDownList().Tables["AUser"];
ddlCountryCode.DataTextField = "CountryName";
ddlCountryCode.DataValueField = "CountryCode";
//ddlCountryCode.SelectedText = ddlCountryCode.Items.FindByText("India(+91)");
ddlCountryCode.DataBind();
}
@Praveen:これは国名だけが欲しいところです
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
UserFunctions objBindDDL = new UserFunctions();
ddlCountry.DataSource = objBindDDL.BindDropDownList().Tables["AUser"];
ddlCountry.DataTextField = "CountryName";
//ddlCountry.DataValueField = "CountryName";
//ddlCountryCode.SelectedText = ddlCountryCode.Items.FindByText("India(+91)");
ddlCountry.DataBind();
}
}