Showing posts with label event. Show all posts
Showing posts with label event. Show all posts

Saturday, March 31, 2012

set an event in a ListBox in runtime

I want to create a ListBox in runtime

dim objCoun as new listbox
objCoun.id = "lbxCountries"
objCoun.Rows = "1"
objCoun.DataTextField = "Country_Name"
objCoun.DataValueField = "Country_Id"

But when I try to set the event

objCoun.OnSelectedIndexChanged="tro"

This is the error message:

Compiler Error Message: BC30390: 'System.Web.UI.WebControls.ListControl.Protected Overridable Sub OnSelectedIndexChanged(e As System.EventArgs)' is not accessible in this context because it is 'Protected'.

Source Error:

Line 44: objCoun.DataTextField = "Country_Name"
Line 45: objCoun.DataValueField = "Country_Id"
Line 46: objCoun.OnSelectedIndexChanged = "tro"
Line 47: objCoun.DataSource=dbread
Line 48: objCoun.DataBind()

How can I set the event in runtime?

Thanks for your helpTry:


AddHandler objCoun.SelectedIndexChanged, AddressOf tro

...assuming tro is the name of your event handler with the appropriate signature:

Private Sub tro(ByVal sender As System.Object, ByVal e As System.EventArgs)

"tro" is the subroutine name. That means when the objCoun changes its index then the action is sent to "tro"

Saturday, March 24, 2012

set focus after postback

How can I set the focus to a control after postback?I have some javascript function to set the focus in onload event,but it looks like the onload event is not trigger after postback.ThanksThis will do what you want:
-- focus and select in a textbox
Dim sb As New System.Text.StringBuilder

sb.Append("<script language=javascript>")
sb.Append("document.forms.myForm.txtMyBox.focus();")
sb.Append("document.forms.myForm.txtMyBox.select();")
sb.Append("</script>")

If Not IsStartupScriptRegistered("MyBoxSelect") Then
RegisterStartupScript("MyBoxSelect", sb.ToString())
End If


Also, you might have a look @.view post 821121

regards

Set Focus on a control

Hello,

On a click event, I need to test certain text boxes and then based on their
values go to another text box. How do you set the focus using vb?

--
Thanks in advance,

StevenLook at the "Focus in WebForms" thread earlier today...

Set focus on a TextBox

How can I set focus on a TextBox from within a button's
event handler.VB
Sub SetFocus(ByVal controlToFocus As Control)

Dim scriptFunction As New StringBuilder
Dim scriptClientId As String

scriptClientId = controlToFocus.ClientID

scriptFunction.Append("<script language='javascript'>")
scriptFunction.Append("document.getElementById('")
scriptFunction.Append(scriptClientId)
scriptFunction.Append("').focus();")
scriptFunction.Append("</script>")

RegisterStartupScript("focus", scriptFunction.ToString())

End Sub

C# (something like this)
Fucntion SetFocus(Control controlToFocus )
{
String scriptFunction;
String scriptClientId;

scriptClientId = controlToFocus.ClientID;

scriptFunction += ("<script language='javascript'>");
scriptFunction. += ("document.getElementById('");
scriptFunction. += (scriptClientId);
scriptFunction. += ("').focus();");
scriptFunction.Append("</script>");

RegisterStartupScript("focus", scriptFunction.ToString());

}
VB
Sub SetFocus(ByVal controlToFocus As Control)

Dim scriptFunction As New StringBuilder
Dim scriptClientId As String

scriptClientId = controlToFocus.ClientID

scriptFunction.Append("<script language='javascript'>")
scriptFunction.Append("document.getElementById('")
scriptFunction.Append(scriptClientId)
scriptFunction.Append("').focus();")
scriptFunction.Append("</script>")

RegisterStartupScript("focus", scriptFunction.ToString())

End Sub

C# (something like this)
Fucntion SetFocus(Control controlToFocus )
{
String scriptFunction;
String scriptClientId;

scriptClientId = controlToFocus.ClientID;

scriptFunction += ("<script language='javascript'>");
scriptFunction. += ("document.getElementById('");
scriptFunction. += (scriptClientId);
scriptFunction. += ("').focus();");
scriptFunction.Append("</script>");

RegisterStartupScript("focus", scriptFunction.ToString());

}
Here's a little script I got, and modified, from Charles Carroll, i think,
(www.learnasp.com).

Public Shared Sub SetFocus(ByVal ControlClientID As String, ByVal Page
As System.Web.UI.Page)
Dim scriptFunction As New System.Text.StringBuilder
Dim scriptClientId As String
scriptFunction.Append("<script language='javascript'>")
scriptFunction.Append("document.getElementById('")
scriptFunction.Append(ControlClientID)
scriptFunction.Append("').focus();")
scriptFunction.Append("</script>")
Page.RegisterStartupScript("focus", scriptFunction.ToString())
End Sub

ControlClientID is the ClientID property of your control (likely a textbox).

Karl

"mg" <mg@.theworld.com> wrote in message
news:084701c34a0e$91af53d0$a401280a@.phx.gbl...
> How can I set focus on a TextBox from within a button's
> event handler.
Here's a little script I got, and modified, from Charles Carroll, i think,
(www.learnasp.com).

Public Shared Sub SetFocus(ByVal ControlClientID As String, ByVal Page
As System.Web.UI.Page)
Dim scriptFunction As New System.Text.StringBuilder
Dim scriptClientId As String
scriptFunction.Append("<script language='javascript'>")
scriptFunction.Append("document.getElementById('")
scriptFunction.Append(ControlClientID)
scriptFunction.Append("').focus();")
scriptFunction.Append("</script>")
Page.RegisterStartupScript("focus", scriptFunction.ToString())
End Sub

ControlClientID is the ClientID property of your control (likely a textbox).

Karl

"mg" <mg@.theworld.com> wrote in message
news:084701c34a0e$91af53d0$a401280a@.phx.gbl...
> How can I set focus on a TextBox from within a button's
> event handler.

Set Focus to control after postback

I am usingVB with .net 1.1 and I wish to set focus to a textbox after the
autopostback event of a dropdownlist. When the user changes the selection of
my dropdownlist an onchange event occurs and the form does autopostback.
After the on change event I need to set focus to the next textbox. How do I
do this>On Oct 29, 1:08 pm, Victorious1
<Victorio...@.discussions.microsoft.comwrote:

Quote:

Originally Posted by

I am usingVB with .net 1.1 and I wish to set focus to a textbox after the
autopostback event of a dropdownlist. When the user changes the selection of
my dropdownlist an onchange event occurs and the form does autopostback.
After the on change event I need to set focus to the next textbox. How do I
do this>


This is C# syntax but the idea is the same for VB.

In the page load method have this:
Page.ClientScript.RegisterClientScriptBlock(this.G etType(),
"SomeName", <WebControlName>.focus());

Thursday, March 22, 2012

Set HTML Attribute In Page_Load Event

I want to be able to set an attribute of the HTML object in the form's
Page_Load event. I can't see how to do this. The main problem is that I
can't see how to access the HTML object from this event. Can anyone help ?

BenBen,

Forthe element you need to specify runat=server and id=myHtml. Then VS will
define the element for you in code-behind as the page's protected member. In
any page method you can set an attribute as (c# syntax):

myHtml.Attributes["myAttribute"]="my string";

Eliyahu

"Ben Foster" <ben.foster@.nospam.com> wrote in message
news:OozPcRBGFHA.2180@.TK2MSFTNGP10.phx.gbl...
> I want to be able to set an attribute of the HTML object in the form's
> Page_Load event. I can't see how to do this. The main problem is that I
> can't see how to access the HTML object from this event. Can anyone help ?
> Ben
Eliyahu,

That works fine. Thanks.

Ben

"Eliyahu Goldin" <removemeegoldin@.monarchmed.com> wrote in message
news:%23TIxNYBGFHA.3244@.TK2MSFTNGP15.phx.gbl...
> Ben,
> Forthe element you need to specify runat=server and id=myHtml. Then VS
> will
> define the element for you in code-behind as the page's protected member.
> In
> any page method you can set an attribute as (c# syntax):
> myHtml.Attributes["myAttribute"]="my string";
> Eliyahu
> "Ben Foster" <ben.foster@.nospam.com> wrote in message
> news:OozPcRBGFHA.2180@.TK2MSFTNGP10.phx.gbl...
>> I want to be able to set an attribute of the HTML object in the form's
>> Page_Load event. I can't see how to do this. The main problem is that I
>> can't see how to access the HTML object from this event. Can anyone help
>> ?
>>
>> Ben
>>
>>

Set HTML Attribute In Page_Load Event

I want to be able to set an attribute of the HTML object in the form's
Page_Load event. I can't see how to do this. The main problem is that I
can't see how to access the HTML object from this event. Can anyone help ?
BenBen,
Forthe element you need to specify runat=server and id=myHtml. Then VS will
define the element for you in code-behind as the page's protected member. In
any page method you can set an attribute as (c# syntax):
myHtml.Attributes["myAttribute"]="my string";
Eliyahu
"Ben Foster" <ben.foster@.nospam.com> wrote in message
news:OozPcRBGFHA.2180@.TK2MSFTNGP10.phx.gbl...
> I want to be able to set an attribute of the HTML object in the form's
> Page_Load event. I can't see how to do this. The main problem is that I
> can't see how to access the HTML object from this event. Can anyone help ?
> Ben
>
Eliyahu,
That works fine. Thanks.
Ben
"Eliyahu Goldin" <removemeegoldin@.monarchmed.com> wrote in message
news:%23TIxNYBGFHA.3244@.TK2MSFTNGP15.phx.gbl...
> Ben,
> Forthe element you need to specify runat=server and id=myHtml. Then VS
> will
> define the element for you in code-behind as the page's protected member.
> In
> any page method you can set an attribute as (c# syntax):
> myHtml.Attributes["myAttribute"]="my string";
> Eliyahu
> "Ben Foster" <ben.foster@.nospam.com> wrote in message
> news:OozPcRBGFHA.2180@.TK2MSFTNGP10.phx.gbl...
>