• Asp中的DropDownList控件和原生下拉框的使用比较


    首先先思考下如果不使用DropdownList控件,如何将一个List集合中的数据通过下拉框(select标签)的形式显示?

    使用下拉框(纯select方式)实现分类数据的显示 

    步骤:

    处理后端:
    ①在后端代码中定义一个公开的userID变量,用于保存url中获取到的userID

    public static int userID ;

    ②先判断url中是否存在userID对应的值

    if (!string.IsNullOrEmpty(Request.QueryString["userID"]))
                    {
                        userID = int.Parse(Request.QueryString["userID"]);
                    }

    ③根据获取到的userID显示对应类型的数据(如果url中没有userID值,默认为1,Dal会做相应处理,为1会返回所有类型数据)

    Repeater1.DataSource = BLL_User.getUserByType(userID);
                Repeater1.DataBind();

      ****因为这里顺便练习三成构架所以用到了三层需要也可以进行相应的更改  附上getUserByType() 的连套方法:

    //BLL层
    public static List<User> getUserByType(int id)
            {
                return DAL_User.getUserByType(id);
            }
    //DAL层
    public static List<User> getUserByType(int id)
            {
                string sql = $"userType ={id}";
                if (id ==1)
                {
                    sql = "";
                }
                List<User> uers = SelectSQL(sql);
                return uers;
            }
    
    public static List<User> SelectSQL(string strWhere)
            {
                string sql = "";
                _users = new List<User>();
                if (strWhere == "")
                {
                    sql = "select * from UserInfo where isShow=1";
                }
                else
                {
                    sql = $"select * from UserInfo where isShow=1 and " + strWhere;
                }
                DataTable td = new DBHerple().SelectDataTable(sql);
                User user = null;
    
                foreach (DataRow dataRow in td.Rows)
                {
                    user = new User();
                    user.userID = int.Parse(dataRow["userID"].ToString());
                    user.userName = dataRow["userName"].ToString();
                    user.userPwd = dataRow["userPWd"].ToString();
                    user.userAddress = dataRow["userAddress"].ToString();
                    user.userType = int.Parse(dataRow["userType"].ToString());
                    _users.Add(user);//添加到集合中去
                }
                return _users;
            }

    处理前端:
    ①在前端页面实现select代码

    ②使用<% %>结合foreach遍历将集合中的数据显示为下拉框形式

    <form id="form1" runat="server">
            <div>
                <select id="slct" onchange="SelectType(this)">
                    <% foreach (var item in userType)
                        {%>
                    <option <%=(userID == item.typeID ? "selected":"") %> value="<% = item.typeID%>"><% = item.typeName%></option>
                    <%} %>
                </select>
            </div>
            <table class="layui-table user-table">
                <thead>
                    <tr>
                        <th>序号</th>
                        <th>账户</th>
                        <th>密码</th>
                        <th>地址</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <asp:Repeater ID="Repeater1" runat="server">
                        <ItemTemplate>
                            <tr>
                                <td><%# Eval("userID") %></td>
                                <td><%# Eval("userName") %></td>
                                <td><%# Eval("userPWd") %></td>
                                <td><%# Eval("userAddress") %></td>
                                <td>
                                    <asp:Button CssClass="layui-btn layui-btn-danger" ID="Button1" runat="server" Text="删  除" />
                                </td>
                            </tr>
                        </ItemTemplate>
                    </asp:Repeater>
                </tbody>
            </table>
    
        </form>
    </body>

    ③使用onchangge事件实现下拉框中选项改变之后的数据回发(userID的回发)

    <script>
            function SelectType(e) {//回发数据到当前页面的后端
                location.href = "SelectDemo.aspx?userID=" + e.value;
            }
        </script>

    ④转到后端获取userID的值 判断url中是否存在userID对应的值如果有就获取userID

     if (!string.IsNullOrEmpty(Request.QueryString["userID"]))
                    {
                        userID = int.Parse(Request.QueryString["userID"]);
                    }
                }

    ⑤<%=(userID== item.TypeID ? "selected":"") %> 通过匹配后端的TypeID值固定下拉框之前选中的项(如果这里不设置会有一个小bug下拉框不会绑定选定的值)

    最终效果图:

    但是如果用asp的DropDownList控件的话就简单多了

      ①:我们先拖入控件到前端中去(唯一要注意的一点是拖动控件到前端的时候一定一定要添加:AutoPostBack="true" 属性不然你后台代码写的在好都不会回发)

      ②:转到后台:

      为下拉框添加事件 getUserByType()方法和上面的一样

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    List<User> users = BLL_User.getAllUser();
                    this.DropDownList1.DataSource = BLL_User.getTypeAll();
                    this.DropDownList1.SelectedIndex = 0;//int值
                    this.DropDownList1.DataTextField = "TypeName";
                    this.DropDownList1.DataValueField = "TypeID";
                    this.DropDownList1.DataBind();
                }
                Repeater1.DataSource = BLL_User.getAllUser();
                Repeater1.DataBind();
            }
            /// <summary>
            /// 下拉框选项改变后触发信息变动
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                int id = int.Parse(this.DropDownList1.SelectedValue);
                Repeater1.DataSource = BLL_User.getUserByType(id);
                Repeater1.DataBind();
            }

       然后就大功告成了

      虽然我们这个控件方便了我们的使用但是我们也要了解一下原生没有控件我们应该怎么办 藏器在身,待时而动。

  • 相关阅读:
    php实现邮件自动发送之PHPMailer
    wnmp配置(windows+nginx+mysql+php开发环境)
    _stdcall和_cdecl,extern “C”的联系
    DLL导出的方式
    C++哈夫曼编码
    Python CGI 环境搭建
    Java学习日记——第二记:基本类型的类型转换
    测试技术的应用
    软件测试的分类
    软件测试生命周期
  • 原文地址:https://www.cnblogs.com/dongshenjun/p/12702886.html
Copyright © 2020-2023  润新知