I am displaying data(wrapped) in grid but when i am clicking on edit button is is not showing complete data because textbox is very small(data not wrapped). any help please...
Sample Code Line:
<asp:TemplateField HeaderText="Reason(NotDone)" SortExpression="tReason">
<EditItemTemplate>
<asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("tReason") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label17" runat="server" Text='<%# Bind("tReason") %>' ></asp:Label>
</ItemTemplate>
<HeaderStyle BackColor="SeaGreen" />
</asp:TemplateField>
........
On page load i am using the following code:
GridView1.Attributes.Add("style", "word-break:break-all; word-wrap:break-word");
Make the textbox a multiline.
<asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("tReason") %>'
TextMode="MultiLine"></asp:TextBox>
Thanks Akash & Michael for the quick response......i have done like this and it is working in grid .....please suggest if it is correct way ........asp:TemplateField HeaderText="Reason (If not Done)" SortExpression="tReason">
' TextMode="Multiline" Rows="10">
' >
Related
This is my code.I am using a gridview in asp.net.I am trying to add a requiredfieldvalidator to the
textbox_id
in gridview edit mode but nothing happens even if I dont put anything in the textbox and click update.
<Columns>
<asp:BoundField DataField="StudentId" HeaderText="Student_ID" />
<asp:TemplateField HeaderText="ID">
<EditItemTemplate>
<asp:TextBox ID="TextBox_id" runat="server" Wrap="False" CausesValidation="true" ValidationGroup="a"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="a" runat="server" ControlToValidate="TextBox_id" ErrorMessage="RequiredField" ForeColor="Red"></asp:RequiredFieldValidator>
</EditItemTemplate>
<%--<ItemTemplate>
<asp:Label ID="Label3" runat="server"></asp:Label>
</ItemTemplate>--%>
</asp:TemplateField>
The button that triggers the validation should have
ValidationGroup attribute set to "a" as well.
Try this:
<asp:TemplateField HeaderText="Popolazione residente"
SortExpression="InhabitantsNum">
<EditItemTemplate>
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem,"InhabitantsNum") %>
</itemtemplate>
<asp:TextBox ID="InsertPopolazioneResidente" runat="server"
Text='<%# Bind("InhabitantsNum") %>'></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="Inserire un numero positivo" ValidationExpression="^[0-9]+$" ForeColor="Red" ControlToValidate="InsertPopolazioneResidente"></asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="InsertPopolazioneResidente" runat="server"
Text='<%# Bind("InhabitantsNum") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
To summarise, to get validation to work in an Edit Template you need:
A ValidationGroup in the control (Eg TextBox)
The same ValidationGroup in the Validator
The same ValidationGroup in the button etc that triggers the action
CausesValidation="true" in the button etc in step 3
Here is my markup:
<asp:TemplateField HeaderText="Loss" SortExpression="Loss"
HeaderStyle-CssClass="StrongText" HeaderStyle-Font-Bold="true"
HeaderStyle-Font-Size="Medium" HeaderStyle-ForeColor="Blue"
HeaderStyle-Font-Underline="true" HeaderStyle-VerticalAlign="Bottom"
ItemStyle-Width="3%" ItemStyle-HorizontalAlign="Left"
ItemStyle-VerticalAlign="Bottom" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:HyperLink runat="server" ID="HyperLink8"
NavigateUrl='<%#"db_LossofPay.aspx?UserID="+ Eval("User ID")%>'
Text='<%# Eval("Loss","{0}")%>'
ForeColor="BLUE" Target="_blank"></asp:HyperLink>
<asp:Label ID="lblLoss" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Could you please help me with this? In BoundField I wrote DataFormatString="{0:N2}" which works fine, but I got stuck when using in hyperlink (template field)
Try with this:
<asp:HyperLink runat="server" ID="HyperLink8"
NavigateUrl='<%#"db_LossofPay.aspx?UserID="+ Eval("User ID")%>'
Text='<%# Eval("Loss","{0:#,##0.00}") %>' ForeColor="BLUE" Target="_blank" />
Try
Text='<%# String.Format("{0:N2}", DataBinder.Eval(Container.DataItem, "Loss"))%>'
I am editing a field in gridview and would like that the edited value not be allowed to be greater than the old value?
is there a front end validation for this? so as to not use a javascript popup
Thanks
<asp:TemplateField HeaderText="FC Amount">
<ItemTemplate>
<asp:Label ID="FCLabel" runat="server" Text='<%# Eval("FC AMOUNT") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="FCTextBox1" runat="server" Text='<%# Eval("FC AMOUNT") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
Use the CompareValidator control:
Add hidden with old value, and compare its value with new one. Or set ValueToCompare property:
<asp:TemplateField HeaderText="FC Amount">
<ItemTemplate>
<asp:Label ID="FCLabel" runat="server" Text='<%# Eval("FC AMOUNT") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="FCTextBox1" runat="server" Text='<%# Eval("FC AMOUNT") %>'></asp:TextBox>
<asp:CompareValidator
ID="cmpAmount"
runat="server"
ValueToCompare='<%# Eval("FC AMOUNT") %>'
ControlToValidate="FCTextBox1"
Type="Double"
Operator="LessThanEqual" />
</EditItemTemplate>
</asp:TemplateField>
How to: Validate Against a Specific Value for ASP.NET Server Controls
CompareValidator.Operator Property
Try this,
<asp:CompareValidator
ID="cval1"
runat="server"
ValueToCompare='<%#Eval("OldValue") %>'
ControlToValidate="FCTextBox1"
Type="Integer"
Operator="GreaterThanEqual" />
In an aspx page I am binding the labels like this:
<asp:TemplateField HeaderText="Date of Joining">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("date_of_joining") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Paid Priviledge Date">
<ItemTemplate>
<asp:Label ID="Label8" runat="server"
Text='<%# Eval("paid_priviledge_date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
And in the code behind I'm binding the grid view like this :(minimum code is given)
GridView1.DataSource = dt2;
GridView1.DataBind();
But the gridview columns show the date like this :
4/12/2011 12:00:00 AM
4/4/2011 12:00:00 AM
Please suggest how to remove the time stamp part and to display only the date part.
I know how to do this by formatting using ToString and SubString. But I am not able to do this in gridview.
You can specify format strings for the eval statement:
Eval("date_of_joining", "{0:dd/MM/yyyy}")
Create a FormatDate method in your codebehind, and call that from your gridview.
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
http://www.csharp-examples.net/string-format-datetime/
This part will go in your code behind
private object FormatDate(DateTime input)
{
return String.Format("{0:MM/dd/yy}", input);
}
And this bit will go in your markup
<asp:TemplateField HeaderText="Date of Joining">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# FormatDate(Eval("date_of_joining")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Paid Priviledge Date">
<ItemTemplate>
<asp:Label ID="Label8" runat="server"
Text='<%# FormatDate(Eval("paid_priviledge_date")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
This is what I would call a D.R.Y. approach to the problem. If you ever need to modify the formatting in any way. You can simply edit the code behind method and it will rain down sweet love to all of your markup.
Use "{0:d}" for short date format.
Try
Text='<%# Eval("paid_priviledge_date","{0:d}") %>'
and
Text='<%# Eval("date_of_joining", "{0:d}") %>'
You can use the DataFormatString in a bound field the same can be set as below:
<asp:Label ID="Label8" runat="server" Text='<%# Eval("paid_priviledge_date","{0:d}") %>'/>
Text='<%# (Convert.ToDateTime((Eval("date_of_joining")))).ToShortDateString() %>'
This is the simplest way I discovered.
I've got a text box that just disappeared. When I add another templateField anywhere on the page, this one bizarelly disappears. Anyone know what might be up?
<asp:TemplateField HeaderText="Summary" SortExpression="summary">
<ItemTemplate>
<asp:Label ID="lblSummary" runat="server" Text='<%# Bind("summary") %>'></asp:Label>
</ItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txtSummary" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("summary") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<AnotherEdit>
I hope you don't take this wrong, and I mean no offense, but it seems like you're missing some of the basic concepts and need a good place to learn them.
Please check out the following article so that you fully understand how to use TemplateFields as opposed to a normal BoundField or a Command field. I think once you "get" it, your disappearing item issues will clear up because you'll be able to see it on your own.
http://www.asp.net/learn/data-access/tutorial-12-cs.aspx
</AnotherEdit>
You can't have more than one ItemTemplate in a TemplateField. You can have an EditItemTemplate and an ItemTemplate, though...
Edit - Added
The ItemTemplate shows when you're in normal display mode.
The EditItemTemplate shows when you're in edit mode
InsertItemTemplate shows when you're in Insert mode.
For any column in a GridView (or field in a FormView or field in a DetailsView) there can only be one TemplateField.
Within that TemplateField, there can only be one ItemTemplate, one EditItemTemplate, and one InsertItemTemplate (and not all three are required but all three are recommended.)
If you want the TextBox to show up next to the label in the normal non-edit mode, you would put the text box within the existing ItemTemplate as follows:
<ItemTemplate>
<asp:Label ID="lblSummary" runat="server" Text='<%# Bind("summary") %>'>
</asp:Label>
<asp:TextBox ID="txtSummary" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("summary") %>'>
</asp:TextBox>
</ItemTemplate>
However, the norm is to have the label when in read mode, and a text box when in edit or update mode like shown below:
<asp:TemplateField HeaderText="Summary" SortExpression="summary">
<ItemTemplate>
<asp:Label ID="lblSummary" runat="server" Text='<%# Bind("summary") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtSummary" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("summary") %>'>
</asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtSummary" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("summary") %>'>
</asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
You have multiple "ItemTemplate" declarations in there. There should be only one.