Change the property visible in a DropDownList through webform - asp.net

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

Related

VB ASP.NET 4.0 - Adding Example to new web project

I'm trying to learn ASP.NET (after many years of using classic ASP, jQuery, Ajax etc). I've installed VS 2010 and have IIS running on my W7 64bit PC.
I've created a new web project (in a folder called C:\ASP.NET Testing\ASP.NET 4.0 Examples) called ASP.NET-4.0.
I can compile the project fine and run the default page etc.
I am trying to load in a simple example from a book (ASP.NET 4.0 In Practice). So, I've created a sub folder CH01, and then copied in the .aspx and .aspx.vb files.
When I debug this, I get
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: Could not load type 'ASP.NET_4._0.Global_asax'.
Source Error:
Line 1: <%# Application Codebehind="Global.asax.vb" Inherits="ASP.NET_4._0.Global_asax" Language="vb" %>
in the browser window.
The sample code (which has been downloaded from the website) is :
<%# Page Language="VB" AutoEventWireup="false" CodeFile="1-4.aspx.vb" Inherits="_1_4" %>
<html>
<head>
<title>Listing 1.4</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<asp:literal id="ResponseText" runat="server" />
<br />
Enter your name:
<asp:textbox runat="server" ID="Name" />
<br />
<asp:button runat="server" Text="Click Me" ID="ClickButton" OnClick="HandleSubmit" />
</div>
</form>
</body>
</html>
and
Partial Class _1_4
Inherits System.Web.UI.Page
Sub HandleSubmit(ByVal sender As Object, ByVal e As EventArgs)
ResponseText.Text = "Your name is: " & Name.Text
End Sub
End Class
In VS, I also get an error message highlighting the ResponseText.Text = "Your name is: " & Name.Text
'ResponseText' is not declared. It may be inaccessible due to its protection level.
Global.asxa file
Imports System.Web.SessionState
Public Class Global_asax
Inherits System.Web.HttpApplication
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session is started
End Sub
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request
End Sub
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires upon attempting to authenticate the use
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Fires when an error occurs
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session ends
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application ends
End Sub
End Class
I'm obviously missing something really simple, but I don't get it. I can't see any instructions as to anything which I need to set within VS.
Thanks.
This page works OK if I add from VS. In effect the same page as I'm copying in from the examples, but with slight changes to the page tag. It alos has a .designer.vb page which was automatically generated by VS.
14.aspx
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="14.aspx.vb" Inherits="ASP.NET_4._0._14" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Listing 1.4</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<asp:literal id="ResponseText" runat="server" />
<br />
Enter your name:
<asp:textbox runat="server" ID="Name" />
<br />
<asp:button runat="server" Text="Click Me" ID="ClickButton" OnClick="HandleSubmit" />
</div>
</form>
</body>
</html>
14.aspx.vb
Public Class _14
Inherits System.Web.UI.Page
Sub HandleSubmit(ByVal sender As Object, ByVal e As EventArgs)
ResponseText.Text = "Your name is: " & Name.Text
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
14.aspx.designer.vb
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
Option Strict On
Option Explicit On
Partial Public Class _14
'''<summary>
'''Form1 control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents Form1 As Global.System.Web.UI.HtmlControls.HtmlForm
'''<summary>
'''ResponseText control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents ResponseText As Global.System.Web.UI.WebControls.Literal
'''<summary>
'''Name control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents Name As Global.System.Web.UI.WebControls.TextBox
'''<summary>
'''ClickButton control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents ClickButton As Global.System.Web.UI.WebControls.Button
End Class
Check you global.asax on the root of your website. There seems to be something wrong with the class name/namespace. I don't think it has anything to do with the file you added, but rather with something you probably moved or removed by mistake.

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

asp.net custom buttons

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

Formatting text in an ASP.Net Boundfield

I have a webform with a a couple of boundfields in an edit window as follows:
<asp:BoundField DataField="TS_TITLE" HeaderText="Title" SortExpression="TS_TITLE" HeaderStyle-VerticalAlign="Top" HtmlEncode="True" >
<ControlStyle Width="500px" />
</asp:BoundField>
<custom:BoundTextBoxField DataField="TS_DESCRIPTION" HeaderText="Desription" HeaderStyle-VerticalAlign="Top" SortExpression="TS_DESCRIPTION"
TextMode="MultiLine" Rows="20" Columns="100" Wrap="True" HtmlEncode="True" />
I'm using the Html Encode property of the BoundField to secure against cross-site scripting attacks. What I would like to do is when a user reopens the edit window, I want the encoded html to be decoded and presented, html tags and all. My problem is that when I try to decode the html in the code-behind, under the Page_Load function, it doesn't get set when the page is presented to the user, i.e. it has no effect. Here is the snippet of code from the Page_Load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dvrTest As DetailsViewRowCollection = DetailsView1.Rows
Dim DescriptionTB As TextBox = dvrTest.Item(1).Cells(1).Controls(0)
DescriptionTB.Text = HttpUtility.HtmlDecode(DescriptionTB.Text)
End Sub 'Page_Load
When debugging I can see the html decoded text as it should look, my guess is that there's an additional databind that occurs after the Page_Load exits that resets the BoundTextBoxField. Just a note, I've tested this on both the BoundField and BoundTextBoxField and the effect is the same on both.
I had a similar issue with a dropdown list I'm using in another part of my application, only there I was using the onLoad event to call a function to do data manipulation after the page was loaded and databound. Unfortunately, Boundfield doesn't seem to have that event, the closest thing I've found is DataFormatString property, but that only seems to be useful when working with dates and currency.
UPDATE:
In case anyone was wondering, even with the HTMLEncode property set to false, I get the encoded text when the Edit Window is Reloaded.
UPDATE 2:
Tried overriding the OnDataBinding method, but that didn't seem to do anything.
Protected Overrides Sub OnDataBinding(ByVal e As System.EventArgs)
Me.OnDataBinding(e)
Dim dvrTest As DetailsViewRowCollection = DetailsView1.Rows
Dim DescriptionTB As TextBox = dvrTest.Item(1).Cells(1).Controls(0)
DescriptionTB.Text = HttpUtility.HtmlDecode(DescriptionTB.Text)
End Sub
Got it. Since my boundfields were encased in a DetailsView, I used the onLoad event of the DetailsView to call a function in the code-behind to decode the any html within the text of the Boundfields
''' <summary>
''' Decodes any HTML formatted tags in the Title and Description Textboxes of the Edit Window
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Protected Sub HTMLDecode(ByVal sender As Object, ByVal e As System.EventArgs)
If Page.IsPostBack = False Then
''Grab the Title and Description text boxes from the RowCollection
Dim dvrTest As DetailsViewRowCollection = DetailsView1.Rows
Dim TitleTB As TextBox = dvrTest.Item(0).Cells(1).Controls(0)
Dim DescriptionTB As TextBox = dvrTest.Item(1).Cells(1).Controls(0)
''Decode HTML tags that are in either text box
DescriptionTB.Text = HttpUtility.HtmlDecode(DescriptionTB.Text)
TitleTB.Text = HttpUtility.HtmlDecode(TitleTB.Text)
End If
End Sub 'HTMLDecode
And calling it in the DetailsView using the onLoad event
<asp:DetailsView ID="DetailsView1" runat="server" Height="260px" Width="500px" AutoGenerateRows="False"
DataKeyNames="TS_ID" DataSourceID="SqlDataSource2" EnableModelValidation="true"
GridLines="Both" Font-Names="Arial" HorizontalAlign="Center" OnLoad="HTMLDecode" >
If there are any more straightforward alternatives, I'd be glad to hear them.

How do you expose a Panel as a property in a User Control?

I have a user control with a panel on it:
CustomControl.ascx
<%# Control Language="vb" CodeBehind="CustomControl.ascx.vb" %>
<asp:Panel ID="myPanel" runat="server"></asp:Panel>
I'd lke to expose that panel as a property that I can use at design time.
CustomControl.ascx.vb
<ParseChildren(True), PersistChildren(False)> _
Partial Public Class CustomControl
Inherits System.Web.UI.UserControl
#Region "Members and Properties"
<PersistenceMode(PersistenceMode.InnerProperty)> _
Public Property SomePanel() As Panel
Get
Return myPanel
End Get
Set(ByVal value As Panel)
myPanel= value
End Set
End Property
#End Region
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Write(myPanel.HasControls.ToString())
End Sub
End Class
And then bring the control onto a page like so:
<ucl:CustomControl runat="server">
<SomePanel>
<asp:Label ID="lblText" AssociatedControlID="txtBox" runat="server"></asp:Label>
<asp:TextBox id="txtBox" runat="server"></asp:TextBox>
</SomePanel>
</ucl:CustomControl>
When I run the page, HasControls is true but nothing is rendered. What am I doing wrong?
You could achieve a similar effect by designing your user control as a template control. It's not that hard to set up, if you choose to go the template route.
Check out this brief tutorial to determine if control templates are suitable for you:
http://msdn.microsoft.com/en-us/library/36574bf6.aspx

Resources