Postback后将焦点放在postback的源头上

 

控件在引起postback后,在__EVENTTARGET中有控件的信息(button除外).所以根据这个可以在

Postback后将焦点放在postback的源头上。

 

我写了一个基类,要实现这种效果的继承这个类就行了。




ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Page Language="VB" AutoEventWireup="false" CodeFile="Default4.aspx.vb" Inherits="Default4" %>
None.gif
None.gif
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
None.gif
None.gif
<html xmlns="http://www.w3.org/1999/xhtml" >
None.gif
<head runat="server">
None.gif    
<title>Untitled Page</title>
None.gif
</head>
None.gif
<body>
None.gif    
<form id="form1" runat="server">
None.gif    
<div>
None.gif        
<asp:Button ID="Button1" runat="server" Text="Button1" /><BR>
None.gif        
<asp:Button ID="Button2" runat="server" Text="Button2" /><BR>
None.gif        
<asp:Button ID="Button3" runat="server" Text="Button3" /><BR>
None.gif        
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton> <BR>
None.gif        
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack=true>
None.gif        
</asp:DropDownList>    
None.gif           
</div>
None.gif    
</form>
None.gif
</body>
None.gif
</html>

ExpandedBlockStart.gifContractedBlock.gifPartial Class Default4Class Default4
InBlock.gif    
Inherits Helper
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Protected  Sub Page_Load()Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load
InBlock.gif        
If Not IsPostBack Then
InBlock.gif            
Dim values As ArrayList = New ArrayList()
InBlock.gif            values.Add(
"IN")
InBlock.gif            values.Add(
"KS")
InBlock.gif            values.Add(
"MD")
InBlock.gif            values.Add(
"MI")
InBlock.gif            values.Add(
"OR")
InBlock.gif            values.Add(
"TN")
InBlock.gif            DropDownList1.DataSource 
= values
InBlock.gif            DropDownList1.DataBind()
InBlock.gif        
End If
ExpandedSubBlockEnd.gif    
End Sub

ExpandedBlockEnd.gif
End Class

None.gif


None.gifImports Microsoft.VisualBasic
None.gif
ExpandedBlockStart.gifContractedBlock.gif
Public Class HelperClass Helper
InBlock.gif    
Inherits System.Web.UI.Page
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub GetPostbackControl()Sub GetPostbackControl(ByVal targPage As Page)
InBlock.gif        
Dim ctl As Control = New Control
InBlock.gif        
Dim ctl_tmp As Control = New Control
InBlock.gif        
If (targPage.IsPostBack) Then
InBlock.gif            
Dim ctlName As String = targPage.Request.Form("__EVENTTARGET")
InBlock.gif            
If ((ctlName <> NothingAnd (ctlName <> String.Empty)) Then
InBlock.gif                ctl 
= targPage.FindControl(ctlName)
InBlock.gif            
Else
InBlock.gif                
For Each ctrl As String In targPage.Request.Form
InBlock.gif                    
If ctrl.EndsWith(".x"Or ctrl.EndsWith(".y"Then
InBlock.gif                        ctlName 
= ctrl.Substring(0, ctrl.Length - 2)
InBlock.gif                        ctl_tmp 
= targPage.FindControl(ctlName)
InBlock.gif                    
Else
InBlock.gif                        ctl_tmp 
= targPage.FindControl(ctrl)
InBlock.gif                    
End If
InBlock.gif                    
If Not IsNothing(ctl_tmp) Then
InBlock.gif                        
If ctl_tmp.ToString = "System.Web.UI.WebControls.Button" Or _
InBlock.gif                           ctl_tmp.ToString 
= "System.Web.UI.WebControls.LinkButton Then" Then
InBlock.gif                            ctl 
= ctl_tmp
InBlock.gif                            
Exit For
InBlock.gif                        
End If
InBlock.gif                    
End If
InBlock.gif                
Next
InBlock.gif
InBlock.gif            
End If
InBlock.gif            ctl.Focus()
InBlock.gif        
End If
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub Page_Load()Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load
InBlock.gif        GetPostbackControl(
CType(sender, Page))
ExpandedSubBlockEnd.gif    
End Sub

ExpandedBlockEnd.gif
End Class

None.gif

转载于:https://www.cnblogs.com/meluckeve/archive/2005/07/15/193628.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值