Binding time to a textbox - c#

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

Related

Ajax Calendar Extender, client side event shows previous day

I am trying to store the selected date of an Ajax Calendar Extender control in a hidden field. I used OnClientDateSelectionChanged to set it.
However, when I select 02/22/2015, the selected date in the script shows as 02/21/2015. I added setting the textbox's value using this selected value, to test it, and it shows as 02/21/2015.
It also sets the value of the hidden field as "Feb 21 19:00:00 EST 2015" which throws an error when trying to convert to datetime.
<asp:TextBox runat="server" ID="tbStartDate" ReadOnly="true" />
<asp:Image runat="server" ID="imgFrom" ImageUrl="Calendar.gif" />
<ajaxToolkit:CalendarExtender runat="server"
ID="calStartDate"
Format="MM/dd/yyyy"
TargetControlID="tbStartDate"
OnClientDateSelectionChanged= "startDateChanged"
PopupButtonID="imgFrom" />
function startDateChanged(sender, args) {
var hfStartDate = document.getElementById('<%=hfStartDate.ClientID %>');
var selectedDate = sender._selectedDate;
hfStartDate.value = selectedDate;
sender._textbox.set_Value(sender._selectedDate.format(sender._format))
}

Populate current date and time as default in footer template

How do I populate the RadDateTimePicker with a default of Now below:
<FooterTemplate>
<telerik:RadDateTimePicker Width="150px" ID="txt_QualityInspectorDateTimeFooter" runat="server" />
</FooterTemplate>
Check this link :
DateTimePicker
txt_QualityInspectorDateTimeFooter.radDateTimePicker1.Value = DateTime.Now;
I have found correct solution:
GridViewRow gridStoppageFooterRow = gvw_InspectionMainProcess.FooterRow;
RadDateTimePicker txt_LineInchargeDateTime = gridStoppageFooterRow.FindControl("txt_LineInchargeDateTimeFooter") as RadDateTimePicker;
txt_LineInchargeDateTime.SelectedDate = DateTime.Now;

Stop DropDownList SelectedIndexChanged Event firing on FormView command

I have a dropdown list inside an editItemTemplate. The Dropdownlist onSelectedIndexChanged event fires on change like I want. However it also will fire when I submit the form.
********UPDATE ************
Per Saechel comment below, I started to investigate further. Here it describes it can be done. http://blog.programmingsolution.net/net-windows-application/selectionchangecommitted-and-selectedindexchanged-events-system-nullreferenceexception-while-closing-windows-form/
BUT, I tried it and it didn't even fire the event.
I checked http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist(v=vs.110).aspx and they don't list the event.
My goal is to let the user enter the values as a % of the Total or actual as actual tons and then toggle back and forth. The end value that will be inserted into the DB will be in tons.
I want to either:
a) not fire the "onSelectedIndexChanged" event
b) Some how determine if I am on the same index and have an if statement to skip everything
c) A better way of going about this that I don't know of.
I tried verifying via the ViewState the Dropdown's current index and skip the code if it was the same but couldn't find the value. Maybe help me figure out how to get that value? Searched around and could not find how do do it.
I tried: `var vs = ViewState["ddl_units"].ToString(); and some other variations as I needed to find the control via FindControl
.aspx:
<asp:Panel ID="pnlRecycledMaterialsReceivedForm" runat="server" Visible="false">
<asp:FormView ID="fvAddRecycledMaterialsReceived" runat="server" SkinID="annualReportFormview" EnableViewState="false"
HeaderText="Selected Recycled Materials Received Detail" DataKeyNames="RecycleDetailId" DefaultMode="Insert"
DataSourceID="odsRecycledMaterialsReceivedDetail" OnDataBound="fvAddRecycledMaterialsReceived_DataBound"
OnItemCommand="fvAddRecycledMaterialsReceived_ItemCommand" OnItemInserted="fvAddRecycledMaterialsReceived_ItemInserted"
OnItemUpdated="fvAddRecycledMaterialsReceived_ItemUpdated" OnItemDeleted="fvAddRecycledMaterialsReceived_ItemDeleted">
<EditItemTemplate>
<asp:TextBox ID="tbxRecycledTotalWasteQuantity" runat="server" Text='<%# Bind("TotalWasteQuantity") %>' Width="64px"></asp:TextBox>
<asp:TextBox ID="tbxRecycledWasteCommercialQuantity" runat="server" Text='<%# Bind("CommercialQuantity") %>' Width="64px"></asp:TextBox>
<asp:TextBox ID="tbxRecycledWasteResidentialQuantity" runat="server" Text='<%# Bind("ResidentialQuantity") %>' Width="64px"></asp:TextBox>
<asp:DropDownList ID="ddl_Units" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_Units_SelectedIndexChanged">
<asp:ListItem Value="" Text="" Enabled="false" />
<asp:ListItem Text="Tons" Value="1" />
<asp:ListItem Text="Percent" Value="9" />
</asp:DropDownList>
<asp:LinkButton ID="lbtnWasteReceivedUpdate" runat="server" Text="Update" CommandName="Update"
ValidationGroup="RecycledWasteReceivedDetail" Font-Bold="True" />
<asp:LinkButton ID="lbtnWasteReceivedInsertCancel" runat="server" Text="Cancel" CausesValidation="False" CommandName="Cancel" />
</EditItemTemplate>
</asp:FormView>
</asp:Panel>
.cs:
protected void ddl_Units_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox tbxRecycledTotalWasteQuantity = (TextBox)fvAddRecycledMaterialsReceived.FindControl("tbxRecycledTotalWasteQuantity");
TextBox tbxRecycledWasteResidentialQuantity = (TextBox)fvAddRecycledMaterialsReceived.FindControl("tbxRecycledWasteResidentialQuantity");
var d_TotalWasteQuantity = Convert.ToDecimal(tbxRecycledTotalWasteQuantity.Text);
ResidentialQuantity = Convert.ToDecimal(tbxRecycledWasteResidentialQuantity.Text);
DropDownList ddl_units = (DropDownList)fvAddRecycledMaterialsReceived.FindControl("ddl_units");
if (ddl_units.SelectedIndex.ToString() == "2")
{
//2 = percent
//Take tb value and convert to percent
//300/700 * 100
tbxRecycledWasteResidentialQuantity.Text = ((ResidentialQuantity / d_TotalWasteQuantity) * 100).ToString();
//ResidentialQuantity = ResidentialQuantity * (d_TotalWasteQuantity / 100);
}
else
{
//Else 'tons' was chosen. Convert value(%) to tons.
//700 * (43/100) = 301
ResidentialQuantity = d_TotalWasteQuantity * (ResidentialQuantity / 100);
tbxRecycledWasteResidentialQuantity.Text = ResidentialQuantity.ToString();
//tbxRecycledWasteResidentialQuantity.Text = "Tons";
}
}
Try using the SelectionChangeCommitted Event.
This will not trigger on form load but triggers on selectionchange

Convert dropdownlist into text

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?

I want the textbox AJAX Mask Edit Extender to change based on a radio button selection

I creating an ASP.net online application with C# background. I am also using AJAX MaskEditExtender. I'm pretty new to AJAX and don't know Javascript. What I need to do is have the textbox AJAX mask change based on the selection of the radio buttons.
In this example they are choosing salary or hourly. I need the salary to be "999,999" and the hourly to be "99.99".
<asp:TextBox ID="finalwage" runat="server" Width="80px">$</asp:TextBox>
<!-- Salary Mask -->
<asp:MaskedEditExtender
ID="MaskedEditExtender1"
runat="server"
TargetControlID="finalwage"
Mask="999,999"
MessageValidatorTip="true"
MaskType="Number"
InputDirection="RightToLeft"
AcceptNegative="None"
ErrorTooltipEnabled="true">
</asp:MaskedEditExtender>
<asp:MaskedEditValidator
ID="MaskedEditValidator1"
runat="server"
ControlExtender="MaskedEditExtender1"
IsValidEmpty="true"
MinimumValue="0"
MaximumValueMessage="Must enter a number"
ControlToValidate="finalwage" >
</asp:MaskedEditValidator>
<!-- Hourly Mask -->
<asp:MaskedEditExtender
ID="MaskedEditExtender2"
runat="server"
TargetControlID="finalwage"
Mask="99.99"
MessageValidatorTip="true"
MaskType="Number"
InputDirection="RightToLeft"
AcceptNegative="None"
ErrorTooltipEnabled="true">
</asp:MaskedEditExtender>
<asp:MaskedEditValidator
ID="MaskedEditValidator2"
runat="server"
ControlExtender="MaskedEditExtender1"
IsValidEmpty="true"
MinimumValue="0"
MaximumValueMessage="Must enter a number"
ControlToValidate="finalwage" >
</asp:MaskedEditValidator>
.......
<asp:RadioButtonList
ID="RadioButtonList1"
runat="server"
AutoPostBack="true"
RepeatDirection="Horizontal"
OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem Text="Hourly" Value="Hourly"
<asp:ListItem Text="Salary" Value="Salary" />
</asp:RadioButtonList>
Heres the C# code that I thought would work:
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadioButtonList1.SelectedValue = "Hourly")
{
MaskedEditExtender1.Mask = "99.99";
}
if (RadioButtonList1.SelectedValue == "Salary")
{
MaskedEditExtender1.Mask = "999,999";
}
}
Try to move the RadioButtonList1_SelectedIndexChanged code to Page_Init. I believe RadioButtonList1_SelectedIndexChanged is too late in the lifecycle process to change the mask.

Categories