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;
Related
I develop one web form by using ASP.net, I want to select the row and display that row data into web form but the date cannot display in the textbox of type DateTime, someone helps me to sort out this issue
ASP.Net Code
<div class="col-4">
<asp:Label ID="Label7" runat="server" Text="Birth Date"></asp:Label>
<asp:TextBox ID="txtBirthDate" runat="server" TextMode="Date"></asp:TextBox>
</br>
<asp:Button ID="btnSave" Text="Save" runat="server" OnClick="btnSave_Click" />
</div>
<div class="col-8">
<asp:GridView ID="studentGrid" runat="server"
DataKeyNames="Id"
OnSelectedIndexChanged="StudentGrid_SelectedIndexChanged">
<Columns>
<asp:CommandField ButtonType="Button" ShowSelectButton="True" />
</Columns>
</asp:GridView>
</div>
c# Code
protected void StudentGrid_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
selId = Convert.ToInt16(studentGrid.SelectedDataKey[0]);
if (selId > 0)
{
using (var ctx = new dbTestEntities())
{
var oldObj = ctx.tblStudents.Find(selId);
if (oldObj != null)
{
txtBirthDate.Text = oldObj.BirthDate.ToString();
}
}
}
}
catch (Exception)
{
throw;
}
}
The data keys is always referenced by name.
So, from the grid it is
GridView1.DataKeys[5]["ID"]
(Get the 5th row datakey value of "ID")
In your case, you have the selected row, so you need this:
selId = Convert.ToInt16(studentGrid.SelectedDataKey["ID"]);
Date format of the textbox with TextMode="Date" is "yyyy/MM/dd" but it stores date with default time like 2021/25/09 12:00:00 AM so the date is fetched from database but not able to set to the textbox, So we need to set the proper format of date like
txtBirthDate.Text = String.Format("{0:yyyy/MM/dd}", oldObj.BirthDate);
I am pulling data from an access database to show in a GridView control on a ASP.NET project. It works fine but I want to see if I can format the data that is being pulled. Currently any currency is being truncated from xx.xx to just the dollar amounts. Also the dates are displaying mm/dd/yyyy hh/mm/ss AM/PM
I tried editing the database itself to the right values (I set the currency field to "Currency" and the date field to "Short Date" but when I pull that date it still shows them not formatted.
EDIT: Sorry, had to take the code down
Any ideas?
Thank you
in the grid view of yours add the property called DataFormatString
DataFormatString examples:
{0:dd MMMM yyyy} - gives 24 February 2006
{0:MMM dd} - gives Feb 24 (substitue MMM with MMMM for the full month name
instead of abbreviation)
{0:dd/MM/yy} - gives 24/02/06
{0:dd/MM/yyyy} - gives 24/02/2006
Sample Code
<asp:BoundField HeaderText="Date"
DataField="SampleDate"
DataFormatString="{0:MM/dd/yyyy}" >
MSDN BoundField.DataFormatString Property
You just need to set the dataformatstring with how you want it to be populated.
As exemplified on the MSDN page:
Money:
<asp:BoundColumn HeaderText="Price" DataField="Price"
DataFormatString="{0:c}" />
With the {0:c}, placing a number after the c value (such as {0:c2}) will give you that many decimal places.
Date:
<asp:boundfield datafield="MyDate" dataformatstring="{0:MM/dd/yyyy}" />
One solution would be:
<asp:GridView ID="gvLEmployees" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%# Eval("Name") %>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<%# Convert.ToDateTime(Eval("JoinDate")).ToShortDateString() %>
OR
<%# Convert.ToDateTime(Eval("JoinDate")).ToString("d") %>
</ItemTemplate>
</asp:TemplateField>
</Column>
</Gridview>
You have to Create a BoundField, set its properties including the property dataformatstring which you can use to set format of the field appear in the GridView, and add it to the GridView control.
here below some code
public GridView Cr_GridView(string[] ColNames,string[] ColLable, string[] ColFormat)
{
GridView GV = new GridView();
int x = ColNames.GetUpperBound(0);
for (int i = 0; i < x; i++)
{
BoundField GvField = new BoundField();
GvField.DataField = ColNames[i];
GvField.HeaderText = ColLable[i];
GvField.DataFormatString = ColFormat[i];// for example "{0:dd/MM/yyyy}" for a date field
GV.Columns.Add(GvField);
}
return GV;
}
You can also check if you are using Template Field:
<asp:TemplateField HeaderText="last_modified_date">
<ItemTemplate>
<asp:Label ID="lblModyDate" runat="server" Font-Size="10px" CssClass="ControlStyleUpperCase"
Text='<%# Bind("last_modified_date","{0:dd/MM/yyyy HH:mm:tt}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
I have a repeater and and and I set its DataSourceID .
<asp:Repeater ID="rpProducts" runat="server" DataSourceID="ItemDataSource" >
<HeaderTemplate>....</HeaderTemplate>
<ItemTemplate>
....
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
But I some time I want to change the DataSourceID and load data and again set the ItemDataSource.
This is my code for change the rpProducts DataSourceID .
rpProducts.DataSourceID = null;
rpProducts.DataSource = goods;
rpProducts.DataBind();
So then again I want to set the DataSourceID to ItemDataSource.
How can I do it?
I try like this. but is not working
rpProducts.DataSourceID = null;
rpProducts.DataSourceID = ItemDataSource.ToString();
Can you try with: ItemDataSource.ID
Also, both datasource and datasourceid cannot be set at same time, so you will have to set the datasource to null.
rpProducts.DataSource = null;
rpProducts.DataSourceID = ItemDataSource.ID;
rpProducts.DataBind();
I would like to add controls like textBox in GridView from code on the fly.
In my project i have one Grid in that i can't decide how many rows and columns are there. so that i just give it DataSource. This working fine.
GridView G = new GridView();
G.DataSourse = dt;
G.DataBind();
now i want to do such thing that in Gridview all the controls are Textbox control so that i can write in that textbox.
TextBox t= new TextBox();
G.Contorls.Add(t);
This will throw exception...
do anyone have any idea about this???
Thanks in advance..
Regards
Amit Vyas
Why not do it in design time with ItemTemplate
<asp:GridView ID="GrdDynamic" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox runat="server" ID="Name" Text='<%#Eval("Name") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
EDIT
Here is an interesting CodeProject post on dynamically adding template columns
Please check this http://www.devasp.net/net/articles/display/708.html link and use below code:
DropDownList ddl = new DropDownList();
ddl.Visible = true;
ddl.ID = "ddl1";
ddl.Items.Add("Item1");
TableCell cell = new TableCell();
gv.Rows[0].Cells.Add(cell);
gv.Rows[0].Cells[0].Controls.Add(ddl);
If you are looking a way to add TextBox dynamically in the existing GridView, then using RowDataBound event of the GridView would be best solution.
Add a PlaceHolder control in the ItemTemplate field.
<asp:GridView ID="GrdDynamic" runat="server" OnRowDataBound="GridView_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:PlaceHolder runat='server' ID="PlaceHolder1"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
public void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
//find placeholder control
PlaceHolder placeHolder = e.Row.FindControl("PlaceHolder1") as PlaceHolder;
TextBox TextBox1 = new TextBox();
placeHolder.Controls.Add(TextBox1);
}
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