如何访问Formview中的控件

本文介绍如何在ASP.NET中使用FindControl方法访问Formview控件,并通过FControl方法实现根据DropDownList选择显示或隐藏特定组件的逻辑。当DropDownList的选项为58时,显示id为FControl的组件,否则将其隐藏。
None.gif//.net Framework类库中的FindControl方法可以帮助我们访问Formview中的控件:
None.gif
protected void FormView1_ItemCreated(object sender, EventArgs e)   
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{        
InBlock.gif    DropDownList test;        
InBlock.gif   
switch (FormView1.CurrentMode)       
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{            
InBlock.gif         
case FormViewMode.Edit: 
InBlock.gif            test 
= ((DropDownList)FormView1.Row.FindControl("DropDownList3")); 
InBlock.gif            test.Attributes.Add(
"onchange""FControl('" + test.UniqueID + "');");    
InBlock.gif            
break
InBlock.gif       
case FormViewMode.Insert: 
InBlock.gif               test 
= ((DropDownList)FormView1.Row.FindControl("DropDownList3"));  
InBlock.gif              test.Attributes.Add(
"onchange""FControl('" + test.UniqueID + "');");   
InBlock.gif             
break;  
ExpandedSubBlockEnd.gif      }
   
ExpandedBlockEnd.gif}

None.gif
None.gif
//上面的程序实现的功能是:
None.gif
//给FormView中的EditItemTemplate和InsertItemTemplate中的DropDownList控件加上FControl;
None.gif
//FControl方法的代码是:
None.gif
  function FControl(ctlName)        
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif           var rowIndex
=document.getElementById(ctlName).selectedIndex;
InBlock.gif            
if(document.getElementById(ctlName).options[rowIndex].innerText=='58')
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                document.getElementById(
'FControl').style.display="";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                document.getElementById(
'FControl').style.display="none"
ExpandedSubBlockEnd.gif           }
 
ExpandedBlockEnd.gif}

None.gif
//当DropDownList的值为58时显示id为FControl的组件,否则不显示。
None.gif

转载于:https://www.cnblogs.com/JoeDZ/archive/2008/02/27/1083260.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值