按另一个DropDownList中的选定项填充DropDownList项?

时间:2013-10-29 09:36:39

标签: c# asp.net

<table><tr>
   <td>
      Type:
   </td>
    <td>
       <asp:DropDownList ID="ddltype" runat="server" OnSelectedIndexChanged="ddltype_SelectedIndexChanged">
       <asp:ListItem Value="0">---select---</asp:ListItem>
       <asp:ListItem Text="Setter" Value="1">
       </asp:ListItem>
       <asp:ListItem Text="Getter" Value="2"></asp:ListItem>
       </asp:DropDownList>
    </td>
       <td>
          Name:
       </td>
            <td>
              <asp:DropDownList ID="ddlname" runat="server">
              </asp:DropDownList>
            </td>
 </tr> </table>

如果用户在第一个下拉列表中选择settergetter,我想在第二个下拉列表中填写相关名称。如何在c#页面中编写此要求的代码。请帮助我。

1 个答案:

答案 0 :(得分:1)

 public void NamesToDropdownList()
{
        objhatcheryPL.type = ddltype.SelectedItem.Text;
        DataTable dtname = new DataTable();
        dtname = objhatcheryBAL.dtnames(objhatcheryPL);
        ddlname.DataTextField = "Name";
        ddlname.DataValueField = "Name";
        ddlname.DataSource = dtname;
        ddlname.DataBind();
        ddlname.Items.Add(new ListItem("--select--", "0"));
        ddlname.SelectedIndex = ddlname.Items.Count - 1;
 }

protected void ddltype_SelectedIndexChanged(object sender, EventArgs e)
   {
    if (ddltype.SelectedItem.Text != "--Select--")
    {
        NamesToDropdownList();
    }
   }

用这个我的代码完美运行....