I have a gridview with a dropdownlist in one column in editing mode
The problem is that I can't save the selected value after edit and I can show the data from the field when I press edit.
I show some examples doing SelectedValue='<%# Bind("Category") %>' but the SelectedValue is unrecognizable.
The page is
<asp:TemplateField HeaderText="Category" SortExpression="Category">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="Category" >
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Category") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
When I use a textbox instead of the dtopdown then the values after editing are saved correctly
Any ideas?
Finally I found a solution to my problem.
In the templatefield I've select edit databindings and then in the SelectedValue I've select Custom Binding and put Bind("category")
Now it works
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
I have a GridView with some TemplateField items containing TextBox controls. I would like to add a required field validator on it. This is my code:
<asp:TemplateField HeaderText="vid">
<EditItemTemplate>
<asp:TextBox ID="txtvid" runat="server" Width="150px"
Text='<%# Bind("vid") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label
ID="lblvid" runat="server"
Text='<%# Bind("vid") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
How do I place a required field validator on txtvid?
In the Edit template, add a RequiredFieldValidator like this:
<EditItemTemplate>
<asp:TextBox ID="txtvid"
runat="server" Width="150px"
Text='<%# Bind("vid") %>'>
</asp:TextBox>
<asp:RequiredFieldValidator
ControlToValidate="txtvid"
runat="server"
ErrorMessage="Please enter a 'vid' number"
Text="*"/>
</EditItemTemplate>
Here is the reference for the RequiredFieldValidator on MSDN.
UPDATE:
If you wanted a regular expression validator, its pretty much the same, but with the RegularExpressionValidator control:
<asp:RegularExpressionValidator
ControlToValidate="txtvid"
ValidationExpression="\d{10}"
runat="server"
ErrorMessage="Please enter a 'vid' of 10 digits"
Text="*"/>
Here is a complete list of the functionality for the RegularExpressionValidator on MSDN.
Within gridview i assign textbox,requiredfieldvalidator and button,This validator validate all textboxes in gridview when button click without filling textbox. How can i solve this..
<asp:TemplateField HeaderText="vid">
<ItemTemplate>
<asp:TextBox ID="txtvid" runat="server" Width="150px" ValidationGroup ="subgrp">
</asp:TextBox>
<asp:RequiredFieldValidator ID="rfvQuantity" ControlToValidate="txtvid" runat="server"
ErrorMessage="Required" ForeColor="Red"
ValidationGroup = "subgrp"></asp:RequiredFieldValidator>
<asp:Label
ID="lblvid" runat="server"
Text='<%# Bind("vid") %>'>
</asp:Label>
<asp:Button ID="btnSelect" runat="server" Text="Select" ValidationGroup ="subgrp"/>
</ItemTemplate>
</asp:TemplateField>
This will validate all textboxs in gridview,When i click button in particular row without filling the textbox in itemtemplate.
I'm using asp.net and I'm creating a detail view from a database. The database I create got a column I want set auto show current when edit or insert like using timer like vb.net code
lblClock.Text = TimeOfDay
My Sample source from asp.net :
<asp:TemplateField HeaderText="Time Out" SortExpression="TIME_OUT">
<EditItemTemplate>
<asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("TIME_OUT") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox10" runat="server" Text='<%# Bind("TIME_OUT") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Bind("TIME_OUT") %>'></asp:Label>
</ItemTemplate>
What I want is when I click the edit button it will automatically show the current time and then I just click update.
try this..........TextBox1.Text = DateTime.Now.ToString("HH:mm");
In your Page Load event add this code
lblClock.Text = DateTime.Now.ToString();
Possible duplicate Formatting DataBinder.Eval data
i want to change the format of an item template in a gridview, i want to show the currency..
this is my code:
<asp:TemplateField HeaderText="Monto">
<ItemTemplate><%# Eval("Monto")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtMonto" Text='<%# Eval("Monto", "{0:C2}")%>' /><%-- Eval("Price", "{0:C2}")--%>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtfooterMonto" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" id="rfvMonto" controltovalidate="txtfooterMonto" Display="None"
errormessage="Monto" ValidationGroup="InsertValidationControls"/>
</FooterTemplate>
</asp:TemplateField>
You have formatting setup for editing here: EditItemTemplate
But not for viewing here: ItemTemplate
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.