Error: "is already declared as protected WithEvents" - asp.net

Web Controls
<asp:TextBox id="txtUsername" runat="server" CssClass="input-block-level" placeholder="Username" type="text" required></asp:TextBox>
<asp:TextBox id="txtPassword" runat="server" CssClass="input-block-level" placeholder="Password" type="password" required></asp:TextBox>
Code Behind
Inherits System.Web.UI.MasterPage
Protected WithEvents txtUsername As System.Web.UI.WebControls.Literal
Protected WithEvents txtPassword As System.Web.UI.WebControls.Literal
Error:
Parse Error: 'txtPassword' is already declared as 'Protected WithEvents txtPassword As System.Web.UI.Webcontrols.TextBox in' this class
'txtUsername' is already declared as 'Protected WithEvents txtUsername As System.Web.UI.Webcontrols.TextBox in' this class
What could be the cause of the error?

I know this is 3 years late, but I've just solved this problem and I'll share my solution to help out anyone stumbling here.
I got this error when I commented out a control, worked on other parts of the project, then uncommented it.
Simply change the ID of your control in your aspx and aspx.vb and the error will be gone.
<!-- changed from txt to text -->
<asp:TextBox id="textUsername" runat="server" CssClass="input-block-level" placeholder="Username" type="text" required></asp:TextBox>
<asp:TextBox id="textPassword" runat="server" CssClass="input-block-level" placeholder="Password" type="password" required></asp:TextBox>

When you add controls to the aspx page your controls are added to the partial class of the same name as your code behind file. If you add then again to the code file you will get the error.

You may have two or more Web Forms with the same class name.
Contents of somepage.aspx and anotherpage.aspx:
<%# Page ... Inherits="somepage" %>
...
Contents of somepage.aspx.vb and anotherpage.aspx.vb:
Partial Class somepage
...
End Class
In the sample code above, changing the Inherits attribute and class name in the anotherpage.* files to anotherpage fixes the problem. The code-behind looks a little different if you are using C#, but it's the same idea.

Related

txtAddress is not declared. I cannot reference TextBox inside of AJAX .NET VB Update Panel

Trying to fill a few textboxes using AJAX, VB .NET:
<div align="left">
<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="pnlAddresses" runat="server">
<ContentTemplate runat="server">
<asp:dropdownlist id="ddlVenueAddresses" Width="264px" Height="24px" runat="server" AutoPostBack="true" OnSelectedIndexChanged ="ddlVenueAddresses_SelectedIndexChanged" CssClass="admin" BackColor="Transparent" DataTextField="VenueName" DataValueField="VenueAddress"></asp:dropdownlist> (optional)
<br />
<input name="txtAddress" id="txtAddress" type="text" size="70" maxlength="100" value='<%= Request.Form.Get("txtAddress")%>'/>
<br />
<input name="txtGoogleAddress" type="text" size="70" maxlength="100" value='<%= Request.Form.Get("txtGoogleAddress")%>'/>
<br />
<input name="txtGoogleCity" type="text" size="70" maxlength="100" value='<%= Request.Form.Get("txtGoogleCity")%>'/>
<br />
<input name="txtGoogleState" type="text" size="70" maxlength="100" value='<%= Request.Form.Get("txtGoogleState")%>'/>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</td>
dropdown list gets populated and when item is picked, code stops on breakpoint where it should. But stepping through I get error when trying to just add simple "xxxx"s to textbox as a test:
Public Sub ddlVenueAddresses_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim txtAddressFound As TextBox
txtAddressFound = pnlAddresses.FindControl("txtAddress")
txtAddressFound.Text = "xxxxx"
End Sub
txtAddressFound is Null and when trying to set it to "xxxxx" I get this error:
System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
If I just trying to set txtAddress.Text = "xxxx" with no FindControl method, it says it doesn't exist:
? txtAddress
'txtAddress' is not declared. It may be inaccessible due to its protection level.
Totally lost. This application has been working for years, I just wanted to use Ajax to populate some address stuff.
Does anyone have any ideas?
I have tons of textboxes on this form (not using AJAX on them). I also put this at the top under Inherits System.Web.UI.Page with no luck.
Protected WithEvents txtAddress As System.Web.UI.WebControls.TextBox
Because you are trying to access client side control txtAddress from Server side script.
In ASP.NET, control should be
<asp:TextBox ID="txtAddress" runat="Server" />
This way you can access it from Server Side.
<input name="txtAddress" id="txtAddress" runat="server" type="text" size="70" maxlength="100" value='<%= Request.Form.Get("txtAddress")%>'/>
protected void ddlVenueAddresses_SelectedIndexChanged(object sender, EventArgs e)
{
txtAddress.Value = "XXXX";
}
I tested the above it works, please check ones. I kept the input html control as is but added runat="server" attribute, I am not sure why are you instantiating a new TextBox to access your input control.

Referencing a ASP:TextBox control - run time error

I have a file called Play.aspx with these controls:
<asp:TextBox ID="txtGuess" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Submit Guess" OnClick="PostGuess" />
The Sub "PostGuess" is in the CodeFile (Play.aspx.vb) and a line in this Sub references the TextBox using txtGuess.Text
I also have this line in Play.aspx.vb:
Protected WithEvents txtGuess As Global.System.Web.UI.WebControls.TextBox
This is the contents of my Play.aspx.designer.vb file:
Option Strict On
Option Explicit On
Partial Public Class Play
Protected WithEvents Head1 As Global.System.Web.UI.HtmlControls.HtmlHead
Protected WithEvents form1 As Global.System.Web.UI.HtmlControls.HtmlForm
Protected WithEvents ScriptManager1 As Global.System.Web.UI.ScriptManager
Protected WithEvents LoginView1 As Global.System.Web.UI.WebControls.LoginView
When I run the web app, I put some text in txtGuess and click Button1 and it gives me this error on the txtGuess.Text:
System.NullReferenceException: Object reference not set to an instance of an object
Any ideas? Cheers.
Issued has been FIXED
The problem was the textbox was inside a ASP:LoginView control

referencing a ASP:TextBox from CodeFile

I have 2 ASP controls in a page called Play.aspx:
<asp:TextBox ID="txtGuess" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Submit Guess" OnClick="PostGuess" />
The code at the top of my page is:
<%# Page Language="VB" AutoEventWireup="false" Debug="false" Inherits="Play" CodeFile="Play.aspx.vb" %>
In the CodeFile (Play.aspx.vb) I have a procedure called PostGuess which tries to reference the TextBox using the code: txtGuess.text
Visual Studio 2008 gives this error: Name 'txtGuess' is not declared.
When I access the page in a browser, I get: 'txtGuess' is not declared. It may be inaccessible due to its protection level.
This is the contents of the Play.aspx.designer.vb:
Option Strict On
Option Explicit On
Partial Public Class Play
Protected WithEvents Head1 As Global.System.Web.UI.HtmlControls.HtmlHead
Protected WithEvents form1 As Global.System.Web.UI.HtmlControls.HtmlForm
Protected WithEvents ScriptManager1 As Global.System.Web.UI.ScriptManager
Protected WithEvents LoginView1 As Global.System.Web.UI.WebControls.LoginView
End Class
It might be an easy fix, I am not very good at this, please help!
Declaring
Protected WithEvents txtGuess As Global.System.Web.UI.WebControls.TextBox
in the Play.aspx.vb file should be the ticket.

ASP.NET form using label

I have to create an asp.net form using label. I found an example for this task and tried to run it but nothing from the expected result appeared. Do you have any idea what else should be done to run it? (I'm still at the very beginnig of asp.net.)
Here is the code:
<%# Page Language="vb" %>
<script runat="server">
Sub submit(Sender As Object, e As EventArgs)
label1.Text=txt1.Text
End Sub
</script>
<html>
<body>
<form runat="server">
Write some text:
<asp:TextBox id="txt1" Width="200" runat="server"/>
<asp:Button id="b1" Text="Copy to Label" OnClick="submit" runat="server"/>
<p><asp:Label id="label1" runat="server"/></p>
</form>
</body>
</html>
Please check the default access modifier for your function(SUB).
In c# we use protected and from MSDN
Class members, including nested classes and structs, can be public,
protected internal, protected, internal, or private. The access level
for class members and struct members, including nested classes and
structs, is private by default. Private nested types are not
accessible from outside the containing type.
So try with Friend access modifier for VB.Net.

asp.net data binding in literal

Lets say we have the following in the default.aspx file
<asp:Literal runat="server" Text="<%= TestMethod() %>" />
What needs to be defined in the default.aspx.cs file to make this work?
I tried to add a method called TestMethod to the _Default class which simply returned the string Test, but it didn't seem to work.
Can anyone help?
Thanks,
AJ
Apart from the method being marked public...
i think you could also remove the asp:Literal altogether
example
your code
<p><asp:Literal runat="server" Text="<%= TestMethod() %>" /></p>
could be
<p><%= TestMethod() %></p>
However if you are intent on using the Literal then please rather set it on page load.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.literal.aspx
Regards.
I think you can get the same result by doing this
In your .aspx file
<asp:Literal runat="server" ID="ltr1" />
And in your aspx.cs file
ltr1.text = TestMethod();

Resources