如何将数据库的记录填充到下拉列表? [英] How to populate a record for database to dropdownlist?

查看:57
本文介绍了如何将数据库的记录填充到下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击gridview中的编辑按钮后,我无法将数据库中的城市值填充到下拉列表中...



I在我的GridView旁边有这个按钮:

I can't populate the value of the city in a record from my database into a dropdownlist upon clicking the edit button from the gridview...

I have this button beside my GridView:

<asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="ButtonEdit" runat="server" Text="Edit" causesvalidation = "false"

                    OnCommand="EditDetails" CommandArgument='<%# Eval("TableID").ToString() %>' CommandName="ButtonCommandEdit"

                     />
                </ItemTemplate>
        </asp:TemplateField>





然后,t他的:(请注意,这只是EDIT按钮而不是UPDATE按钮)







And then, this: (Note that this is just EDIT button and not UPDATE button yet)


string recordID = ""; //to be used in ***
protected void EditDetails(object sender, CommandEventArgs e)
        {
            recordID = e.CommandArgument.ToString(); //to set value of ***
            GridViewUserInfoTable.Enabled = false;
            ButtonUpdate.Visible = true;
            ButtonCancel.Visible = true;
            ButtonSubmit.Visible = false;
            var connectionString = "server=localhost;uid=root;" + "pwd=xxxxx;database=xxxxx;";
            using (var connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                var query = "select * from table where tableID= " + recordID; //using the value of ***
                using (var command = new MySqlCommand(query, connection))
                {
                    using (var reader = command.ExecuteReader())
                    {

                        while (reader.Read())
                        {
                            TextBoxfirstName.Text = (reader.GetString("firstName"));
                            TextBoxmiddleName.Text = (reader.GetString("middleName"));
                            TextBoxlastName.Text = (reader.GetString("lastName"));
                            TextBoxmobileNo.Text = (reader.GetString("mobileNo"));
                            TextBoxemailAddress.Text = (reader.GetString("emailAddress"));
                            TextBoxregistrationDate.Text = reader.GetDateTime("registrationDate").ToString("yyyy-MM-dd");
                            TextBoxstreet.Text = (reader.GetString("street"));
                            DropDownListCity.SelectedValue = (reader.GetString("city")); 
//i believe i need to change something here in DropDownLists
                            DropDownListRegion.SelectedValue = (reader.GetString("region"));
                            TextBoxcountry.Text = (reader.GetString("country"));
                        }
                    }
                }
            }
            if (Page.IsPostBack)
            {
                //nothing
            }
        }





除了下拉列表外,所有东西都会填充...有人可以帮我这个吗?所有回复都将受到高度赞赏。在此先感谢!



Everything populates except for the drop down lists... Can someone please help me with this? All responses will be highly appreciated. Thanks in advance!

推荐答案

尝试以下代码



try the below code

DropDownListCity.SelectedItem.Text = (reader.GetString("city")); 


朋友,



1-首先你绑定DropDownListCity下拉列表然后你可以检查和分配选择的值。

2-在页面加载时使用IsPostBack。







DropDownListCity.Items.Add(new ListItem(reader.GetString(city)));



或者是否需要价值



DropDownListCity.Items.Add(new ListItem(Text,value);
Friend,

1- First you Bind the DropDownListCity dropdownlist then you can check & assigned selected value.
2- Use IsPostBack on your page load.

or

DropDownListCity.Items.Add(new ListItem(reader.GetString("city")));

or if value required

DropDownListCity.Items.Add(new ListItem("Text","value");


这篇关于如何将数据库的记录填充到下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆