惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
Jina AI
Jina AI
罗磊的独立博客
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
J
Java Code Geeks
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
T
The Blog of Author Tim Ferriss
腾讯CDC
Hugging Face - Blog
Hugging Face - Blog
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
InfoQ
月光博客
月光博客
博客园_首页
Vercel News
Vercel News
P
Proofpoint News Feed
GbyAI
GbyAI
Y
Y Combinator Blog

博客园 - winglzz

高手经验谈 招牌net程序员(包吃住有电话,冰箱等)工作地点上海 SQL注入 用asp.net写的论坛程序 ASP.NET中如何防范SQL注入式攻击 .NET 专题-安全-SQL注入攻击 经典的导航二级式导航菜单增强版 ASP.NET中树形图的实现 WEB应用程序中的进度条 C#中英文语音合成与中文语音识别技术 ASP.NET实现投票结果的图片进度条显示 几个漂亮的Button的CSS 使用C#在进度条中显示复制文件的进度 Net 下安装、调试的常见问题与错误!!! ASP.NET中的事务处理和异常处理 三层Web体系结构里的两种数据绑定模式 C#实现-移位加密 C#非对称加密程序 C#MD5算法
asp.net中的联动菜单
winglzz · 2007-03-26 · via 博客园 - winglzz

标达到的效果:两个下拉框,第二个跟随第一个变化而变化,使用客户端脚本JavaScriptASP.NET环境下实现。

第一步:建立JavaScript脚本:

Page_Load中建立并注册这个js脚本:

string scriptKey = "MenuChange";

if (!Page.IsStartupScriptRegistered(scriptKey) &&  !Page.IsPostBack)

{

       string scriptBlock = 

              @"<script language=""JavaScript"">

                <!--

                       function InitBigClass()

                       {

                              bigclass  = new Array();

                              bigclass[0]  = new Array();

                              bigclass[0][0]  = '0';

                              bigclass[0][1]  = '全部论坛';

                              bigclass[1]  = new Array();

                              bigclass[1][0]  = '3';

                              bigclass[1][1]  = 'Web 开发';

                              bigclass[2]  = new Array();

                              bigclass[2][0]  = '4';

                              bigclass[2][1]  = '软件工程/管理';

                       }

                     function InitSmallClass()

                     {

                            smallclass  = new Array();

                            smallclass[0]  = new Array();

                            smallclass[0][0]  = '301';

                            smallclass[0][1]  = 'ASP';

                            smallclass[0][2]  = '3';         // 此处与上面的大类对应

                            smallclass[1]  = new Array();

                            smallclass[1][0]  = '303';

                            smallclass[1][1]  = 'PHP';

                            smallclass[1][2]  = '3';

                            smallclass[2]  = new Array();

                            smallclass[2][0]  = '401';

                            smallclass[2][1]  = '软件工程';

                            smallclass[2][2]  = '4';

                            smallclass[3]  = new Array();

                            smallclass[3][0]  = '403';

                            smallclass[3][1]  = '软件测试';

                            smallclass[3][2]  = '4';

                     }

                     InitBigClass();

                     InitSmallClass();

                     function changeitem(myfrm)               // 主要js的函数!!!

                     {      

                            var SelectedBigId,i,j;

                            for (i= myfrm.smallclassid.options.length-1;i>=0  ;--i)      

                            {

                                   myfrm.smallclassid.options[i] = null; 

                            }

              SelectedBigId = myfrm.bigclassid.options[myfrm.bigclassid.selectedIndex].value;

                            j = 0;       

                            for (i=0 ;i< smallclass.length ;i++)      

                            {

                                   if (SelectedBigId == smallclass[i][2])

                                   {

                     myfrm.smallclassid.options[j] = new Option(smallclass[i][1],smallclass[i][0]); 

                                          ++j;

                                   }

                            }

                     }

              //-->

              </script> ";

       Page.RegisterClientScriptBlock(scriptKey, scriptBlock);      // 注册这个脚本

}

第二步:在页面中加入两个<select>

<select id="bigclassid" onchange="javascript:changeitem(document.Form1);" name= "bigclassid"> (FormidForm1)

<option value="0" selected>全部论坛</option>

</select>

<select id="smallclassid" name="smallclassid">

<option>请您选择</option>

</select>

注意selectidname属性要与上面的js相一致。

第三步:在Button_OnClick()中加入代码

int i;

for(i=0;i<Request.Form.Count;i++)

       if(Request.Form.AllKeys[i].ToString()=="smallclassid")

              break;                          // form中找到这个select (根据id或者name查找)

int SelectValue = Request.Form.GetValues(i)[0];   // 这个值就是select选中的值