为ASP.NET页面中的输入框“选择”值(代替直接输入)

在Windows应用程序中,可以使用日期控件选择日期,代替直接输入,这样的好处是日期格式不会错误,而且界面比较友好。又比如我需要在输入框中填写一个单位的编码,可是单位的编码很难记,需要查询得到,这在WinForm中同样很容易实现,在WebForm中,我们模仿WinForm的实现方式:打开一个新窗口,在新窗口中查询得到结果,选择记录行,将结果返回到父窗口的相应输入框中。

需要输入数据的界面(父窗口):InputData.aspx

None.gif < asp:TextBox  id ="txtDepartID"  runat ="server"   ></ asp:TextBox >
None.gif
< href ="javascript:linkfind_window=window.open('getDepart.aspx?controlname=Form1.txtDepartID','linkfind_window','width=500,height=500');linkfind_window.focus()" > 选择 </ a >

打开的新窗口的页面为:getDepart.aspx,参数为controlname=Form1.txtDepartID,其中Form1为页面InputData.aspx中的表单名称(ID),txtDepartID为输入框控件的ID。

子页面:getDepart.aspx
在该页面中用DataGrid或者DataList列出所有的单位编码和单位名称,这样操作者就非常清楚,直接用鼠标点击相应的行,将选择的单位的编码返回到父页面InputData.aspx的控件txtDepartID中。
其实将值返回父页面非常简单,语法如下:
javascript:window.opener.Form1.txtDepartID.value = '0401-203';
它表示将'0401-203'赋值给父窗口的输入框txtDepartID。

相信在浏览器的两个窗口中传递值的语法大家都很熟悉,现在的问题是需要在子窗口的DataGrid中每行都加入相应的代码,使得鼠标点击相应的行可以将行中相应的值返回到父页面,并关闭页面本身。为了使选择数据更加生动一些,可以加入行背景变色的效果。

下面是最终的显示效果的html代码:

None.gif < tr  onmouseover ="this.style.backgroundColor='#cccccc'"  onmouseout ="this.style.backgroundColor='white'"  onclick ="javascript:window.opener.Form1.txtDepartID.value = '0401-203';window.close();" >
None.gif        
< td > 0401-203 </ td >< td >测试单位名称一 </ td >
None.gif
</ tr >

可以看到,我们加入了背景变色的动态效果,但是最关键的是需要生成onclick的代码,在我的另外一片文章《用户操作“确认” 》中,介绍了如何在DataGrid中为单元格中的控件添加JavaScript代码,我们这里可以借鉴,但是区别在于:
1、为DataGrid的每行添加JavaScript代码,而不是单元格
2、需要取得DataGrid的每行的Cell(0)的值

我直接使用了前面的代码进行修改,代码如下:

ExpandedBlockStart.gif ContractedBlock.gif          Private   Sub dgDepartment_ItemCreated() Sub dgDepartment_ItemCreated(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgDepartment.ItemDataBound
InBlock.gif            
Dim dgi As DataGridItem = e.Item
InBlock.gif            
Dim strUrl As New StringBuilder(64)
InBlock.gif
InBlock.gif            
If (dgi.ItemType = ListItemType.Item OrElse dgi.ItemType = ListItemType.AlternatingItem) Then
InBlock.gif                
' 本例只检查DataGrid的Item和AlternatingItem行
InBlock.gif
                dgi.Attributes.Add("onmouseover""this.style.backgroundColor='#cccccc'")
InBlock.gif                dgi.Attributes.Add(
"onmouseout""this.style.backgroundColor='white'")
InBlock.gif                dgi.Attributes.Add(
"onclick", strUrl.Append("javascript:window.opener.").Append("Form1.txtDepartID").Append(".value = '").Append(dgi.Cells(0).Text).Append("';window.close();").ToString)
InBlock.gif            
End If
ExpandedBlockEnd.gif        
End Sub

实际运行时,DataGrid的每行都生成了前面所示的代码,但是value的值却都是空,而不是Cells(0)单元格中的单位编,经过跟踪程序发现在DataGrid的ItemCreated事件中,只有每一项ItemCreated事件发生以后Cells(i).Text才有值,而在事件发生时是没有值的。看来需要在其他事件发生时进行处理才能得到需要的值。查看了一下DataGrid的事件列表,发现ItemDataBound事件,将代码移动到该事件中,一切正常。最终代码如下:

ExpandedBlockStart.gif ContractedBlock.gif          Private   Sub dgDepartment_ItemDataBound() Sub dgDepartment_ItemDataBound(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgDepartment.ItemDataBound
InBlock.gif            
Dim dgi As DataGridItem = e.Item
InBlock.gif            
Dim strUrl As New StringBuilder(64)
InBlock.gif
InBlock.gif            
If (dgi.ItemType = ListItemType.Item OrElse dgi.ItemType = ListItemType.AlternatingItem) Then
InBlock.gif                
' 本例只检查DataGrid的Item和AlternatingItem行
InBlock.gif
                dgi.Attributes.Add("onmouseover""this.style.backgroundColor='#f1f5e1'")
InBlock.gif                dgi.Attributes.Add(
"onmouseout""this.style.backgroundColor='white'")
InBlock.gif                dgi.Attributes.Add(
"onclick", strUrl.Append("javascript:window.opener.").Append(Convert.ToString(ViewState("FORMCONTROL"))).Append(".value = '").Append(dgi.Cells(0).Text).Append("';window.close();").ToString)
InBlock.gif            
End If
ExpandedBlockEnd.gif        
End Sub


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值