") .a...">
美文网首页
C#下拉框选择

C#下拉框选择

作者: Dove_Q | 来源:发表于2017-08-24 15:21 被阅读215次

下拉框

// AutoPostBack和OnSelectedIndexChanged是为了控制函数回调

<asp:DropDownList ID="ddlGrade" runat="server" AutoPostBack="True" 
OnSelectedIndexChanged="ddlChange_SelectedIndexChanged"></asp:DropDownList>
<asp:DropDownList ID="ddlPassTypeList" runat="server" AutoPostBack="True" 
OnSelectedIndexChanged="ddlPassTypeList_SelectedIndexChanged">
    <asp:ListItem Value="All">[所有]</asp:ListItem>
    <asp:ListItem Value="1">已审核</asp:ListItem>
    <asp:ListItem Value="0">未审核</asp:ListItem>
</asp:DropDownList>

选中下拉框

js获取asp标签的ID:$("#<%=ddlTypeYear.ClientID%>")

.aspx中选中第0个选项
addDdlDept.SelectedValue = Convert.ToString(0);
在javascript中选中指定的选择框
  • 根据value来指定
$("#<%=ddlTypeYear.ClientID%>").val("[请选择]");
$("#<%=ddlTypeStoragePeriod.ClientID%>").val("[请选择]");
$("#<%=addTypeDdlDept.ClientID%>").val("[请选择]");
  • 根据index来指定
$("#<%=ddlTypeYear.ClientID%>")[0].selectedIndex = 0;
$("#<%=ddlTypeStoragePeriod.ClientID%>")[0].selectedIndex = 0;
$("#<%=addTypeDdlDept.ClientID%>")[0].selectedIndex = 0;

给下拉框赋值

  • 从数据库表中取数据赋值给下拉框
ddlDept.Items.Clear();
string strDepart = "select ClassName,Id from FM_ListClass where DeleteSign = 0";
zhyFunctions.GetDropDownList(ddlDept, strDepart, "ClassName", "Id", 1);
// ddlDept:下拉框的Id 
//数据库表中字段:ClassName:显示出来的Text, Id:Value的值
  • 用枚举方法给下拉框赋值
//  定义枚举
public class FileManageConfig
{
    public enum FilePass
    {
        [EnumValue(1, "已归档")]
        Pass = 1,
        [EnumValue(0, "未归档")]
        UnPass = 0,
        [EnumValue(-1, "不知道")]
        nokonw = 0,
    }
}
// FileManageConfig.FilePass是枚举方法的路径
ddlPass.Items.Clear();
ddlPass.Items.Add(new ListItem("[全部]", "All"));
BidirHashtable<object, EnumValueAttribute> passMap = EnumConvertUtils.EnumToAttributeMap(typeof(FileManageConfig.FilePass));
foreach (string passStatus in Enum.GetNames(typeof(FileManageConfig.FilePass)))
{
    ddlPass.Items.Add(new ListItem((string)passMap[Enum.Parse(typeof(FileManageConfig.FilePass), passStatus)].DisplayValue, Convert.ToString((int)passMap[Enum.Parse(typeof(FileManageConfig.FilePass), passStatus)].DbValue)));
}

相关文章

网友评论

      本文标题:C#下拉框选择

      本文链接:https://www.haomeiwen.com/subject/cdledxtx.html