【问题标题】:binding dropdownlist to a dataset returned from a function将下拉列表绑定到从函数返回的数据集
【发布时间】:2013-03-21 21:25:36
【问题描述】:

我编写了以下将下拉列表绑定到数据集的方法。我需要在我的项目中的不同页面上调用此方法两次。所以我创建了一个类并将方法放入其中,我试图通过创建一个对象来访问这个方法。这样做有困难...

 public void bind()
    {
        DataSet ds1 = 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(ds1, "AUser");
        ddlCountryCode.DataSource = ds1.Tables["AUser"];
        ddlCountryCode.DataTextField = "CountryCode";

        //ddlCountryCode.SelectedValue = "India(+91)";
        ddlCountryCode.DataBind();
        ddlCountryCode.SelectedIndex = ddlCountryCode.Items.IndexOf(ddlCountryCode.Items.FindByText("India(+91)"));
        con.Close();
    }

如果我在新类中编写此完整方法,它无法识别其中使用的控件(下拉列表),因此会引发错误。所以我只包含了以下部分:

  public void bindddl()
    {
        DataSet ds1 = 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(ds1, "AUser");

        con.Close();
    }

现在这会返回一个数据集,我需要将它与另一个表单 (.aspx) 上的下拉列表绑定。我该怎么做?

protected void Page_Load(object sender, EventArgs e)
    {
        Bind objbind = new Bind();
        ddlCountryCode.DataSource = objbind.---->?????????;
        ddlCountryCode.DataTextField = "CountryCode";

        //ddlCountryCode.SelectedValue = "India(+91)";
        ddlCountryCode.DataBind();
        ddlCountryCode.SelectedIndex = ddlCountryCode.Items.IndexOf(ddlCountryCode.Items.FindByText("India(+91)"));
    }

另外,我还能做什么?这里还有更好的选择吗?

【问题讨论】:

    标签: c# asp.net drop-down-menu dataset


    【解决方案1】:

    让你的函数返回 DataSet,然后将它分配给你想要的任何东西

     public DataSet bindddl()
        {
            DataSet ds1 = 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(ds1, "AUser");
    
            con.Close();
    
            return ds1;
        }
    

    然后将它们分配如下。

    Bind objbind = new Bind();
    ddlCountryCode.DataSource = objbind.bindddl().Tables["AUser"];
    

    【讨论】:

    • 找不到类型或命名空间数据集。您是否缺少程序集参考或指令...? --->公共数据集 bindddl()
    • @AdityaNawandar - 您是否使用 System.Data 添加;在班上名列前茅?
    • 是的……没关系,我重新输入了“数据集”,就完成了。谢谢...(标记为答案)
    • @AdityaNawandar - 很高兴为您提供帮助。
    • 我还有另一个问题.. 为什么它只取代码而不是表中的国家名称??
    猜你喜欢
    • 1970-01-01
    • 2011-12-31
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多