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
Related
I have a dropdownlist that, when a specific item is selected, needs to set another dropdownlist.visible = true. Unfortunately, I cannot get the dropdownlist (or any other dropdownlist on my entire site) to fire its SelectedIndexChanged event.
Details:
This is a site that was created by my predecessor and I do not have the time to rebuild it or even upgrade it so, I really need to simply add this one feature to complete my task.
The site is written in Visual Studio .NET 2003.
Currently, no matter what I try, the SelectedIndexChanged event never fires.
I have tried the following things:
Set AutoPostBack="true"
Set AutoEventWireup="true"
Set EnableViewState="true"
Add OnSelectedIndexChanged="ddlCLSortOptions_SelectedIndexChanged" to the asp:dropdownlist object, which gives me the following error:
Compiler Error Message: BC30456: 'ddlCLSortOptions_SelectedIndexChanged' is not a member of 'ASP.reports_aspx'.
This error has led me down the path of checking any differences in the Inherits and Class name, but the only difference I found is that the class is named reports and the Inherits is ProviderLicense.reports. Of course, when I change them to match identically, that throws a completely different error.
Change the ddlCLSortOptions_SelectedIndexChanged Sub from Private to Protected, and then even to Public just for good measure.
Remove the Handles ddlCLSortOptions.SelectedIndexChanged and add the AddHandler option
I'm sure there's even more that I've tried, but I can't think of anything else.
Here is my code:
reports.aspx
<%# Page Language="vb" AutoEventWireup="true" Codebehind="reports.aspx.vb" Inherits="ProviderLicense.reports"%>
...
<asp:dropdownlist id="ddlCLSortOptions" style="POSITION: absolute; LEFT: 365px; Z-INDEX: 140; TOP: 315px" runat="server" AutoPostBack="True" Visible="False">
<asp:ListItem Value="Provider" Selected="True">Provider</asp:ListItem>
<asp:ListItem Value="RecDate">Received Date</asp:ListItem>
<asp:ListItem Value="EntDate">Entered Date</asp:ListItem>
<asp:ListItem Value="DateComp">Date Completed</asp:ListItem>
<asp:ListItem Value="LicType">License Type</asp:ListItem>
<asp:ListItem Value="Investigator">Investigator</asp:ListItem>
<asp:ListItem Value="Worker">Worker</asp:ListItem>
<asp:ListItem Value="Disposition">Disposition</asp:ListItem>
</asp:dropdownlist>
reports.aspx.vb
Public Class reports
Inherits System.Web.UI.Page
Protected WithEvents ddlCLSortOptions As System.Web.UI.WebControls.DropDownList
...
Private Sub ddlCLSortOptions_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlCLSortOptions.SelectedIndexChanged
Label2.Text = "IT WORKED!" 'Just to see if the event fires
If ddlCLSortOptions.SelectedValue = "Disposition" Then
lblDisposition.Visible = True
ddlDisposition.Visible = True
Else
lblDisposition.Visible = False
ddlDisposition.Visible = False
End If
End Sub
I hope it's something simple that I've just missed, but I'm open to trying just about anything now.
have you tried this?
set AutoEventWireUp="false" in reports.aspx
set the OnSelectedIndexChanged="ddlCLSortOptions" in the DropDownList tag in reports.aspx
change Private Sub to Protected Sub on the OnSelectedIndexChanged event in reports.aspx.vb
I have a user control inside the control have a combobox, buttons, DropDownList etc in my code add the reference:
<%# Register Src="~/UserControls/UCCom.ascx" TagName="UCCom" TagPrefix="ucc" %>
<ucc:UCCom ID="UCCom" runat="server" MuestraFiltros="true" />
in the code behind i have an object, for example: this code:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim ctrx As New UCCom
End
but when i try to modify some combobox or textbox i see a message: Cannot access Protected field combobox here
The code in the user control:
<td style="width: 100px">
<asp:DropDownList ID="CboUsersCom" runat="server" DataTextField="name" AutoPostBack="true"
DataValueField="cve_user" ClientIDMode="Static" Enabled="false" Width="100px">
</asp:DropDownList>
</td>
thanks for yours comments.
In your user control designer page i.e. usercontrol.ascx.designer change the access modifier of combobox from 'protected' to 'public' like below. Then you can access that control in other pages globally.
The sample code demonstrates textbox set to 'public'.
'------------------------------------------------------------------------------
'
' This code was generated by a tool.
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
'
'------------------------------------------------------------------------------
Option Strict On
Option Explicit On
Partial Public Class UserControl
'''<summary>
'''txtDateTime control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Public WithEvents txtDateTime As Global.System.Web.UI.WebControls.TextBox
'''<summary>
'''btnCalendar control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents btnCalendar As Global.System.Web.UI.WebControls.ImageButton
'''<summary>
'''ajaxCalendar control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents ajaxCalendar As Global.AjaxControlToolkit.CalendarExtender
End Class
in your Page_Load method, you can do as below:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim dropDownList As DropDownList= TryCast(UCCom.FindControl("CboUsersCom"), DropDownList)
dropDownList.Enabled = True
End
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.
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.
Button Layout
I have 3 labels inside of a div. I want to make the div clickable and trigger a postback from clicking the div. In essensce I want to make a custom button that looks like the picture. So far the only way I found out of doing this is either having the div onclick event trigger javascript, or do some custom usercontrol magic, which i cant get to work.
User Control
<div class="meetingContainer" >
<div class="meetingCityState">
<asp:Label ID="lblMeetingCityState" runat="server" Text='<%# Eval("City") %>'></asp:Label>
</div>
<div>
<asp:Label ID="lblMeetingDate" runat="server" Text='<%# Eval("MeetingDate") %>'></asp:Label>
</div>
<div>
<asp:Label ID="lblMeetingSite" runat='server' Text='<%# Eval("Location") %>'></asp:Label>
</div>
</div>
code behind
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Partial Public Class MeetingButton
Inherits System.Web.UI.UserControl
Implements System.Web.UI.IPostBackEventHandler
Public Event Click As EventHandler
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Overridable Sub OnClick(ByVal E As EventArgs)
RaiseEvent Click(Me, E)
End Sub
Public Sub RaisePostBackEvent(ByVal eventArgument As String) _
Implements IPostBackEventHandler.RaisePostBackEvent
OnClick(New EventArgs())
End Sub
End Class
Yes, you can do this, using the __doPostBack method. You can create a custom reference to this from the server using this method: http://msdn.microsoft.com/en-us/library/ms153112(v=vs.80).aspx.
Otherwise, you can manually create it by passing it this:
<div id="<Server control's Client ID>" name="<Server control's Unique ID>" onclick="__doPostBack('<unique name>', '<command name and args>');">
On the server, check the following collection for the event:
string target = Request.Form["__EVENTTARGET"];
if (target != null && target.EndsWith("<id>"))
{
// do this
}
You need to math the __doPostBack unique name to the UniqueID property of the user control.
This would automatically invoke the method defined by the IPostBackEventHandler; any control that implements this interface has its RaisePostBackEvent method called... however, I'm not 100% sure it works the same way for user controls...
looks link an asp:linkbutton will do what I need thaank you