刷新网络表单中的Dropdownlist

本文关键字:Dropdownlist 表单 网络 刷新 | 更新日期: 2023-09-27 18:27:13

我想刷新webform中的dropdownlist。我放了

<asp:DropDownList 
  ID="ddlNewJobOrder" 
  DataTextField="name" 
  DataValueField="value"
  runat="server" 
  AppendDataBoundItems="True" 
  DataSourceID="NewJobOrderDS" 
  ToolTip="New Job Order List" 
  Width="110" 
  AutoPostBack="True" 
  OnSelectedIndexChanged="ddlNewJobOrder_SelectedIndexChanged">
 </asp:DropDownList>

 ddlNewJobOrder.DataBind();

但它不起作用。

刷新网络表单中的Dropdownlist

DataSourceID是不够的。它不是"必须"的属性。您需要的属性是DataSource。在服务器端,创建一个包含要添加的数据的对象并进行

ddlNewJobOrder.DataSource = dataObject;
ddlNewJobOrder.DataBind();
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="Sql_dalei" DataTextField="name1" DataValueField="flag1" AutoPostBack="True" OnSelectedIndexChanged="zhonglei_SelectedIndexChanged"></asp:DropDownList>
<asp:SqlDataSource ID="Sql_dalei" runat="server" 
ConnectionString="<%$ ConnectionStrings:SignBoardConnectionString%>"
SelectCommand="SELECT * FROM [TestCodeClass1]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="Sql_zhonglei" DataTextField="name2" DataValueField="flag2" AutoPostBack="True" OnSelectedIndexChanged="nmnm_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="Sql_zhonglei" runat="server"
 ConnectionString="<%$ ConnectionStrings:SignBoardConnectionString %>"
SelectCommand="SELECT * FROM [TestCodeClass2]"></asp:SqlDataSource>
----------------------------------------------------------------------------
 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, new ListItem { Text = "大類名稱", Value = "0" });
            DropDownList1.SelectedItem.Selected = true;
            tet.Attributes["class"] = "s2";
        }
    }
protected void zhonglei_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedIndex != 0)
        {
            DropDownList2.Items.Clear();
            String s = DropDownList2.SelectedValue;
            Sql_zhonglei.SelectCommand = "SELECT * FROM [TestCodeClass2] where preflag1='" + DropDownList1.SelectedIndex + "'";
            DropDownList2.DataBind();
            DropDownList2.Items.Insert(0, new ListItem { Text = "中類名稱", Value = "0" });
            tett.Attributes["class"] = "s2";
            tett2.Attributes["class"] = "s";
            if (DropDownList2.SelectedValue == "")
            {
                tett.Attributes["class"] = "s";

            }
        }
        else
        {
            tett.Attributes["class"] = "s";
            tett2.Attributes["class"] = "s";
        }
    }