how to get date from ajax calender in asp.net - asp.net

The following is my .aspx code for ajax calender
<ajax:CalendarExtender ID="CalendarExtender1" TargetControlID="TextBox1" runat="server">
</ajax:CalendarExtender>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td><td>
and aspx.cs code is
string date = Request.Form[TextBox1.UniqueID];
comp.DATETIME = Convert.ToDateTime(date);
string value could not converted to datetime. how to convert this into datetime

<td>
Date Received From
</td>
<td >
<asp:TextBox ID="TxtSearchDate_Received_From" runat="server"></asp:TextBox>
<a runat="server" id="HrefDateReceivedFrom"></a>
<asp:CalendarExtender Format="dd-MM-yyyy" ID="CalendarExtender1" runat="server" TargetControlID="TxtSearchDate_Received_From"
PopupButtonID="HrefDateReceivedFrom" Enabled="True" />
</td>
Then use txtsearchDate_received_from control on the server side. This is a good practice as the use can view selected date in text box control.
Let me know if it works for you

Why haven't you tried just getting the .Text property value from the server control, like this?
string date = this.TextBox1.Text;
Then you can do your conversion to DateTime, like this:
comp.DATETIME = Convert.ToDateTime(date);

Related

How to limit characters in "year" when asp textbox is set to textmode "date"?

I set ASP textbox text mode to date, and noticed its "YEAR" component allows input of up to 9 characters.
My question is, how can limit the year input field to accept a maximum of 4 characters?
<asp:TextBox ID="bday" runat="server"
TextMode="Date"
CssClass="textbox"
style="width: 30%; " >
</asp:TextBox>
Use the MaxLength property.
MaxLength="4"
Here is the full code:
<asp:TextBox ID="bday" runat="server"
MaxLength="4"
TextMode="Date"
CssClass="textbox"
style="width: 30%; " >
</asp:TextBox>
Take the date as a string and use the substring fonction..
for exemple string s1=01 jan 2017
String s2= s1.substring (7) will give you only 2017
I guess thats the easiest way
If you want to prevent the user entering the wrong values ,you should use Date Picker..
If you want a Server-Side validation :
aspx:
<asp:CustomValidator runat="server" ControlToValidate="bday" ErrorMessage="Not valid format" OnServerValidate="CustomValidator1_ServerValidate" />
C# :
protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)
{
DateTime d;
e.IsValid = DateTime.TryParseExact(e.Value, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out d);
}
Or you may need a Client-Side validation :
<asp:RegularExpressionValidator runat="server" ControlToValidate="bday" ValidationExpression="(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$"
ErrorMessage="Invalid date format." ValidationGroup="Group1" />
To limit the number of year characters, you need to set the maximum value (Max="9999-12-31").
Note that this format is fixed - do not confuse it with the date format string. This is a maximum value.
Full working example:
<asp:TextBox
TextMode="Date"
ID="txtDate"
runat="server"
Max="9999-12-31"
CssClass="textbox"
style="width: 30%;" >
</asp:TextBox>

Update SSRS report parameters using custom ASP.NET controls

I have a ASP.NET web app, which uses SSRS 2008 R2 on another machine in Native mode. The app has a page for generation reports created in SSRS.
I use my own custom controls for report parameters. After all the parameters are set, the page generates the report using a hidden ReportViewer control.
Here is the page layout:
<asp:Repeater ID="rptParameters" runat="server">
<ItemTemplate>
<tr>
<td class="label">
<asp:Literal ID="label" runat="server"/>:
</td>
<td class="control">
<asp:TextBox ID="tbx" runat="server" Visible="false"/>
<my:tsJCalendar ID="calendar" runat="server" GeneratePostback="false" HasValidation="true" ErrorDisplay="None" CompareMessage="Date must be in valid 'mm/dd/yyyy' format!" Visible="false"/>
<asp:RadioButtonList ID="radioList" runat="server" Visible="false"
RepeatDirection="Horizontal" RepeatLayout="Flow">
<asp:ListItem Text="True" Value="1"></asp:ListItem>
<asp:ListItem Text="False" Value="0"></asp:ListItem>
</asp:RadioButtonList>
<asp:DropDownList ID="ddl" runat="server" Visible="false"></asp:DropDownList>
<asp:HiddenField ID="hdnForDdl" runat="server"/>
</td>
<td width="22px"></td>
</ItemTemplate>
<AlternatingItemTemplate>
<td class="label">
<asp:Literal ID="label" runat="server"/>:
</td>
<td class="control">
<asp:TextBox ID="tbx" runat="server" Visible="false"/>
<my:tsJCalendar ID="calendar" runat="server" GeneratePostback="false" HasValidation="True" ErrorDisplay="None" CompareMessage="Date must be in valid 'mm/dd/yyyy' format!" Visible="false"/>
<asp:RadioButtonList ID="radioList" runat="server" Visible="false">
<asp:ListItem Text="True" Value="1"></asp:ListItem>
<asp:ListItem Text="False" Value="0"></asp:ListItem>
</asp:RadioButtonList>
<asp:DropDownList ID="ddl" runat="server" Visible="false"></asp:DropDownList>
<asp:HiddenField ID="hdnForDdl" runat="server"/>
</td>
</tr>
</AlternatingItemTemplate>
</asp:Repeater>
<rsweb:ReportViewer ID="rvReports" runat="server" Width="100%"
ShowReportBody="False" ShowPromptAreaButton="false" ShowWaitControlCancelLink="false"
AsyncRendering="false" Visible="false">
</rsweb:ReportViewer>
<hr />
<asp:Button ID="btnRun" runat="server" Text="Run Report" />
To generate custom parameter controls I use the repeater and show an appropriate control according by the report parameter type.
I am working with SSRS using its ReportingService2005 webservice. E.g. on the first page loading, I am getting report parameters using this code:
Private Function GetParameterReports(values As ParameterValue()) As ReportParameter()
Dim rs As New ReportingService2005()
rs.Credentials = New MyReportCredentials().NetworkCredentials
Dim forRendering As Boolean = True
Dim historyID As String = Nothing
Dim credentials As DataSourceCredentials() = Nothing
Dim paramInfos As ReportParameter() = rs.GetReportParameters(ReportPath, historyID, forRendering, values, credentials)
Return paramInfos
End Function
The 'values' parameter is Nothing for the first time. However, if a parameter depends on other parameters, I do a postback, and pass report values to the report to get updated parameters, using the same GetParameterReports() method.
All the reports have a special field, 'user_id', which is the identifier of the logged in user. I must set this parameter manually in the code, and I am trying to do this by using 2 ways:
Dim Values As New List(Of ParameterValue)()
Dim singleUserParameter As New ParameterValue()
With singleUserParameter
.Name = "user_id"
.Value = Security.CurrentUserPeopleID
End With
Values.Add(singleUserParameter)
paramInfos = GetParameterReports(Values.ToArray)
and
Dim userIdParameter As New ReportParameter()
userIdParameter.DefaultValues = New String() {Security.CurrentUserPeopleID.ToString()}
userIdParameter.PromptUser = False
Dim rs As New ReportingService2005()
rs.Credentials = New MyReportCredentials().NetworkCredentials
rs.SetReportParameters(ReportPath, paramInfos)
paramInfos = GetParameterReports(Nothing)
Some of the report parameters depend on the 'user_id', but, unfortunately, after calling ReportingService2005.GetReportParameters() with the passed 'user_id' value, dependent parameters still don't have DefaultValues.
Could you help to understand whether I am doing something wrong while setting the report parameters values, or the reason is in how the SSRS report was created?

How do I format a date pulled from a database?

I am trying to pull a date from database and putting it on a webpage with the below code:
<asp:Label ID="Label3" runat="server" Text='<%# Eval("TravelDate") %>' /><br /><br />
It pulls the date with no problem, but when it shows up on the page, no matter how it is formatted in the database, it seems to want to always display the date as "6/17/2013 12:00:00am". Is there something I'm missing in the VS portion that I have to use to format the date? I'd prefer "June 17, 2013", but the only option close to that in Access is where it adds the day of the week in front of it. Getting rid of the time is important.
<asp:Label ID="txtDate" Width="65px" runat="server" Font-Size="8.5pt" ForeColor="#000f9f"
Text='<%# Eval("How_date","{0:dd/MMM/yyyy}") %>'></asp:Label>
Try to extract like the field like this:
Expr1:Format([Field Name],"DD/MM/YYYY")
or
If it's a text field then you can use the string function left() or right() to get the date. Expr1:Left([Field Name],10)
Just remove the single quotation mark '' after the property text of the TextBox.
<asp:Label ID="Label3" runat="server" Text=<%# Eval("TravelDate", "{0:MMMM dd, yyyy}") %> /><br /><br />

ajax control toolkit calendar startdate=datetime.now FromDate To Date Validation

how to validate FromDate and ToDat?
i'm using two textbox id-'FromDate', 'ToDate'
here is the client side code
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="FromDate">
</asp:CalendarExtender>
<asp:TextBox ID="FromDate" runat="server" width="158px"></asp:Textbox>
<asp:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="FromDate">
</asp:CalendarExtender>
<asp:TextBox ID="ToDate" runat="server" width="158px"></asp:Textbox>
here is the backend code
CalendarExtender1.startdate=datetime.now
CalendarExtender2.startdate=datetime.now
i'd lik to know, if i select fromdate(5/1/2012), the selection for 'Todate' should be equal or greater then fromdate,,,
it shouldn't lesserthen 'fromdate' lesserdays should be disable
you can work with a combination of the client and server side validation.
below alidation will work at client side to get a valid date from user.
<ajaxtoolkit:MaskedEditValidator ID="MaskedEditValidator3" runat="server" ControlExtender="MaskedEditExtender3" ControlToValidate="FromDate" EmptyValueMessage="Prosim vpišite Datum začetka." InvalidValueMessage="Please enter valid date." Display="None" TooltipMessage="Prosim vpišite Datum začetka." EmptyValueBlurredText="Empty" ValidationGroup="Offers" />
And then you can have a date validation method at server side to comapare from and to date. as below
int result = DateTime.Compare(Convert.ToDateTime(FromDate.Text), Convert.ToDateTime(ToDate.Text));
if (result < 0 || result == 0)
{
// user input passed
}
else
{
// error msg: "'To date' should be greater then 'From date'";
}

How to cast listview object

I'm wondering how to cast a listview object to a datetime. Is there a way to do this on front end markup page?
Here's my code below from part of the listview functionality. This code below doesn't work properly.
<asp:ListView ID="listviewAttachments" runat="server">
<ItemTemplate>
<tr class="announcementPost">
<td class="posted">
<h6>Convert.ToDateTime(Eval("DateModified")).Day<span><asp:Label ID="Label1" runat="server" Text='<%# Convert.ToDateTime(Eval("DateModified")).Month %>' /> </span></h6>
</td>
You are not casting a ListView object to DateTime. What you are doing there is trying to cast one of your properties to DateTime
Try this:
<%=Convert.ToDateTime(Eval("DateModified")).Day %>

Resources