asp.net 如何停止在下拉列表中随机选择的第一个项目?

mbskvtky  于 2023-03-20  发布在  .NET
关注(0)|答案(1)|浏览(90)

在这里,我编写了代码,让用户从下拉列表中选择表的名称作为一项,然后表信息显示在下面的表模板中,但是当我运行它时,它从下拉列表中选择第一项,即使我尝试选择另一项,也知道我禁用了第一项。

aspx页面

<asp:DropDownList ID="ddl1" runat="server" runat="server"
    AppendDataBoundItems="true" ValidateRequestMode="Enabled">
</asp:DropDownList>

aspx.cs页面

protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(connectionString);
    string ddlQuery = "SELECT name from sys.tables ";
    SqlCommand sqlCom = new SqlCommand(ddlQuery, conn);
    conn.Open();
    SqlDataAdapter adpt = new SqlDataAdapter(ddlQuery, conn);
    DataTable dt = new DataTable();
    adpt.Fill(dt);
    ddl1.DataSource = dt;
    ddl1.DataTextField = "name";
    ddl1.DataValueField = "name";
    ddl1.DataBind();
    ddl1.Items.Insert(0, new ListItem("Select Section", string.Empty));
    ddl1.Items[0].Selected = false;
    ddl1.Items[0].Attributes["disabled"] = "disabled";
    conn.Close();

    DataTable table1 = GetData();

    if (!this.IsPostBack)
    {
        GetData();
        StringBuilder html = new StringBuilder();
        html.Append("<table>");

        html.Append("<tr>");

        html.Append("<th>Service ID </th>");
        html.Append("<th>Service Name </th>");
        html.Append("<th>Simple Service Description </th>");
        html.Append("<th>Service Description </th>");
        html.Append("<th>Service Image </th>");
        html.Append("<th>Service Time </th>");
        html.Append("<th>Service Price </th>");
        html.Append("<th>Action </th>");

        html.Append("</tr>");
        foreach (DataRow row in table1.Rows)
        {
            html.Append("<tr>");
            foreach (DataColumn column in table1.Columns)
            {
                html.Append("<td>");
                html.Append(row[column.ColumnName]);
                html.Append("</td>");
            }
            html.Append("</tr>");
        }
        html.Append("</table>");

        PlaceHolder1.Controls.Add(new Literal
        {
             Text = html.ToString()
        });
    }
}

DataTable GetData()
{
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        con.Open();

        string SectionName = ddl1.SelectedValue;
        if (string.IsNullOrEmpty(SectionName))
        {
            return new DataTable(); // or return an empty DataTable
        }
        else
        {
            string query = "SELECT * FROM  ["+ SectionName +"]";
            using (SqlCommand cmd = new SqlCommand(query))
            {
                using (SqlDataAdapter adpt1 = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    adpt1.SelectCommand = cmd;

                    using (DataTable dt1 = new DataTable())
                    {
                        adpt1.Fill(dt1);
                        return dt1;
                    }
                }
            }
        }
    }
}
ctehm74n

ctehm74n1#

您的代码没有显示如何填充ddl1.Items,看起来您只给予了它第一个值,然后禁用它并设置它不应该是选中的那个。
但是,由于下拉列表除了第一个值之外没有其他值可以选择,所以它必须选择它。

相关问题