Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
- I have drag-dropped an ASP button on to a webform. When someone clicks the button, I want it's text property to change.

- This is the HTML for it
HTML
<asp:Button ID="LoginButton" runat="server" Text="Login" OnClick="LoginButton_Click" />



- and this is the vb code behind

VB
Protected Sub LoginButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        LoginButton.Text = "123"
    End Sub


- i'm getting the error "Name 'Loginbutton' is not declared'. And this is really funny, cause the intellisense detects it just fine!

- btw this is an ASP.NET web application....the code works just fine on an ASP.NET website.
Posted
Updated 29-Apr-10 2:26am
v2

An alternative could be to use the sender property of the event handler. I believe this will have the same effect:

VB
Protected Sub LoginButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim button as Button = CType(sender, Button)
    button.Text = "123"
End Sub


If you get in to the habit of this it can make your event handlers easier to reuse, depending on your desired behaviour. This event handler will always change the clicked button's text to 123, regardless of the name of the button. However, you will need to implement the normal checks to make sure that sender actually is a button, otherwise you'll get exceptions thrown.

Disclaimer: I just typed this code as is straight in to here, I haven't tested it and it's been a little while since I used VB, so you might have to sort out syntax errors and incorrect Type names, etc., but hopefully you get the idea.
 
Share this answer
 
thanks...apparently i need to initialize the control....strange that ASP.NET websites (as opposed to web applications) dont require this.
 
Share this answer
 
hello,

your query is very simple all u have to do is write the following code in button's click event

VB
Protected Sub LoginButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginButton.Click
       LoginButton.Text = "abc"
   End Sub



I really dont know why its given u error, ur code is also fine
do rate my answer once u find it useful

Thanks & regards
Radix :rose:
 
Share this answer
 
Hi,

please try to override oninit event of your page where you are using this button as the follow:

C#:
C#
protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    btnLoginButton = new Button();
}


VB.NET:
VB
Protected Overloads Overrides Sub OnInit(e As EventArgs)
    MyBase.OnInit(e)
    btnLoginButton = New Button()
End Sub


Regards,
Jamil

check my latest article:
http://www.codeproject.com/Tips/74491/Silverlight-Html-Host-Control.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900