I have designed a dropdownlist for the entry of date. Now I have to convert it into text format (like the way you enter into a text field - mm-dd-yyyy) so that I can display it in an xml file.
Any suggestions on how to go about it?
My frontend code looks like this. This is just one part of it. I have 3 more entities like this.:
<td>
LICENSE FROM *
</td>
<td>
<asp:DropDownList ID="drpmm0" runat="server" CssClass="drp1">
</asp:DropDownList>
<asp:DropDownList ID="drpdte0" runat="server" CssClass="drp1">
</asp:DropDownList>
<asp:DropDownList ID="drpyyyy0" runat="server" CssClass="drp1">
</asp:DropDownList>
(mm-dd-yyyy)
<asp:RequiredFieldValidator ID="reqmm0" runat="server" ControlToValidate="drpmm0" Display="Dynamic"ErrorMessage="Select Month"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="reqdte0" runat="server" ControlToValidate="drpdte0" ErrorMessage="Select Date"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="reqyyyy0" runat="server" ControlToValidate="drpyyyy0" ErrorMessage="Select Year"></asp:RequiredFieldValidator>
</td>
</tr>
I just need a little guidance for backend.
string strdt= drpdte0.SelectedValue+"/"+drpmm0.SelectedValue+"/"+drpyyyy0.SelectedValue;
string formatedDate = strdt.ToString("dd-MM-yyyy");
To get into string:
string myDate = String.Format("{0}-{1}-{2}", drmm0.SelectedValue, drpdte0.SelectedValue, drpyyy0);
To get into DateTime:
DateTime myDate = new DateTime(Convert.ToInt32(drpyyyy0.SelectedValue)), Convert.ToInt32(drpmm0.SelectedValue),Convert.ToInt32(drpdte0.SelectedValue)));
Format DateTime:
string myDate = String.Format("{0:MM-dd-yyyy}", myDate);
Why don't you use DateTimePicker?
Related
I have the following code, how can I make the cursor to autojump to the next textfield in this case the dd when the user enters a two digit in the month textbox?
<p>
<h5>Date of Birth <font color="red">* </font>(mm/dd/yyyy)</h5>
<asp:TextBox ID="mm" runat="server" MaxLength="2"></asp:TextBox>
<asp:TextBox ID="dd" runat="server" MaxLength="2"></asp:TextBox>
<asp:TextBox ID="yyyy" runat="server" MaxLength="4"></asp:TextBox>
</p>
You need to use jQuery. Something like:
$(document).ready(function(){
function ChangeFocus()
{
if($("#txtboxDay").val().length==2)
{
$("#txtboxmonth").focus();
}
}
$("#txtboxDay").bind("change",ChangeFocus);
});
I have a ASP.NET 4.5 web forms site using EF 5.
One of the object of my data model is a 'medical file' with a DateTime property (among the others):
public partial class MedFile {
public System.Guid MedFileId { get; set; }
public string Name { get; set; }
public Nullable<System.DateTime> MedDate { get; set; }
...
}
Here it is the snippet of the web form for editing:
<asp:ListView ID="MedFilesForm" runat="server"
ItemType="MedFile" DataKeyNames="MedFileId"
SelectMethod="GetFiles"
UpdateMethod="UpdateFile">
...
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" AssociatedControlID="TextBox1">Name</asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>' />
<asp:Label ID="Label2" runat="server" AssociatedControlID="TextBox2">Data</asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("MedDate", "{0:d}") %>' />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="true" CommandName="Update"
</asp:LinkButton>
</EditItemTemplate>
</asp:ListView>
Note the UpdateMethod="UpdateFile" and the TextBox2 with the Bind("MedDate", "{0:d}").
Finally here it is the UpdatedFile in the code behind:
public void UpdateFile(Guid medFileId) {
// get the current med file from DB
MedFile medfile = _cartellaBLL.GetMedFileByFileId(medFileId);
// update it with values taken from the web form
TryUpdateModel(medfile);
// save the updated med file
if (ModelState.IsValid) {
_cartellaBLL.UpdateFile(medfile);
}
...
}
As known, the above TryUpdateModel(medfile) auto-magically validates, parses and converts the fields in the web form updating the passed medfile variable.
In particular, it validates and converts the value of the TextBox2 according to the date format of the application's culture possibly set in the web.config file:
<globalization culture="it-IT" uiCulture="it-IT" />
My problem is that I'd like the user would be able to set the date in the webform using one of two possible formats: the one of the default culture, in my case dd/MM/yyyy (Italy), and the international yyyy-MM-dd (ISO 8601).
Is it possible to instruct TryUpdateModel(TModel) to 'accept' both the format?
I'm having a very hard time figuring out what I'm doing wrong here. In my Edit Item Template I have the following code for my drop down list:
<asp:DropDownList ID="dd_is_active" runat="server" AppendDataBoundItems="true"
DataValueField="Enabled">
<asp:ListItem Text="Yes" Value="1"></asp:ListItem>
<asp:ListItem Text="No" Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:HiddenField ID="is_activeTextBox" runat="server" Value='<%# Bind("Enabled") %>' />
Here is my aspx.cs code:
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
e.Values["SUB_last_modified_date"] = DateTime.Now.ToString();
e.Values["SUB_last_modified_by_user_id"] = HttpContext.Current.User.Identity.Name;
e.Values["SUB_last_modified_by_user_name"] = Session["UserName"].ToString();
e.Values["Enabled"] = ((DropDownList)(sender as ListView).InsertItem.FindControl("dd_is_active")).SelectedValue;
e.Values["Category_ID"] = ((DropDownList)(sender as ListView).InsertItem.FindControl("dd_category")).SelectedValue;
}
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
e.NewValues["SUB_last_modified_date"] = DateTime.Now.ToString();
e.NewValues["SUB_last_modified_by_user_id"] = HttpContext.Current.User.Identity.Name;
e.NewValues["SUB_last_modified_by_user_name"] = Session["UserName"].ToString();
}
It seems something is either missing from my .cs code or I have the values of 1 and 0 bound incorrectly in the html code. This exact same code works for the Insert Item Template, but the Update (or Edit Item Template) is not working correctly.
When I try to edit an item in my table I get an error stating the input string is in an incorrect format. I know it's trying to bind the Text of "Yes" or "No" but I need to ind to the Values of either "0" or "1". Any help is greatly appreciated!
I think your syntax is wrong for HiddenField value.
Instead of this
<asp:HiddenField ID="is_activeTextBox" runat="server" Value='<%# Bind("Enabled") '>' />
It should be
<asp:HiddenField ID="is_activeTextBox" runat="server" Value='<%# Bind("Enabled")%>' />
I am getting the following error, Using asp.net and nHibernate.
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
I have this listview
<asp:ListView ID="CurrentHourList" runat="server" ItemPlaceholderID="itemPlaceholder">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<asp:ImageButton ID="DeleteDateButton" CausesValidation="false" runat="server" CommandName="Delete"
ImageUrl="img/delete.png" />
<span style="display: none">
<asp:Label ID="Id" runat="server" Text='<%# Eval("Id") %>'></asp:Label></span>
<asp:Label ID="Date" Text='<%# Eval("Date", "{0:dd MMM yyyy}") %>' runat="server"></asp:Label>
<asp:DropDownList ID="MedicalType" runat="server" SelectedValue='<%# Eval("MedicalType.Id") %>'>
<asp:ListItem Text="Paid" Value="1"></asp:ListItem>
<asp:ListItem Text="Unpaid" Value="2"></asp:ListItem>
<asp:ListItem Text="Special" Value="3"></asp:ListItem>
</asp:DropDownList>
Half-Day:<asp:CheckBox ID="HalfDay" runat="server" Checked='<%# Eval("IsHalfDay") %>' />
<br />
</ItemTemplate>
</asp:ListView>
and this in my code behind
private void BindData(string id)
{
MedLeaveHelper = new MedicalLeaveHelper();
DTO.MedLeaveDTO dto = MedLeaveHelper.GetMedicalRequest(id);
if (dto != null)
{
EnableForm();
this.RequestId.Text = dto.Id.ToString();
this.ddlEmployeeName.SelectedItem.Text = dto.User;
this.Note.Text = dto.Note;
this.CurrentHourList.DataSource = MedLeaveHelper.MakeMedicalDays(id);
this.CurrentHourList.DataBind();
}
MakeMedicalDays(id) simply looks like this. MedicalDays is a IList
internal IEnumerable MakeMedicalDays(string id)
{
int theId = 0;
if (int.TryParse(id, out theId))
{
MedicalRequest r = MedicalRequest.Get(theId);
return r.MedicalDays;
}
return null;
}
When I get to the DataBind() call the error shows up. I've poked around on the tubes but nothing concrete is jumping out at me. I've tried changing the syntax to something like this
DataBinder.Eval(Container.DataItem,"id")
and the error goes away but no data makes it into my ListView either.
as I understand it DataBinder.Eval uses reflection to determine my datatype, but I am not sure how to troubleshoot this issue. Any help you could provide would be great.
Thanks
Jim
Not sure if the problem is in your listview. I tried a simple repro using the following databinding code - which worked fine with the ASPX you included above:
this.CurrentHourList.DataSource = new[] { new { Id = 1, Date = DateTime.Now, MedicalType = new { Id = 1 }, IsHalfDay = false }, new { Id = 1, Date = DateTime.Now, MedicalType = new { Id = 1 }, IsHalfDay = false } };
this.CurrentHourList.DataBind();
Is it possible that the binding error is coming from a different control (ie. not inside of the listview?)
If you need to use declarative databinding on a control or property which doesn't support it, you can also look into building your own custom expression builder.
I am using a Masked Editor which is great and simple to use, but I was wondering. Is there a way to bind the time to a textbox that has a masked editor and cause the AM or PM to show up?
I know if you type an A or P AM and PM will show up, but how to you get it to appear to a bound textbox of time?
<asp:TextBox ID="txttime" runat="server" Width="90"></asp:TextBox>
<ajaxToolkit:MaskedEditExtender ID = "MaskedEditExtender1" AcceptAMPM="true" ClearTextOnInvalid="true" ClearMaskOnLostFocus="false" runat="server" TargetControlID="txttime"
Mask="99:99" MaskType="Time"></ajaxToolkit:MaskedEditExtender>
<ajaxToolkit:MaskedEditValidator ID = "MEV" ControlToValidate="txttime" runat="server" ControlExtender="MaskedEditExtender1" IsValidEmpty="false"></ajaxToolkit:MaskedEditValidator>
Here is the code that binds to the textbox. All i see is the time without AM or PM
DateTime datetime = Convert.ToDateTime(DataBinder.Eval(FormView1.DataItem, "Date"));
txttime.Text = String.Format("{0:t}", datetime);
Change
MaskType="Number"
To
MaskType="DateTime"
And include the following parameter:
AcceptAMPM="true"
So it would now be:
<asp:TextBox ID="txttime" runat="server" Width="90"></asp:TextBox>
<ajaxToolkit:MaskedEditExtender ID = "MaskedEditExtender1" AcceptAMPM="true" ClearTextOnInvalid="true" ClearMaskOnLostFocus="false" runat="server" TargetControlID="txttime"
Mask="99:99" MaskType="DateTime" AcceptAMPM="true"></ajaxToolkit:MaskedEditExtender>
<ajaxToolkit:MaskedEditValidator ID = "MEV" ControlToValidate="txttime" runat="server" ControlExtender="MaskedEditExtender1" IsValidEmpty="false"></ajaxToolkit:MaskedEditValidator>
ClearMaskOnLostFocus must be set to true. That was the issue. Thanks for the help.
ClearMaskOnLostFocus="true"
Here's where i found the answer
Click here