简体   繁体   English

设置一个 <select> SQL Server数据库表中的动态下拉列表

[英]Setup a <select> dropdown list dynamically from a SQL Server database table

I'm trying to setup a dropdown list to pull from a table in a SQL Server database. 我正在尝试设置一个下拉列表以从SQL Server数据库中的表中提取数据。 I am using aspx with code behind to submit data to the SQL Server database. 我正在使用带有代码的aspx将数据提交到SQL Server数据库。 Ultimately, what I need to have happen is to display customer names in a drop down list then submit that data to another database table. 最终,我需要做的是在下拉列表中显示客户名称,然后将该数据提交到另一个数据库表。 Using <asp:DropDownList> is an option but I am submitting the data by the name attribute and I cannot set a specific name to the asp webform object. 使用<asp:DropDownList>是一个选项,但是我通过name属性提交数据,并且无法为asp webform对象设置特定的名称。 I have not used php in my form as of yet and would like to stay clear if at all possible. 到目前为止,我还没有使用过php格式,如果可能的话,请保持清晰。

aspx html code: aspx html代码:

<select id="customerName" name="custName" class="txtbxList">
 <option></option>
 <option></option>
 <option></option>
</select>

asp DropDownSOurce and List Tags asp DropDownSOurce和列表标签

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:csusaCentralConnection %>' SelectCommand="SELECT [customerNameDD] FROM [customerNameList]"></asp:SqlDataSource>
<asp:DropDownList ID="customerName" CssClass="txtbxList" runat="server" DataSourceID="SqlDataSource1" DataTextField="customerNameDD" DataValueField="customerNameDD"></asp:DropDownList>

My code-behind: 我的后台代码:

string coName = Request.Form["custName"];
string generalInfo = @"INSERT INTO generalInfo(CustomerName)VALUES(@custName);

        SELECT SCOPE_IDENTITY();
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["csusaCentralConnection"].ConnectionString);

        SqlCommand cmd = new SqlCommand(generalInfo, conn);

        //NAME of input field in aspx file

        cmd.Parameters.AddWithValue("custName", coName);

        conn.Open();
        string rowIdentity = cmd.ExecuteScalar().ToString();
        conn.Close();

It looks like you are using the designer to setup your dropdown list data source. 您似乎正在使用设计器来设置下拉列表数据源。 Here is how you could do it in code: 这是您可以在代码中执行的操作:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            MyLinqToSqlDataContext db = new MyLinqToSqlDataContext();
            var result = from c in db.Customers
                         select g;
            customerName.DataSource = result.ToList();
            customerName.DataTextField = "Name";
            customerName.DataValueField = "ID";
            customerName.DataBind();
            ...
        }
   }

By the looks of it you're most of the way there, you should only need to change this line: 从外观上看,这是您最常用的方法,您只需要更改以下行:

string coName = Request.Form["custName"];

To: 至:

string coName = customerName.SelectedValue;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM