Date and Time picker in ASP.net - asp.net

I know this question is on SO a few times already but I cannot find a working or suitable answer.
I want a date picker which includes a time picker. I don't want any fancy frills etc, just something that works. Preferably one control I can drag into the tool box in VS, register at the top and work with. I have been playing around with many versions the last few days and I can't get one to work. They normally seem to include too many files for me or something stupid. A project I am working on now hinges on this stupid thing so any advice would be welcome. I have previously managed to get a calendar control and put some validation on a text-box to pick times. However I cannot take both of these values and enter them into my database as on Date-time field because when I try to use "Selected-date" to get the time it naturally says that text-boxes cannot be members of the "Selected-date" property.
As I have a calendar control in to pick the date and a validated textbox to type in the time I tried to use the following code.
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Dim time As String() = txtTime.SelectedDate.ToString().Trim().Split(" "C)
Dim [date] As String() = cldDate.SelectedDate.ToString().Trim().Split(" "C)
Dim datetime__1 As String = [date](0) & " " & time(1) & time(2)
Dim DateTimeValue As DateTime = DateTime.Parse(datetime__1)
Response.Write(DateTimeValue.ToString())
' Update database with DateTimeValue.
End Sub
The code txtTime.SelectedDate.ToString() is a problem here. It says that SelectedDate is not a supported member of textbox. This is code that I have came across. I'm pretty new to coding.

You could create a new instance of the DateTime structure. The DateTimePicker.SelectedValue returns a Nullable(Of DateTime), so you can use the values of this to create a new DateTime (along with the values from your textboxes).
For example:
Dim myDateTime As New DateTime(cldDate.SelectedDate.Year, cldDate.SelectedDate.Month, cldDate.SelectedDate.Day, HourValue, MinuteValue, 0)
Just replace HourValue and MinuteValue with your values from your textboxes. Note you should convert these to Integers.
Note the 0 at the end represents seconds.
This should save to the database fine.
For more info on DateTime see here.

if jQuery is an option i would look at this plug in
clean and easy to implement

You don't mention whether a commercial product would be an option for you.
We are using telerik's RadDateTimePicker in our projects (and there are certainly other commercial products). See this page for a demo of the datetime picker control.

Related

populate text boxes based on drop down box selection

Not done any programming for about 5 years so im a little rusty on this one.
I am building an Asset management system to make my job easier but am struggling on a few things.
What i have so far is the DB setup and populated with a small amount of data, The main site itself is built and the basic select statements to a gridview of any data currently in the system for that particular page/search and i also have an insert statement that is populating the relevant tables based on data filed in on a form on that page. All these functions are working seamlessly.
The problem i have is that one field that needs to be populated for the insert statement needs to populate based upon the selection that is being made from a drop down box in the same form.
i.e.
An Asset that is being registered into the system has an Asset Type (the type details are contained within a parent table Asset_Type) the form has 3 fields for display purposes (one of these fields will need to return an entry to the Asset table in the database from the form, this is working) that are taken from the Asset_Type table, the other fields only relate to the Asset table itself.
What i am trying to achieve is that a user goes to the page and sees a list of Assets registered (this is working) they then need to add a new asset by filling in a form lower down on the page (for is there and writes to DB) on this form is a drop down menu that queries the Asset_Type table and allows the user to select the Type by name (this works)
What i now need to get working is for the 2 other fields to populate based on what Asset type is selected.
These fields are currently textboxes in the form but can be changed if required.
The code i have behind the page is below:
Protected Sub Name_Model_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Name_Model.SelectedIndexChanged
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("AssetManagementConnectionString").ToString())
Dim cmd As New SqlCommand()
cmd.Connection = conn
cmd.CommandText = "SELECT [ID], [Name_Model], [Description_Spec] FROM [Asset_Type] WHERE ([ID] = #ID)"
cmd.CommandType = CommandType.Text
conn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
If reader.HasRows Then
While (reader.Read())
{
Type_ID.text = (reader["ID"].tostring())
Name_Model.text = (reader["Name_Model"].tostring())
Description_Spec.text = reader["Description_Spec"].tostring
}
End While
End If
End Using
End Sub
So I'm not sure about Steve's comment, but I know he is a lot more familiar with VB based on questions of my own he has answered. However I am going to suggest something.
So "Drop downs" typically have 2 pieces of data associated with them.
A DisplayMember and a ValueMember (also a SelectedValue if it's a ComboBoxColumn)
You can reference either, I believe.
Then, you can do what you please with that value using an If Then or however you plan on using it.
assetValue = YourComboBox.DisplayMember.ToString
YourTextBox.Text = assetValue
or
If ComboBox.ValueMember = YourIndex Then
assetValue = Whatever
ElseIf
....
End If
Give that a try and let me know what you get. I am just throwing ideas out there.

Memo Username and Date stamp

I am new to Access and programing codes. I am building a data base where daily notes for issues can be kept. I want the end user to be able to view past records and make updates. However I do not want the to alter text previously put in the memo field containing the issue they are wanting to update. Also I am wanting to flag there new input with username and date/time.
I am try to accomplish this by inserting the following code into the AfterUpdate()
Private Sub Text0_AfterUpdate()
Me.Text0 = Me.Text0.Value & " " & UserName() & " # " & Now()
End Sub
I used Text0 because I am testing this code. Anyway this works in an unbound text box perfectly for flagging the new input. However when I try using this in my form with bound textboxes it fails to work. Any ideas? If I get this working I want to find a way to prevent editing on previously entered text but that is not a must.
The form containing the memo textbox for update was corrupt. By copying and pasting to create a new form with everything but the textbox I was having problems with I fixed my problem. I just made a new text box in the new form.

PagedDataSource not working with previous/next pages

I'm having a hard time with this scenario and hoping someone can help me clarify where I'm missing a step/logic. I tried searching online but I couldn't find an example that addressed this issue.
We are setting up a search page that when first loads shows a bunch of options (e.g., textboxes, checkboxes, etc). The user will fill out the form and submit the form back to itself. Once posted back, the page will build and run a SQL query (e.g., SELECT ID FROM Customers WHERE Company = 'Acme' AND AmtDue = 3) against the database with the user's options and then the results will show up. This part works ok.
The part that breaks down is when I am trying to add pagination. The result set is a DataTable bound to a Repeater. I am using a PagedDataSource to add pagination. Pagination works great for the first page but for subsequent pages, it does not work. Basically, instead of returning the next page of the result requested (e.g., SELECT ID FROM Customers WHERE Company = 'Acme' AND AmtDue = 3), the results returned is the SQL query before appending the user's search options (e.g., SELECT ID FROM Customers).
I think my basic problem is that I'm not sure how to distinguish a normal Page.IsPostBack and paginating through results. The reason why this is causing problems is because I don't want to be regathering form data, rebuilding query, and requerying the DB.
The examples that I found online involved pagination of a DataTable that is rebuilt every time the page loads (e.g., Not Page.IsPostBack).
Here is a rough outline of our code:
Public dtCustomers As DataTable = New DataTable()
Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Handles Me.Load
' When the page first loads, show the search form with search options.
If Not Page.IsPostBack Then
ShowSearchForm()
' Once the form is submitted, show the search results.
Else
' ----------------------------------------
' This is the part that I'm having trouble with. How do I skip the following two steps (GatherFormData() and BuildDT()) when the user requests a subsequent page?
GatherFormData()
dtCustomers = BuildDT()
' ----------------------------------------
BindData()
End If
End Sub
Sub BindData()
Dim objPDS As PagedDataSource = New PagedDataSource()
objPDS.DataSource = dtCustomers.DefaultView
objPDS.AllowPaging = True
objPDS.PageSize = intNumPerPage
objPDS.CurrentPageIndex = Me.ViewState("_CurrentPage")
rptSearchResults.DataSource = objPDS
End Sub
' Subroutine called when the previous page button is pressed.
Sub GoToPrevPage()
' Set viewstate variable to the previous page.
Me.ViewState("_CurrentPage") -= 1
BindData()
End Sub
' Subroutine called when the next page button is pressed.
Sub GoToNextPage()
' Set viewstate variable to the next page.
Me.ViewState("_CurrentPage") += 1
BindData()
End Sub
Note: I understand that the DataTable will have to be put into cache or a session variable, but haven't decided the best method.
Please excuse the code outline but the actual code is massive so the simplification is to make it easier to get to the heart of the issue.
Let me know if any of that was unclear. Thanks in advance!
I am assuming that you are going to store your data in session/cache (I would prefer the later but there can be use case for storing it in session) - you can store the key in the view-state and presence of key could be use to determine if post-back is for pagination or not. For example,
if (ViewState["dataKey"] == null)
{
// first form submittal, do the search
GatherFormData();
dtCustomers = BuildDT();
// store it in cache
ViewState["dataKey"] = Guid.NewGuid().ToString(); // create a key
Cache.[ViewState["dataKey"].ToString()] = dtCustomers; // TODO use Add method to control expiration etc
}
Its important that you clear the view-state when the search is reset (or similar condition)
Lastly, it is also possible to do the paging from database/data-store (for example, using ranking functions in SQL Server) so that you need not have to store the search results into the session/cache but rather do a database trip at each post-back. Such approach is useful when the complete result set size can be huge.

how do I reference the values from textboxes on form submit that reside in my usercontrol? ASP.NET(VB)

I can't believe I couldn't find an answer to this question after literally hours of searching the web. Is this so basic that everyone just knows how to do this? As you might have guessed I'm new to asp.net(vb).
My situation:
I have a large form that reuses several elements, so I decided to create a usercontrol with some common fields. The problem is when the form is filled out by a user and submitted, how do I reference those values so I can input them to my database???
Example:
Using Conn As New SqlConnection(connect)
Using Cmd As New SqlCommand(SQL, Conn)
Cmd.Parameters.AddWithValue("#Acct_Company", txtPartner.Text)
Cmd.Parameters.AddWithValue("#Acct_AccountNum", txtPartnerAccount.Text)
So, above two Cmd lines are for normally inserted textboxes in my form, but what would the line look like to reference any usercontrol form fields?
Any help would be greatly appreciated.
You need to expose the controls some how to the page that contains the user control. One way to to this is like this (within the control code):
Property CompanyName As String
Get
Return txtPartner.Text
End Get
Set(value As String)
txtPartner.Text = value
End Set
End Property
Then, the page containing the user control can access the value like this:
myControl.CompanyName

How to create an asp.net calendar link to database?

We are trying to implement a calendar in an asp.net page and link it a database. The calendar should highlights dates based on data in the database tables similar to airline ticket booking calendar.
Thank you
No one is going to write an application for you here... If you have a specific question you should ask it. Without that here is my best advice.
Start by looking at how to implement asp.net's calendar control:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.calendar%28v=vs.80%29.aspx
Then look at using ADO.NET to communicate from your web app to your db. Here is a good place to start:
http://msdn.microsoft.com/en-us/library/e80y5yhx.aspx
A calendar has a property SelectedDates which is a list of the dates that have been selected. All you need to do if you get a list of dates from a database is add them to the property SelectedDates.
For example:
Public Sub Calendar_Prerender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar.PreRender
Dim tomorrow As DateTime = Now.AddDays(1)
Dim nextday As DateTime = Now.AddDays(2)
Calendar.SelectedDates.Add(tomorrow)
Calendar.SelectedDates.Add(nextday)
End Sub
The calendar loads with tomorrow and the day after selected.

Resources