【问题标题】:Binding a more than one dropdowndownlist changing only the query绑定多个下拉列表,仅更改查询
【发布时间】:2013-03-23 06:05:46
【问题描述】:

在这里,我绑定了一个 ddl 来填充如下值:Australis(+61)。在另一个地方,我想绑定另一个 ddl 来填充这样的值:澳大利亚。我可以使用相同的代码并更改查询吗?实际上,我在所需位置(不同的 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 页面中的调用方式:

 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();
        }
    }

【问题讨论】:

  • 你想用相同的代码绑定两个不同的dropdownlist
  • 我已经给出了每个下拉列表将包含的值的示例...我在想可能我只会更改查询...
  • 怎么样?我无法做到这一点......正如我所说,我在不同的地方调用该函数......那么我如何让它接受所需的查询?
  • 在我回答之前再澄清一下...您将从两个不同的表中获取数据...对吗?
  • 同一张桌子。它很简单,在一个地方我想要国家名称和国家代码......在另一个地方我只想要国家名称......

标签: asp.net data-binding drop-down-menu


【解决方案1】:

考虑到您的最后一条评论,这样做非常简单:

您需要在resultset 中单独取CountryName 的值,如下所示。

string strQuery = "SELECT CountryName,  CountryName + '(+' + CountryCode + ')' As CountryNameCode, CountryCode from ACountry";

那么每当你绑定dropdownlist时,你只需要更改DataTextField

 ddlCountryCode.DataTextField = "CountryNameCode";   // For Country name and code
 ddlCountryCode.DataTextField = "CountryName";       // for just country name

code 的其余部分将保持不变。

还有一点需要注意:
不要将dropdownlist 直接绑定到page load。使用IsPostBack

protected void Page_Load(object sender, EventArgs e)
{

    if(!IsPostBack)
    {
        // your binding here
    }
}

【讨论】:

  • 我试过了,这没有帮助... !IsPostBack 有什么作用?
  • 没有错误,但它仍然给了我 - 国家名称和国家代码.. 我不想要国家代码......
  • 你能在你的问题中发布更新的代码吗......这似乎是某个地方的愚蠢错误
  • 我没有更改代码中的查询。我需要更改查询吗??
  • 是的...看我的回答...我已经更改了查询...这是我回答中的第一件事
【解决方案2】:

你只需要使用参数,像这样向这个函数添加新参数。

public DataSet BindDropDownList(Bool IsIndexPage)
    {
        DataSet ds = new DataSet();
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
        con.Open();

        string strQuery = "";
if(IsIndexPage)
{
   strQuery = "SELECT CountryName + '(+' + CountryCode + ')' As CountryName,CountryCode from ACountry";
}
else
{
  strQuery = "SELECT CountryName,CountryCode from ACountry";
}
        SqlCommand cmd = new SqlCommand(strQuery, con);
        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
        da.Fill(ds, "AUser");
        con.Close();
        return ds;
    }

【讨论】:

  • IsIndexPage??我不能直接提供页面名称吗?
  • @Aditya Nawandar IsIndexPage 只是示例名称,您可以使用任何名称。当您调用此函数时,如果您需要 india(+91),则发送 true,如果不需要,则发送 false。
  • 所以你是说 IsIndexPage 被用作参数?所以我不必使用页面的实际名称,rt?我可以在那里使用任何变量名...??
  • 错误:不能在此范围内声明名为 strQuery 的局部变量等等等等......
猜你喜欢
  • 2013-12-08
  • 2021-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-10
相关资源
最近更新 更多