Calendar Controls, Event Handler for Load Event - asp.net

I have been working on this for quite a while now. My code will show all the different ways and things that I have tried to get the problem fixed, but with no luck...so far. What I need to do is:
Start an event handler for the Load event of the form. Then add code to display the current date in the format shown above if the form is not being posted back.
Add a calendar control to the next paragraph, and set its Visible property to False so it's hidden when the form is first displayed.
Code an event handler for the Click event of the image button. This shold hide the image button and display the calendar control.
Code an event handler for the SelectionChanged event of the calendar control. This should get the selected date and display it in the text box with today's date and should also hide the calendar control and display the image button.
I hope that someone can help me sort out what I am doing wrong and help me get to the correct solution. Not really happy with the outcome so far.
My code-behind:
Partial Class Request
Inherits Page
Dim ImageButton1 As ImageButton1
Protected Sub Calendar_SelectionChanged(object sender, EventArgs e)
Label.Text = "Current date: " + System.DateTime.Now.ToLongDateString()
Label.Text = "Selected date: " + Calendar.SelectedDate.ToLongDateString()
Dim label1 = System.DateTime.Now
Dim label2 = SelectedDate
Dim ImageButton1 = ImageButton1
End Sub
protected void Calendar_SelectionChanged(object sender, EventArgs e)
{
lblday.Text = Calendar1.TodaysDate.ToShortDateString();
lblbday.Text = Calendar1.SelectedDate.ToShortDateString();
}
' Display using current (en-us) culture's short date format
Dim ddlDate As Date = #3/15/2008#
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Calendar.SelectedDate = DateTime.Now;
Label.Text = "Today's date and time is :" + Calendar.SelectedDate;
Calendar.SelectedDate = DateTime.Today
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.clnArrival = thisDate.ToString
End If
End Sub
Sub Submit(s As Object, e As EventArgs)
TextBox1.Text = "The date and time is " & Now()
End Sub
Protected Sub ddlDay_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlDay.SelectedIndexChanged
clnArrival.Visible = False
Dim day As String = ddlDay.SelectedItem.Text(ddlDay.SelectedValue)
End Sub
Protected Sub ddlMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlMonth.SelectedIndexChanged
Dim month As String = ddlMonth.SelectedItem.Text(ddlMonth.SelectedValue)
End Sub
Protected Sub clnArrival_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles clnArrival.SelectionChanged
ddlMonth_SelectedValue = clnArrival.SelectedDate.Month.ToString
ddlDay_SelectedValue = clnArrival.SelectedDate.Day.ToString
clnArrival.Visible = True
End Sub
End Class
My markup:
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Request.aspx.vb" Inherits="Request" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Chapter 6: Reservations</title>
<link href="Styles/Main.css" rel="stylesheet" type="text/css" />
<link href="Styles/Request.css" rel="stylesheet" type="text/css" />
<script language="VB" runat="server">
Sub Page_Load()
Response.Write("Today is: ")
End Sub
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="page">
<h1>Royal Inn and Suites</h1>
<h2>Where you’re always treated like royalty</h2>
<p id="arrival_date">
Arrival date:
<asp:Calendar ID = "Calendar" runat = "server" SelectionMode="DayWeekMonth" OnSelectionChanged="Calendar_SelectionChanged" SelectedDate="1/1/0001" VisibleDate="1/1/0001">
<asp:ImageButton ID="ImageButton1" runat="server" AlternateText="Click to show calendar" ImageUrl="C:\aspnet4_vb\Jeanne Tatro Webs 424 - HW 6 - Ch06Reservation\Images\Calendar.bmp" />
</asp:Calendar>
</p>
<p class="clear">
Number of nights:
</p>
<p>
Number of adults:
Children:
</p>
<h3>Preferences</h3>
<p>
Room type:
</p>
<p>
Bed type:
</p>
<p id="requests">Special requests:</p>
<h3 class="clear">Contact information</h3>
<p class="contact">Name:</p>
<p class="contact">Email:</p>
<p id="buttons"></p>
<p id="message"></p>
</div>
</form>
</body>
</html>

Related

In vb asp.net I'm unable to obtain the selected TreeView node

The problem I'm having is: when a node is clicked, the selected node index is always -1 and the selected node text is always the text of the first node.
As you can see in the code below, I've created a test-app. This test-app shows the same problem
This is my Default.aspx:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="Treeviewer._Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="tvDir" OnSelectedNodeChanged="tvDir_SelectedNodeChanged" OnTreeNodeExpanded="tvDir_TreeNodeExpanded" runat="server"></asp:TreeView>
</div>
</form>
</body>
</html>
And this is the code-behind:
Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
tvDir.Nodes.Add(New TreeNode("C:\"))
For Each aDir As String In IO.Directory.EnumerateDirectories("C:\")
Dim aNode As TreeNode = New TreeNode(aDir, 0)
If hasSubDirectories(aDir) Then
aNode.ChildNodes.Add(New TreeNode("", 1))
End If
aNode.SelectAction = TreeNodeSelectAction.Select
tvDir.Nodes(0).ChildNodes.Add(aNode)
Next
tvDir.CollapseAll()
End Sub
Protected Sub tvDir_SelectedNodeChanged(sender As Object, e As EventArgs)
Dim tvIndex = tvDir.Nodes.IndexOf(tvDir.SelectedNode)
Debug.Write(tvIndex & " " & tvDir.SelectedNode.Text)
End Sub
Protected Sub tvDir_TreeNodeExpanded(sender As Object, e As TreeNodeEventArgs)
End Sub
Private Function hasSubDirectories(inDirectory As String) As Boolean
Try
If IO.Directory.Exists(inDirectory) Then
Return IO.Directory.EnumerateDirectories(inDirectory).Count > 0
End If
Catch ex As Exception
'
End Try
Return False
End Function
End Class

ASPX Webform Linkbutton in user control does not trigger click event

I've read tons of posts with the same kind of issue, I studied a lot of solutions but I still can't solve my problem.
I have a master page and a default.aspx nesting a user control.
default.aspx :
<%# Register TagPrefix="uc" TagName="MenuSideUserControl" Src="~/Controls/MenuSide.ascx" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="row">
</div>
<div class="row">
<div id="sideMenu">
<uc:MenuSideUserControl runat="server" ID="wkfMenuSide">
</uc:MenuSideUserControl>
</div>
<div id="remoteContent">
</div>
</div>
</asp:Content>
default.aspx.vb :
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
AddHandler wkfMenuSide.UserControlLinkClick, AddressOf MenuSide_UserControlLinkClick
End Sub
Protected Sub MenuSide_UserControlLinkClick(sender As Object, e As EventArgs)
Dim parentId = CType(sender, LinkButton).CommandArgument
_menuSide.BuildSideMenu(_dao.GetMenuItems(_userID, parentId))
End Sub
MenuSide.ascx :
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="MenuSide.ascx.vb" Inherits="IntranetWKF.MenuSide" %>
<div class="col-sm-2" style="padding-left: 0;">
<div runat="server" class="panel-group" id="accordion" ClientIDMode="Static">
</div>
</div>
MenuSide.ascx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
BuildSideMenu(_dao.GetMenuItems(0, 3))
End Sub
Public Sub BuildSideMenu(sideMenuItems As IList(Of WkfMenuItem))
Dim pnlDefault As HtmlGenericControl
Dim pnlHeading As HtmlGenericControl
Dim pnlTitle As HtmlGenericControl
Dim linklist As HtmlGenericControl
Dim link As LinkButton
Dim lastRegroupement = String.Empty
For Each m As WkfMenuItem In sideMenuItems
pnlDefault = New HtmlGenericControl
With pnlDefault
.TagName = "div"
.Attributes("class") = "panel panel-default"
End With
[...]
link = New LinkButton
With link
.Text = m.libelle
End With
If Not String.IsNullOrEmpty(m.url) Then : link.PostBackUrl = m.url
Else
With link
.CommandArgument = m.id
.CommandName = "parentId"
.ClientIDMode = ClientIDMode.Static
.Attributes("data-parentid") = m.id
End With
AddHandler link.Click, AddressOf LinkButtonClicked
End If
pnlTitle.Controls.Add(link)
pnlHeading.Controls.Add(pnlTitle)
pnlDefault.Controls.Add(pnlHeading)
accordion.Controls.Add(pnlDefault)
End If
Next
End Sub
Public Event UserControlLinkClick As EventHandler
Protected Sub LinkButtonClicked(sender As Object, e As EventArgs)
RaiseEvent UserControlLinkClick(Me, e)
End Sub
I use an event and a delegate and bind the delegate to the LinkButton.
In the default.aspx Page_Load (I tried in the OnInit event too) I bind a click handling method that is never triggered, this is where I turn around without a solution.
Those LinkButton has to rebuild the menu they're nesting in on click.
The goal at end is to update the menus and content ajax likely.

Dynamically Add Text Files to DDL in ASP & VB

I am looking to update one of my DDL's functionality by making it dynamically update so if the user adds more files, the drop down will pick this up.
At present my drop down list is pulling from VB code behind, as shown below:
Public Sub DDL_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList) 'item is already dropdownlist
Dim ctl As TextBox = DirectCast(ddl.NamingContainer.FindControl("eTemplate"), TextBox)
If ddl.SelectedValue = 1 Then
ctl.Text = File.ReadAllText("e:Documents\Visual Studio 2013\Projects\Web\Templates\Down.txt")
ElseIf ddl.SelectedValue = 2 Then
ctl.Text = File.ReadAllText("e:Documents\Visual Studio 2013\Projects\Web\Templates\Up.txt")
Else
ctl.Text = ""
End If
End Sub
At the moment I have hard coded in the functionality for the VB to grab specific .txt files, how can I get this to update dynamically from a folder of .txt files?
Thanks for looking.
Here is some sample code for you. This demo uses an UpdatePanel and a Timer to refresh the DropdownList every 5 seconds.
Add a new aspx file to your Web Application and the following code:
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Demo.aspx.vb" Inherits="Zpk_Test2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Asynchronous Update Demo</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager1" />
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" /><br />
<asp:Timer runat="server" ID="Timer1" Interval="5000" Enabled="true" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="DropDownList1" />
</Triggers>
</asp:UpdatePanel>
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Width="300" Height="250" />
</form>
</body>
</html>
This is the code-behind:
Partial Class Demo
Inherits System.Web.UI.Page
Private Const FolderName As String = "C:\Temp" '<-- replace with your folder name
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
RefreshDropDownList()
OpenSelectedFile()
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
' this event is fired everytime a timer ticks.
' refresh your dropdown list here.
RefreshDropDownList()
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
OpenSelectedFile()
End Sub
Private Sub RefreshDropDownList()
Dim currentSelected As String = DropDownList1.SelectedValue
DropDownList1.DataSource = IO.Directory.GetFiles(FolderName, "*.txt").Select(Function(f) IO.Path.GetFileName(f)).ToList
DropDownList1.DataBind()
DropDownList1.SelectedValue = currentSelected
End Sub
Private Sub OpenSelectedFile()
Dim fileName As String = IO.Path.Combine(FolderName, DropDownList1.SelectedValue)
TextBox1.Text = IO.File.ReadAllText(fileName)
End Sub
End Class

Data not being changed in TextBox

Ok, I have been going around and around with this for hours (Searching, etc). This is what I want to do, I want to load data into a Text Box and if the user changes the text in the Text Box I want to be able to save the new text.
My problem in the TxtBox_TextChanged event the data contained in the txtNarrative Text box is the new data that the user typed in (<>ABCD) but in the btnSubmit_Click event the data contained in txtNarrative is the original value ABCD.
What am I doing wrong??
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WorkBench_VBNet._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset>
<span class="title">Entry Form</span>
<ul class="pageitem">
<li class="Narrative">
<asp:TextBox EnableViewState=true ID="txtNarrative" placeholder="Narrative" Width="100%"
Rows="10" TextMode="multiline" runat="server" Height = "100%" OnTextChanged="TxtBox_TextChanged" >
</asp:TextBox></li>
<li class="Submit">
<asp:LinkButton ID="btnSubmit" runat="server">Submit</asp:LinkButton>
</li>
</ul>
</fieldset>
</div>
</form>
</body>
</html>
Code Behind:
Public Class _Default
Inherits System.Web.UI.Page
Public Event TextChanged As EventHandler
Protected Sub TxtBox_TextChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles txtNarrative.TextChanged
ViewState("txtNarrative") = txtNarrative.Text ''<-- The text here is the changed text not ABCD
txtNarrative.Text = ViewState("txtNarrative").ToString
End Sub
Private Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
Dim Narrative as String = txtNarrative.Text '<-- the text in the text box is still ABCD not what was changed.
''Code to update data in the Database goes here
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
txtNarrative.Text = "ABCD"
End If
End Sub
End Class
Get rid of the TxtBox_TextChanged Sub, it's not needed, the IPostBackDataHandler of the TextBox will do that for you:
<asp:TextBox EnableViewState=true ID="txtNarrative" placeholder="Narrative" Width="100%"
Rows="10" TextMode="multiline" runat="server" Height = "100%" >
</asp:TextBox>
I'm not sure if I understand your question but:
You do not need to "OnTextChanged" textbox in your markup.
In your code behind you alredy have handles declaration.
If you use both ("OnTextChanged" on your html markup, and handles in your code behind) the text box event will trigger twice.
Protected Sub TxtBox_TextChanged (ByVal sender As Object, _
ByVal e As System.EventArgs) Handles txtNarrative.TextChanged
ViewState ("txtNarrative") = txtNarrative.Text''<-- The text here text is not changed the ABCD
txtNarrative.Text = ViewState ("txtNarrative"). ToString
end Sub
<asp: TextBox EnableViewState = true ID = "txtNarrative" placeholder = "Narrative" Width = "100%"
Rows = "10" TextMode = "multiline" runat = "server" Height = "100%">
</ asp: TextBox>
also tested your code and the value that is submitted is the new value in the textbox. (Ie if I understand your question)
Hope this helps

Declarative event handling from ASP.NET user control to page

I am trying to figure out how to declaratively pass in a event handler into
a user control, but I am stumped. All I can make work is the user control's
event handler.. I can't seem to bubble up the caught event into the parent
page. Ideas would be quite welcome. Here is my code:
Default.aspx:
<%# Page Language="VB" %>
<%# Register TagPrefix="rpt" TagName="filter" Src="WebUserControl.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test Controls</title>
</head>
<body>
<form id="form1" runat="server">
<rpt:filter ID="DataView1Filters" runat="server" SelectedIndexChanged="DropDown_SelectedIndexChanged" />
<asp:Label ID="Label1" runat="server" />
</form>
<script runat="server">
Public Sub DropDown_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Label1.Text = String.Format("Inside declarative event handler. {0}<br>", Label1.Text)
End Sub
</script>
</body>
</html>
WebUserControl.ascx:
<%# Control Language="VB" ClassName="WebUserControlTest" %>
<asp:Panel ID="TestPanel" runat="server"></asp:Panel>
<script runat="server">
Private AllEvents As New System.ComponentModel.EventHandlerList
Public Custom Event SelectedIndexChanged As EventHandler
AddHandler(ByVal value As EventHandler)
AllEvents.AddHandler("SelectedIndexChanged", value)
End AddHandler
RemoveHandler(ByVal value As EventHandler)
AllEvents.RemoveHandler("SelectedIndexChanged", value)
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
Dim value As EventHandler = CType(AllEvents("SelectedIndexChanged"), EventHandler)
If Not value Is Nothing Then
value.Invoke(sender, e)
End If
End RaiseEvent
End Event
Private Sub _SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ctrl As DropDownList = Me.FindControl("TestDropDownList")
If Not ctrl Is Nothing Then
Me.ViewState("ItemSelection") = ctrl.SelectedIndex
End If
Dim Label1 As Label = Parent.FindControl("Label1")
Label1.Text = String.Format("Inside user control event handler. {0}<br>", Label1.Text)
RaiseEvent SelectedIndexChanged(sender, e)
End Sub
Private Overloads Sub OnLoad(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim ctrl As New DropDownList
With ctrl
.ID = "TestDropDownList"
.Items.Clear()
.AutoPostBack = True
AddHandler .SelectedIndexChanged, AddressOf _SelectedIndexChanged
.Items.Add(New ListItem("-- Select --", String.Empty))
.Items.Add(New ListItem("Item 1", "1"))
.Items.Add(New ListItem("Item 2", "2"))
If Not Me.ViewState("ItemSelection") Is Nothing Then
.SelectedIndex = CInt(Me.ViewState("ItemSelection"))
Else
.SelectedIndex = 0
End If
End With
TestPanel.Controls.Add(ctrl)
End Sub
</script>
Thanks!
See this previous post:
Handling User Control Events on Containing Page
Edit - added based on your comment
I should have read the question more clearly.
As far as having a UserControl raise an event that the containing page can respond to, I do not believe that this can be done declaratively.
Unless my knowledge is just lacking, the only way to accomplish this is by explicitly creating an event in the control and then handling it (by coding the event handler) on the parent page, as shown in the example I linked to.
I was recently having this same issue in C#. When you set up an event called SelectedIndexChanged asp.net will bind the attribute OnSelectedIndexChanged when using the declarative syntax.
So if you change
<rpt:filter ID="DataView1Filters" runat="server" SelectedIndexChanged="DropDown_SelectedIndexChanged" />
To
<rpt:filter ID="DataView1Filters" runat="server" OnSelectedIndexChanged="DropDown_SelectedIndexChanged" />
It should work.

Resources