I am using Datagrid and I have bound column as below
<asp:BoundColumn DataField="Salary" HeaderText="Salary" DataFormatString="{0:#,##0.00}">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
I want to use above dataformat string for below TextBox.
<asp:TextBox DataFormatString="{0:#,##0.00}" ID="textbox1" runat="server" MaxLength="150"></asp:TextBox>
If I try to set Dataformatstring to TextBox it is not working. How can I add dataformatstring to TextBox in ASP.NET or Javascript code in order to to same issue?
If you set the TextBox.Text value in code behind, you can do like this:
textBox1.Text = string.Format("{0:#,##0.00}", yourValue);
Check it on dotnetfiddle
If you set in aspx side, you can with Eval:
<asp:TextBox Text='<%# Eval("myField", "{0:#,##0.00}") %>' ID="textbox1" runat="server" MaxLength="150"></asp:TextBox>
Related
This is how I have it in the .aspx page:
<asp: TemplateField HeaderText="Hide/Show" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="HideShowChk" runat="server" Checked='<%# Bind("Hide_Show") %>' />
</ItemTemplate>
<asp: TemplateField>
This is how the TemplateField is like in Griview. How do I enable sorting on this since the "Enable Sorting" checkbox in Visual Studio doesn't enable it for this column.
Note, I have absolutely no code in the code behind.
Use the SortExpression property within the TemplateField
<asp:TemplateField HeaderText="Hide/Show" ItemStyle-HorizontalAlign="Center" SortExpression="Hide_Show">
<ItemTemplate>
<asp:CheckBox ID="HideShowChk" runat="server" Checked='<%# Bind("Hide_Show") %>' />
</ItemTemplate>
<asp:TemplateField>
in ASP.NET gridview binding two dates. I want to display dd/MM/yyyy but it displays 10/03/2014 00:00:00.
<asp:TemplateField HeaderText ="Fromdate" >
<ItemTemplate >
<asp:Label ID="lblFromDate" runat="server"
DataFormatString="{0:dd/MM/yyyy}"
HtmlEncode="false"
Text='<%# Eval("Fromdate") %>' />
</ItemTemplate>
</asp:TemplateField>
DataFormatString is a property of BoundField control and doesn't affect any other control. You can specify the format in an Eval expression:
Text='<%# Eval("Fromdate", "{0:dd/MM/yyyy}") %>' />
According to this article on MSDN the DataFormatString attribute has a limited number of variants for DateTime data.
What you are looking for is:
DataFormatString="{0:d}"
which is the short date pattern.
In order to get the dd/MM/yyyy format, you need to also set your culture info properly (for example to **en-GB**).
I achieved it with {0:dd/MM/yyyy} on the DataFormatString property of the field
It works well
DataFormatString is a property of BoundField control and doesn't affect any other control. You can specify the format right in the Eval expression:
Text='<%# Eval("Fromdate", "{0:dd/MM/yyyy}") %>' />
<asp:TemplateField HeaderText="Fromdate">
<ItemTemplate>
<asp:Label ID="lblFromdate" runat="server"
Text='<%#Eval("Fromdate", "{0:dd/MM/yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Blockquote
Eval("Date","{0:dd-MM-yyyy}") %>' />
I have gridview output like this .
when I click edit link, I need to pass unitsinstock value should transfer to textbox control. which is present in templatefield.
Here is my c# code:
TextBox tt = (TextBox)GridView1.Rows[i].Cells[3].FindControl("TextBox2").ToString();
TextBox text_ref = (TextBox)GridView1.Rows[e.NewEditIndex].Cells[2].FindControl("TextBox2");
TextBox3.Text = text_ref.Text;
Is there anything wrong? when I debugging e.NewEdtIndex=0, TextBox3=null. How to solve this?
You should use EditItemTemplate for this. For exemple, this code above will bound the unitsinstock value to unitsinstock copy text box when you edit the row.
Change The SqlDataSource as well:
<UpdateParameters>
<asp:Parameter Name="unitsinstock" Type="Int32" /> /*Put the correcly type here*/
/*other fields*/
</UpdateParameters>
And the GridView:
<asp:TemplateField HeaderText="unitsinstock">
<ItemTemplate>
<asp:label id="labelUnitsinstock" runat="server" text='<%#Eval("unitsinstockActual")%>'>
</asp:label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="unitsinstock copy">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("unitsinstock") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
Take a look here for more information Using TemplateFields in the GridView Control
I have a gridview to which I dynamically bind data to.
It features SELECT and EDIT commands.
When it is in EDIT mode all the fields turn into textboxes that can be edited.
What I would like to do is add an Ajax Calendar Extender to one of the textboxes.
How can I create a TemplateField just for the one field?
Is it even possible? I know how to do it if I bound all fields and assign the data via a SqlDataSource.
I have tried adding the templatefield to my gridview but the data is just not showing up:
<asp:GridView ID="gvCheckResults" runat="server" OnRowDataBound="gvCheckResults_RowDataBound"
OnRowEditing="gvCheckResults_RowEditing" OnRowUpdating="gvCheckResults_RowUpdating" OnRowCancelingEdit="gvCheckResults_RowCancelingEdit"
OnRowDeleting="gvCheckResults_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="DateOfTest" SortExpression="DateOfTest">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DateOfTest") %>'></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="test" runat="server"
TargetControlID="TextBox1"
CssClass="calendar"
Format="dd/MM/yyyy">
</ajaxToolkit:CalendarExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("DateOfTest") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
How the data gets bound:
protected void TestDataBind()
{
string Name = txtCheckName.Text;
if (string.IsNullOrEmpty(Name))
Name = null;
gvCheckResults.DataSource = dataContext.GetRecords(Name);
gvCheckResults.DataBind();
}
I want to get the value of a hidden field in a grid view from code-behind, but not to be used in the _RowDataBound or any other similar method. Here is my present code (it is a shopping cart scenario):
<asp:GridView ID="gvShoppingCart"
runat="server"
AutoGenerateColumns="False"
AllowPaging="True"
DataKeyNames="ID"
ShowFooter="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="lblProductID" runat="server" Text='<%# Eval("ProductID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("ProductID", "product_details.aspx?id={0}") %>'
Text='<%# GetProduct(Eval("ProductID")) %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Width="35" CssClass="input" onkeypress="return isNumberKey(event)" AutoPostBack="true" ontextchanged="txtQuantity_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
For the sake of brevity I removed certain fields since they are there only for the display. The Quantity field is there for the user to input a number to add a number of products to his cart. I wish to access the lblProductID label in the _TextChanged event. In this same event, I tried
Label lblProductID = (Label)gvShoppingCart.FindControl("lblProductID");
but it didn't work and returns only a null value. What is the solution?
For each row in your GridView there is a HiddenField for the ProductID.
You can access the HiddenField of a row (in the example below the first row) by using the following code (assuming your HiddenField is in the first cell):
HiddenField hiddenFieldProductID =
(HiddenField)gvShoppingCart.Rows[0].Cells[0].FindControl("lblProductID");
string productID = hiddenFieldProductID.Value
// Do something with the value
Hope, this helps.
Try to replace the HiddenField to a label or a textbox and set the visible attribute to false.
I had tried this before and it works.