473,899 Members | 3,984 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Set Focus on PostBack

8 New Member
Well I wasn't to sure if post this on the javascript forum or here, but since is an error that probably I got because something related to my ASP.Net code I decided to posted here.

Ok my problem is that I need to set focus on a textbox everytime the page is loaded. Maybe you already thinking what I thought at first, that with a textbox.setFocu s() that will do it. Nop this is .Net Framework 1.1 and textbox.setFocu s() didn't exist back then. So I did the other simple thing add a javascript on body onload="documen t.getElementByI d(<%= txtPCode.Client ID %>).focus(). It works on page load but not in postback. I really have try a couple of other things like adding this function in the .vb
Expand|Select|Wrap|Line Numbers
  1.  
  2.         Private Sub SetFocus(ByVal ClientID As String)
  3.             Dim Script As New System.Text.StringBuilder
  4.             'Dim ClientID As String = FocusControl.ClientID
  5.             With Script
  6.                 .Append("<script language='javascript'>")
  7.                 .Append("document.getElementById('")
  8.                 .Append(ClientID)
  9.                 .Append("').focus();")
  10.                 .Append("</script>")
  11.             End With
  12.             RegisterStartupScript("setFocus", Script.ToString())
  13.         End Sub
  14.  
and calling it in from the Page_Load like this SetFocus(textbo x.ClientID).

Anyway nothing of this have work to set the focus of the textbox on the page post back.

Anyone have another idea?

Thanks,

Erick
Jul 20 '07
24 3930
Frinavale
9,735 Recognized Expert Moderator Expert
It doesn't seem to work with a asp button, I got this error:
Compiler Error Message: BC30390: 'ASPNET.Starter Kit.Portal.Prod uctLookUpPage.P rivate Sub SetFocus(Client ID As String)' is not accessible in this context because it is 'Private'.
Could you post the html code for your aspx page?
Jul 27 '07 #21
nateraaaa
663 Recognized Expert Contributor
It doesn't seem to work with a asp button, I got this error:
Compiler Error Message: BC30390: 'ASPNET.Starter Kit.Portal.Prod uctLookUpPage.P rivate Sub SetFocus(Client ID As String)' is not accessible in this context because it is 'Private'.
'07/20/07 This procedure sets focus to specific controls.
Private Sub SetFocus(ByVal ClientID As String)
Dim Script As New System.Text.Str ingBuilder
'Dim ClientID As String = FocusControl.Cl ientID
With Script
.Append("<scrip t language='javas cript'>")
.Append("docume nt.getElementBy Id('")
.Append(ClientI D)
.Append("').foc us();")
.Append("</script>")
End With
RegisterStartup Script("setFocu s", Script.ToString ())
End Sub

Change the SetFocus method to be a public method instead of a private method.

Nathan
Jul 27 '07 #22
erickme
8 New Member
Here is the html

Expand|Select|Wrap|Line Numbers
  1.     <body bottomMargin="0" leftMargin="0" topMargin="0" rightMargin="0" marginwidth="0" marginheight="0">
  2.         <form runat="server">
  3.             <table cellSpacing="0" cellPadding="0" width="100%" border="0">
  4.                 <tr vAlign="top">
  5.                     <td colSpan="2"><asp:placeholder id="phBanner" runat="server"></asp:placeholder></td>
  6.                 </tr>
  7.                 <tr>
  8.                     <td class="contentCellTop" vAlign="top">
  9.                         <center><br>
  10.                             <table width="650" border="0">
  11.                                 <tr>
  12.                                     <td>
  13.                                         <TABLE id="Table1" cellSpacing="2" cellPadding="2" width="100%" border="0">
  14.                                             <TR>
  15.                                                 <TD>
  16.                                                     <asp:label id="lblTitle" runat="server" CssClass="Head"> Product Locate</asp:label></TD>
  17.                                                 <TD>
  18.                                                     <P align="right">[
  19.                                                         <asp:linkbutton id="lnkReturn0" CssClass="CommandButton" Visible="True" Runat="server" EnableViewState="True"
  20.                                                             Enabled="True" CausesValidation="False">return</asp:linkbutton>]</P>
  21.                                                 </TD>
  22.                                             </TR>
  23.                                         </TABLE>
  24.                                         <HR noShade SIZE="1">
  25.                                         <span class="normal" id="spnContent" runat="server">
  26.             <P>Enter a Product Code (PCode) or look it up using the look-up function provided.&nbsp; Once you select a product, 
  27.                                                 you can find a display of stores which have the product in stock as of&nbsp;end 
  28.                                                 of business&nbsp;yesterday.</P>
  29.             <P><asp:validationsummary id="vsPCode" runat="server" CssClass="Error" Width="100%" HeaderText="The following error(s) occurred:"></asp:validationsummary></P>
  30.             <P align="center"><asp:textbox id="txtPCode" runat="server" Width="63px" ToolTip="Enter the PCode into this textbox."></asp:textbox><asp:requiredfieldvalidator id="rfvPCode" runat="server" CssClass="strError" ToolTip="You must enter a valid PCode to continue."
  31.                                                     ControlToValidate="txtPCode" Display="Dynamic" ErrorMessage="You must enter a valid PCode to continue.">!</asp:requiredfieldvalidator><asp:regularexpressionvalidator id="revPCode" runat="server" CssClass="strError" ToolTip="The PCode can only contain 5 or 6 numbers."
  32.                                                     ControlToValidate="txtPCode" Display="Dynamic" ErrorMessage="The PCode can only contain 5 or 6 numbers." ValidationExpression="\d{5,6}">!</asp:regularexpressionvalidator><asp:customvalidator id="cvPCode" runat="server" CssClass="strError" ToolTip="The Product Code entered was not found."
  33.                                                     Display="Dynamic" ErrorMessage="The Product Code entered was not found." EnableClientScript="False" OnServerValidate="ValidatePCode">!</asp:customvalidator>&nbsp;
  34.                                                 <asp:button id="cmdLocate" runat="server" CssClass="CommandButton" ToolTip="Click here to locate the product."
  35.                                                     Text="Locate"></asp:button>&nbsp; <INPUT class="CommandButton" id="cmdLookup" onclick="this.disabled = true; NewWindow('product_lookup.aspx','ProductLookUp',550,500,0,'center');"
  36.                                                     type="button" value="Look-Up PCode" name="cmdLookup"></P>
  37.             <P align="center">
  38.                                                 <TABLE id="tblInfoContainment" cellSpacing="2" cellPadding="2" width="475" border="0">
  39.                                                     <TR>
  40.                                                         <TD vAlign="top">
  41.                                                             <TABLE id="tblResultsLabels" cellSpacing="3" cellPadding="3" border="0">
  42.                                                                 <TR>
  43.                                                                     <TD class="strNormal" colSpan="4">Click the LOCATE button to initiate the query.</TD>
  44.                                                                 </TR>
  45.                                                                 <TR class="Normal">
  46.                                                                     <TD>
  47.                                                                         <asp:label id="lblPCodeValue" runat="server" CssClass="strNormal"></asp:label></TD>
  48.                                                                     <TD colSpan="2">
  49.                                                                         <P align="left">
  50.                                                                             <asp:label id="lblDescValue" runat="server" CssClass="Normal"></asp:label></P>
  51.                                                                     </TD>
  52.                                                                     <TD>
  53.                                                                         <asp:label id="lblSizeValue" runat="server" CssClass="Normal"></asp:label></TD>
  54.                                                                 </TR>
  55.                                                                 <TR>
  56.                                                                     <TD colSpan="2">
  57.                                                                         <asp:Label id="lblWHHead" runat="server" CssClass="strNormal" Visible="False">Warehouse O/H:</asp:Label>&nbsp;
  58.                                                                         <asp:Label id="lblWHQty" runat="server" CssClass="Normal"></asp:Label></TD>
  59.                                                                     <TD colSpan="2">
  60.                                                                         <asp:Label id="lblStoreHead" runat="server" CssClass="strNormal" Visible="False">Your Store O/H:</asp:Label>&nbsp;
  61.                                                                         <asp:Label id="lblStoreQty" runat="server" CssClass="normal" Visible="False"></asp:Label>&nbsp;</TD>
  62.                                                                 </TR>
  63.                                                             </TABLE>
  64.                                                         </TD>
  65.                                                         <TD vAlign="bottom"><INPUT id="btnPriceIt" type="button" value="Price It!" name="btnPriceIt" runat="server"
  66.                                                                 class="CommandButton" tooltip="Click here to see the prices of the product in the different price zones">&nbsp;</TD>
  67.                                                     </TR>
  68.                                                 </TABLE>
  69.                                             </P>
  70.             <P align="center">
  71.                                             </P>
  72.             <P align="center"><asp:datalist id="dlProductNumbers" runat="server" CssClass="Normal" Width="100%" RepeatColumns="5"
  73.                                                     CellPadding="3" BorderColor="#DEBA84" BorderStyle="None" BackColor="#DEBA84"
  74.                                                     GridLines="Both" BorderWidth="1px" CellSpacing="2">
  75.                                                     <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#738A9C"></SelectedItemStyle>
  76.                                                     <HeaderTemplate>
  77.                                                         <TABLE id="tblDataHeader" cellSpacing="0" cellPadding="3" width="100%" border="0">
  78.                                                             <TR>
  79.                                                                 <TD align="center" class="strNormal" style="color: #ffffff;">The Product has been 
  80.                                                                     found in the following Stores:</TD>
  81.                                                             </TR>
  82.                                                         </TABLE>
  83.                                                     </HeaderTemplate>
  84.                                                     <FooterStyle ForeColor="#8C4510" BackColor="#F7DFB5"></FooterStyle>
  85.                                                     <ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>
  86.                                                     <ItemTemplate>
  87.                                                         <table id="tblDataItem" border="0" cellpadding="3" cellspacing="0" width="100%">
  88.                                                             <TR>
  89.                                                                 <td align="left" class="strNormal"><a href='<%# "../store_info_lookup.aspx?storenumber=" & DataBinder.Eval(Container.DataItem, "STORE_NO")%>'>Store
  90.                                                                         <%# DataBinder.Eval(Container.DataItem, "STORE_NO")%>
  91.                                                                     </a>
  92.                                                                     <span class="Normal">(<%# DataBinder.Eval(Container.DataItem, "END_QTY")%>)</span>
  93.                                                             </TR>
  94.                                                         </table>
  95.                                                     </ItemTemplate>
  96.                                                     <HeaderStyle Font-Bold="True" ForeColor="#DEBA84" BackColor="#A55129"></HeaderStyle>
  97.                                                     <AlternatingItemTemplate>
  98.                                                         <table id="tblDataAltItem" border="0" cellpadding="3" cellspacing="0">
  99.                                                             <TR>
  100.                                                                 <td align="left" class="strNormal"><a href='<%# "../store_info_lookup.aspx?storenumber=" & DataBinder.Eval(Container.DataItem, "STORE_NO")%>'>Store
  101.                                                                         <%# DataBinder.Eval(Container.DataItem, "STORE_NO")%>
  102.                                                                     </a>
  103.                                                                     <span class="Normal">(<%# DataBinder.Eval(Container.DataItem, "END_QTY")%>)</span>
  104.                                                             </TR>
  105.                                                         </table>
  106.                                                     </AlternatingItemTemplate>
  107.                                                 </asp:datalist></P>
  108.             <P align="left"><STRONG><U>Tip</U>:</STRONG> Hold down the shift&nbsp;key when you click on a store number to open 
  109.                                                 the store information in a new window.</P>
  110.             <P>[<asp:linkbutton id="lnkReturn1" runat="server" cssclass="CommandButton" borderstyle="none" text="Return"
  111.                                                     causesvalidation="False">return</asp:linkbutton>]</span></P>
  112.                                         <P>&nbsp;</P>
  113.                                     </td>
  114.                                 </tr>
  115.                             </table>
  116.                         </center>
  117.                     </td>
  118.                 </tr>
  119.                 <tr>
  120.                     <td class="contentCellBottom">&nbsp;</td>
  121.                 </tr>
  122.                 <tr>
  123.                     <td class="FootBg" vAlign="middle" height="30">
  124.                         <div align="center"><span class="SiteLink"><font color="#eeeeee">Thank you for visiting 
  125.                                     ABCNet.</font></span></div>
  126.                     </td>
  127.                 </tr>
  128.             </table>
  129.         </form>
  130.     </body>
  131.  
Jul 27 '07 #23
erickme
8 New Member
'07/20/07 This procedure sets focus to specific controls.
Private Sub SetFocus(ByVal ClientID As String)
Dim Script As New System.Text.Str ingBuilder
'Dim ClientID As String = FocusControl.Cl ientID
With Script
.Append("<scrip t language='javas cript'>")
.Append("docume nt.getElementBy Id('")
.Append(ClientI D)
.Append("').foc us();")
.Append("</script>")
End With
RegisterStartup Script("setFocu s", Script.ToString ())
End Sub

Change the SetFocus method to be a public method instead of a private method.

Nathan
I set it to public but still gives me an error
Jul 27 '07 #24
Frinavale
9,735 Recognized Expert Moderator Expert
It doesn't seem to work with a asp button, I got this error:
Compiler Error Message: BC30390: 'ASPNET.Starter Kit.Portal.Prod uctLookUpPage.P rivate Sub SetFocus(Client ID As String)' is not accessible in this context because it is 'Private'.
You are getting this error because your button has a runat=server.
So since it is an asp button, it is trying to call the server side code called SetFocus (except this function is private and so you get an error). It is not calling the JavaScript function called SetFocus as because it is an asp button and is calling the Server Side code.

In order to call JavaScript using an asp button you'd have to do something like the following in your in your server side code:

BTN_MyButton.At tributes.Add("O nMouseUP", "SetFocus(' " + TXT_myTextBox.C lientID"');")

However, the asp button will cause a PostBack event.
Since this happens your focus will be lost when the page is redisplayed in the browser.

If you were following the example from the w3c site and substituted the <input type="button"> for <asp:Button>, the example will obviously not work for these reasons.

-Frinny
Jul 27 '07 #25

Sign in to post your reply or Sign up for a free account.

Similar topics

2
5197
by: Marco Liedekerken | last post by:
Hi, Is it possible to retrieve the control that had the focus when the page was posted back? Because the focus is lost when a postback occurs I want to manually set the focus to the control that previously had the focus (smartnavigation doesn't do the trick). Thanks, Marco
2
2441
by: Elliot M. Rodriguez | last post by:
Is it possible to change a control's focus at runtime? I'm sure you can.... I have a form with 2 textbox controls that cause postbacks. They are located in the middle of my form. When a postback occurs, no control has the focus. I would like it to be so that the control that causes the postback keeps the focus. Since I have 2 controls that potentally need to retain focus, using Javascript in this case is a PIA.
6
1391
by: Mike | last post by:
I have a few textbox controls that have autopostback so that when they loose focus they update a label control that shows the count of characters in their respective text control. This works fine, except that after the postback, the page is shown reset -- scrolled to the top and with no control having focus. Can I maintain scroll/focus w/o writing client-side scripting? thanks
3
1650
by: Dexter | last post by:
Hello All, I have a web control that when receive the focus, a postback is called, using getPostBackEventReference. But, when the PostBack is called, i want that the focus goes to the web control. Somebody know how to make this? Dexter
1
1187
by: psual | last post by:
Hi I have a page (1st page) with a button. this button opens a new page (2nd page) in full-screen mode. The problem is that the 2nd page is always loaded before the postback of the 1st page, so the 1st page takes back the focus instead of remaining behind the 2nd page. Even if the 2nd page takes time to render, the 1st page waits for its
5
2047
by: Finn Stampe Mikkelsen | last post by:
Hi How can i set a focus to a textbox in my codebehind page?? I have this WebForm, that takes information from a user and 2 buttons on the form. One that takes action on the entered information and one that resets the form... The second on, the reset button, i would like to set focus back to the first textbox on the form. I already have an onload in my body, which sets the
2
3887
by: Rey | last post by:
Howdy all. Using visual web developer (VB) on xp pro box. My problem with with a web form that on accessing the calendar control causes a postback that moves the cursor back to the txtFirstName field. Form uses the Page_LoadComplete in order to access the last employeeID # in a dataview which is populated by an sqlDataObject querying an SQL Server dbs.
0
1793
by: BizEd | last post by:
I have a textbox that fires an autopostback when filled in. The next field after the textbox is a RadioButtonList which I set focus to. The radiobutton list does have focus and activates when you hit the spacebar, but the little selection box does not appear around the first item like it does when you tab to a radiobutton list. So in essence, the user has no idea where they are on the screen. I've tried both the focus() method and...
8
6914
by: Mel | last post by:
I have several text boxes and drop-down lists in an AJAX Update Panel. All user inputs have the Postback property set to True. After I type something in the first input entry and press the "Tab" key how can I set the focus to the next box after the postback? Please help! Using Visual Studio 2005 Pro, Asp.net 2.0, vb.net, WinXP
0
9997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11269
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10863
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10966
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9666
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5886
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4720
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4299
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.