下拉列表从数据库绑定

时间:2016-07-04 04:54:38

标签: c# asp.net

我的表单中有两个Dropdownlist控件。我从数据库中填写一个Dropdownlist。为什么它会填充Dropdownlist两个控件中的值?

这就是我所做的。

<asp:DropDownList AutoPostBack="false" Style="position: static" ID="ddlcountry" OnSelectedIndexChanged="country_SelectedIndexChanged" runat="server"></asp:DropDownList>


public void Bind_ddlCountry()
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("select id,name from country order by id", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds != null)
            {
                ddlcountry.Items.Clear();
                ddlcountry.DataSource = ds;
                ddlcountry.DataTextField = (ds.Tables[0].Columns["name"].ToString());
                ddlcountry.DataValueField = (ds.Tables[0].Columns["id"].ToString());
                ddlcountry.DataBind();
                ddlcountry.Items.Insert(0, new ListItem("Select Country", "0"));
                ds.Dispose();
            }
            conn.Close();
        }

我在页面加载时调用此Bind_ddlCountry()。这完全绑定。

但我还有一个这种形式的下拉列表

<asp:DropDownList Style="position: static" AutoPostBack="false" ID="ddlPhone" runat="server"></asp:DropDownList>


 public void Bind_ddlPhone()
        {
            conn.Open();

            SqlCommand cmd = new SqlCommand("select id,phonecode from country", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds != null)
            {
                ddlPhone.Items.Clear();
                ddlPhone.DataSource = ds;
                ddlPhone.DataTextField = (ds.Tables[0].Columns["phonecode"].ToString());
                ddlPhone.DataValueField = (ds.Tables[0].Columns["id"].ToString());
                ddlPhone.DataBind();
                ddlPhone.Items.Insert(0, new ListItem("Select Phone", "0"));
                ds.Dispose();
            }
            conn.Close();
        }

这也填补了旧数据意味着ddlcountry数据。

提前致谢。

2 个答案:

答案 0 :(得分:0)

更改cmd,da和ds名称 如

created_at

答案 1 :(得分:0)

您有两种不同的方法。 &#39;新&#39;关键字在该方法中创建该对象的新实例,因此重命名您的字段并非如此。

检查你的桌子。我相信您在电话代码中拥有相同的数据&#39;和&#39; name&#39;列。

另外,你的方法很长。 您可以像这样更改DataTextField和DataValueField:

- (void)fetchFileList {

_fileList = nil;
_fileListFetchError = nil;


GTLRDriveService *service = self.service;

GTLRDriveQuery_FilesList *query = [GTLRDriveQuery_FilesList query];

// Because GTLRDrive_FileList is derived from GTLCollectionObject and the service
// property shouldFetchNextPages is enabled, this may do multiple fetches to
// retrieve all items in the file list.


query.fields = @"kind,nextPageToken,files(mimeType,id,kind,name,webViewLink,thumbnailLink,trashed)";



_fileListTicket = [service executeQuery:query
                      completionHandler:^(GTLRServiceTicket *callbackTicket,
                                          GTLRDrive_FileList *fileList,
                                          NSError *callbackError) {
                          // Callback
                          _fileList = fileList;
                          _fileListFetchError = callbackError;
                          _fileListTicket = nil;



                          NSLog(@"%@", fileList);


                      }];
}

确保您已经使用了所有用途。

            ddlPhone.DataTextField = "phonecode";
            ddlPhone.DataValueField = "Id";
            ddlPhone.DataBind();

 ddlcountry.DataTextField = "name";
            ddlcountry.DataValueField = "id";
            ddlcountry.DataBind();