<asp: DropDownList>实现事件处理

本文介绍如何使用ASP.NET中的DropDownList控件实现数据的联动显示。通过设置AutoPostBack属性为true,并利用OnSelectedIndexChanged事件,可以在用户选择不同项时动态更新另一个DropDownList的内容。

需求:从上面的截图中,可以看到这是两个<asp:DropDownList>控件实现的界面,现在的需求是这样的,实现当选择第一个下拉控件并选择了相应的数据后,那么此时在第二个<asp:DropDownList>中进行绑定他的子类在此显示,从而实现页面两级菜单实现数据统一绑定。

解决办法:

  <tr>

                   <th>服务大类</th>

                    <td class="pro_title_css">

                        <asp:DropDownList runat="server" ID="txt_fuwu_goods_add_str" CssClass="pro_title_css  form-control" AutoPostBack="true"  OnSelectedIndexChanged="txt_fuwu_goods_add_str_SelectedIndexChanged"/>

                    </td>

 

                    <th>服务子类</th>

                    <td class="pro_title_css">

                            <asp:DropDownList runat="server" ID="txt_fuwu_goods_add_type" CssClass="pro_title_css  form-control" />

                    </td>

                </tr>

就是从DropDownList控件的事件处理方法入手,使用的是OnSelectedIndexChanged该方法,但是在使用该方法的时候,还要注意的一点,就是单纯的使用这个方法,DropDownList方法是不会触发这个事件的,原因就是要加上AutoPostBack="true"这个属性,这个属性表示的意思就是要求当前服务器控件的值改变后,要与服务器值保持同步,也就是实现自动回传功能,有了这个属性,此时DropDownList的事件处理就会触发,在触发后,将二级下拉菜单在该事件处理中进行数据绑定。

  protected void txt_fuwu_goods_add_str_SelectedIndexChanged(object sender, EventArgs e)

        {

            string strname = this.txt_fuwu_goods_add_str.SelectedItem.Text;

            initDropdownService(strname);

        }

 通过上面的处理后,就会实现两级<asp:DropDownList>实现联动。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShengGongSiGuanJianZhiBiaoList.aspx.cs" Inherits="ShengGongSiGuanJianZhiBiaoList" %> <%@ Import Namespace="System.Data" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link href="../css/css.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="../js/jquery-1.4.4.min.js"></script> <style type="text/css"> #head { height: auto; width: 900px; margin-left: auto; margin-right: auto; } #table01 { border-left: #99bbe8 solid 1px; border-top: #99bbe8 solid 1px; } #table01 th, #table01 td { border-right: #99bbe8 solid 1px; border-bottom: #99bbe8 solid 1px; width: 2%; font-size: 12px; height: 30px; text-align: center; } #table01 th { background: #f5fafa; color: #047E6C; font-weight: bold; } #no_border { border-left: 0px; border-right: 0px; border-bottom: 0px; } #cccc { border-right: #99bbe8 solid 1px; border-left: #99bbe8 solid 1px; border-bottom: #99bbe8 solid 1px; width: 2%; font-size: 12px; height: 30px; text-align: center; background: #f5fafa; } </style> <script type="text/javascript"> function validate() { $("input[name^='yi_pingfenbiaozhun_']").attr("name", "yi_pingfenbiaozhun_"); $("input[name^='ba_pingfen_']").attr("name", "ba_pingfen_"); return true; } </script> </head> <body> <form id="form1" runat="server"> <center> <div> <br /><br /> <table style="width: 90%; border-top: #99bbe8 solid 1px;" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <th id="cccc" colspan="11" class="auto-style1"><span style="color: black; font-size: x-large"><asp:DropDownList ID="dlyear" runat="server"> <asp:ListItem Value="2025">2025</asp:ListItem> <asp:ListItem Value="2026">2026</asp:ListItem> <asp:ListItem Value="2027">2027</asp:ListItem> <asp:ListItem Value="2028">2028</asp:ListItem> </asp:DropDownList>年<asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Value="1">1</asp:ListItem> <asp:ListItem Value="2">2</asp:ListItem> <asp:ListItem Value="3">3</asp:ListItem> <asp:ListItem Value="4">4</asp:ListItem> <asp:ListItem Value="5">5</asp:ListItem> <asp:ListItem Value="6">6</asp:ListItem> <asp:ListItem Value="7">7</asp:ListItem> <asp:ListItem Value="8">8</asp:ListItem> <asp:ListItem Value="9">9</asp:ListItem> <asp:ListItem Value="10">10</asp:ListItem> <asp:ListItem Value="11">11</asp:ListItem> <asp:ListItem Value="12">12</asp:ListItem> </asp:DropDownList>省公司关键业绩指标排名</span></th> </tr> </table> <br /> <div> <asp:Button ID="Button2" runat="server" CssClass="Button" Text="查 询" OnClick="Button2_Click"></asp:Button> <asp:Button ID="Button1" runat="server" OnClientClick="return validate()" CssClass="Button" Text="保 存" OnClick="Button1_Click"></asp:Button> </div> <br /> <table id="table01" style="width: 100%" cellpadding="0" cellspacing="0"> <tr> <th>序号</th> <th>考核指标</th> <th>排名</th> <th>一级指标牵头部门</th> <th>一级指标责任部门名称</th> <th>二级指标</th> <th>二级指标责任部门</th> </tr> <%if (dtbm.Rows.Count > 0) { foreach (DataRow bmRow in dtbm.Rows) { string firstId = bmRow["uid"].ToString(); // 1. 先完整计算一级指标总嵌套行数(修复核心:先计算后使用) int firstTotalRows = 1; // 一级自身行 DataRow[] secondRows = dtsan.Select("一级Id='" + firstId + "'"); if (secondRows.Count() > 0) { firstTotalRows = 0; } foreach (DataRow sanRow in secondRows) { string secondId = sanRow["uid"].ToString(); DataRow[] thirdRows = dtwu.Select("二级Id='" + secondId + "'"); int secondTotalRows = 1 + thirdRows.Length; // 二级自身行 + 三级行数 firstTotalRows += secondTotalRows; // 累加所有二级的总行数 } // 2. 获取一级责任部门 DataRow[] firstDeptRows = dtbminfo.Select("一级Id='" + firstId + "'"); string firstDeptName = firstDeptRows.Length > 0 ? firstDeptRows[0]["一级指标责任部门名称"].ToString() : ""; %> <!-- 一级指标行(使用预先计算好的firstTotalRows) --> <tr> <td rowspan="<%= firstTotalRows %>"><%= bmRow["序号"].ToString() %></td> <td rowspan="<%= firstTotalRows %>"><%= bmRow["考核指标"].ToString() %></td> <td rowspan="<%= firstTotalRows %>"> <input type="hidden" id="yi_pingfenbiaozhun_<%=bmRow["uid"].ToString()%>" name="yi_pingfenbiaozhun_<%=bmRow["uid"].ToString()%>" value="<%=bmRow["uid"].ToString() %>" style="width: 98%" /> <input type="text" style="width:20px" value="" name="ba_pingfen_<%= bmRow["uid"].ToString() %>" /> </td> <td rowspan="<%= firstTotalRows %>"><%= bmRow["一级指标牵头部门"].ToString() %></td> <td rowspan="<%= firstTotalRows %>"><%= firstDeptName %></td> <!-- 处理二级指标 --> <%if (secondRows.Length > 0) { bool isFirstSecond = true; foreach (DataRow sanRow in secondRows) { string secondId = sanRow["uid"].ToString(); DataRow[] thirdRows = dtwu.Select("二级Id='" + secondId + "'"); int secondTotalRows = 1 + thirdRows.Length; // 二级合并行数 %> <!-- 二级指标行 --> <%if (isFirstSecond) { isFirstSecond = false; %> <td rowspan="<%= secondTotalRows %>"><%= sanRow["二级指标"].ToString() %></td> <%} else { %> <tr> <td rowspan="<%= secondTotalRows %>"><%= sanRow["二级指标"].ToString() %></td> </tr> <%}%> <!-- 处理三级指标(二级责任部门) --> <%if (thirdRows.Length > 0) { foreach (DataRow wuRow in thirdRows) { %> <tr> <td><%= wuRow["二级指标责任部门名称"].ToString() %></td> </tr> <%} } else { %> <tr> <td></td> </tr> <%}%> <%} } else { %> <td></td> <td></td> <%}%> </tr> <% } } else { %> <tr> <td colspan="7" style="height:30px;">暂无数据</td> </tr> <%}%> </table> </div> </center> </form> </body> </html>按照这个代码生成,二级指标和二级责任部门要保留
07-19
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值