Showing posts with label loadi. Show all posts
Showing posts with label loadi. Show all posts

Monday, March 26, 2012

Set Focus

Hi, i'm working on MS visual.net using C# asp.net web application. Can someone help with the code to set focus on a textbox when the page is load?
I tried many ways including the examples in metabuilders.com but just couldn't do it.
Wat is the code that need to be insert during pageload.

Hope someone will help me with this. Thanks.

RegisterStartupScript("myfocus", "<SCRIPT>document.all." + TextBox1.ClientID + ".focus();</SCRIPT>");

Thanks for your input. But where do i place this code in? is it in the pageload part? Because there is an error message.

Thank you.
what is the error?
pls post code and error.
the same

in vb .net
'write this function in side server code

Private Sub mySetFocus(ByVal ctrl As System.Web.UI.Control)

Dim strFocus As String
strFocus = strFocus + "<SCRIPT language='javascript'>"
strFocus = strFocus + "document.getElementById('" & ctrl.ID & "').focus();"
'if want select
strFocus = strFocus + "if ( document.getElementById('" & ctrl.ID & "').select != null ){document.getElementById('" & ctrl.ID & "').select();}"

strFocus = strFocus + "</SCRIPT>"
RegisterStartupScript("mySetFocus", strFocus)

End Sub

'call whit control for focus
call mySetFocus(txtControl)
Is there any way that we can do this without using javascript?
AFAIK, that's a client side / browser issue,
so Javascript can utimately do this in ASP.NET
hi ChinHan:

If you want set focus to a textbox when the page load,

if so txtbox is a textbox control that if you want , you can do like this

<body onload="javascript:document.all.txtbox.focus();"></body>
Thanks.. rox.scott. manage to solve the problem.. Thanks a lot.

I have another qns.. after i enter the data in the textbox, i have a search button to find the information.. but i can't press the ENTER key to search it. i need to use a mouse click.. how do i solve this.. In windows form there is no problem but in asp.net web application this problem occurs..

Thanks!
Hello,

that is another task where you can use javascript to accomplish this. In my opinion one of the best way to solve this has made Janus Kamp Hansen with his script. I want to show you this in the following example:

Public Sub DefaultButton(ByRef Page As System.Web.UI.Page, ByRef objTextControl As TextBox, ByRef objDefaultButton As Button)

' Sets default buttons.
' Created by Janus Kamp Hansen - http://www.kamp-hansen.dk
Dim sScript As New System.Text.StringBuilder()

sScript.Append("<SCRIPT language=""javascript"">" & vbCrLf)
sScript.Append("function fnTrapKD(btn){" & vbCrLf)
sScript.Append(" if (document.all){" & vbCrLf)
sScript.Append(" if (event.keyCode == 13)" & vbCrLf)
sScript.Append(" { " & vbCrLf)
sScript.Append(" event.returnValue=false;" & vbCrLf)
sScript.Append(" event.cancel = true;" & vbCrLf)
sScript.Append(" btn.click();" & vbCrLf)
sScript.Append(" } " & vbCrLf)
sScript.Append(" } " & vbCrLf)
sScript.Append("}" & vbCrLf)
sScript.Append("</SCRIPT>" & vbCrLf)

objTextControl.Attributes.Add("onkeydown", "fnTrapKD(document.all." & objDefaultButton.ClientID & ")")
Page.RegisterStartupScript("ForceDefaultToScript", sScript.ToString)

End Sub

Put this code in your code-behind module and in the page load event you can define the default buttons for the ENTER-Key like in the following sample:
 DefaultButton(Me.Page, Me.txtField, Me.cmdDefaultButton)
For any explanation please look at the web-address in the code...

HTH,
If you are using ServerSide button control, then if you have your code in buttonX_Click event then on click on that buitton only that event fires.

But, there is an alternative solution for this.

If you enter data in TextBox and press Enter, then again Page_Load event will fires as my understanding.

So what you have to do is

on Page_Load check if the textbox has value or not, if the textbox has value then call your button_click event

Ex:

on page load event

If len(textbox1.text) > 0 Then
Button1_Click(sender,e)
End if

this kind of logic might helps to your problem!
hi kbrocksi SEC.. i'm working on a C# project. Do u have it in C#? thanks!
Hi, I am having the opposite problem...I have multiple textboxes on my page, and I have set focus to the first one...but when the user presses the enter key it fires the btnEdit_Click event...causing all kinds of issues. How do I stop it from running the button click and make it set focus to the next string??

I've tried capturing the keypress event of the form, to simply move it to another control, but that doesn't seem to work either.
here's the code:


frmMain.Attributes.Add("onkeypress", "frmMain_keyPress;")

Dim sbJavaFunction As StringBuilder = Nothing
sbJavaFunction = New StringBuilder

sbJavaFunction.Append("<script language=""javascript"">function frmMain_keyPress()")
sbJavaFunction.Append("{frmmain.lblTest.Text='hi';")
sbJavaFunction.Append("if (event.keyCode == 13){")
'sbJavaFunction.Append("event.cancelBubble = true;")
'sbJavaFunction.Append("event.returnValue = false;")
sbJavaFunction.Append("document.forms[0].txtTotalPairs.Focus;")
'sbJavaFunction.Append("document.forms[0].txtTurn_TextChanged();")
sbJavaFunction.Append("}}</script>")
Page.RegisterClientScriptBlock("HandleEnterKey", sbJavaFunction.ToString())
sbJavaFunction = Nothing

any ideas would be helpful!
Chinhan...here is the translation of the above code:

private void Page_Load(object sender, System.EventArgs e)
{
DefaultButton(this.Page, this.txtField, this.cmdDefaultButton);
}

public void DefaultButton(Page Page, TextBox objTextControl, Button objDefaultButton)
{
// Sets default buttons.
// Created by Janus Kamp Hansen - http://www.kamp-hansen.dk
System.Text.StringBuilder sScript = new System.Text.StringBuilder();

sScript.Append("<SCRIPT language='javascript'>\n");
sScript.Append("function fnTrapKD(btn){\n");
sScript.Append(" if (document.all){\n");
sScript.Append(" if (event.keyCode == 13)\n");
sScript.Append(" { \n");
sScript.Append(" event.returnValue=false;\n");
sScript.Append(" event.cancel = true;\n");
sScript.Append(" btn.click();\n");
sScript.Append(" } \n");
sScript.Append(" } \n");
sScript.Append("}\n");
sScript.Append("</SCRIPT>\n");

objTextControl.Attributes.Add("onkeydown", "fnTrapKD(document.all." + objDefaultButton.ClientID + ")");
Page.RegisterStartupScript("ForceDefaultToScript", sScript.ToString());
}


Daisy, why not use theonblur event

Thursday, March 22, 2012

Set MaxLength on form Load

Hi,

Does anyone know how to populate the maxlength property of an asp:textbox on page load?

I'm getting the value from the my config table in my DB

When I try setting myControl.MaxLength=10 in the onload event
It's not written to the control and when I enter text I'm not restricted at all

I've also tried

<asp:textbox id="Abstract" Runat="server" Wrap="True" Rows="11" Columns="45" TextMode="MultiLine" maxlength="<%=SummaryLimit%>"></asp:textbox>

' And

<asp:textbox id="Abstract" Runat="server" Wrap="True" Rows="11" Columns="45" TextMode="MultiLine" maxlength="<%=Convert.ToInt32(SummaryLimit)%>"></asp:textbox>
Geting error - <%=SummaryLimit%> is not a valid value for Int32.
And
<%=Convert.ToInt32(SummaryLimit)%> is not a valid value for Int32.

SummaryLimit Value is declared and assigned as an Int32
Dim m_iSummaryLimit As Int32 = 250

Public Property SummaryLimit() As Int32
Get
Return Convert.ToInt32(m_iSummaryLimit)
End Get
Set(ByVal Value As Int32)
m_iSummaryLimit = Convert.ToInt32(Value)
End Set
End PropertyThe textbox control when in multiline mode actually renders as a HTML textarea therefore on a textarea there is no maxlength property so maxlength does not work. One of the ways to control this is to use javascript instead.
Basilisk,

Thanks for that, just wasted the whole morning trying to get it working.

Cheers Al
I did want to pipe in and say that when you use the following:

<%= ... %>

in an item tag, you need to surround it with single quotes - ' - and not double quotes - " -.
Lord Rat,

Thanks, feel free to pop out of your cave with useful tips like that at anytime :D

Cheers Al