I use Visual Studio 2010 in ASP.NET with C# code behind.
I have an ASP.NET page with code in Source View:
<p>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
</p>
If I switch in Design View and come back to Source View in VS, it unexpectedly removes the last </p> automatically.
Do you have the same problem in your Visual Studio?
Any ideas how to solve it?
Thanks.
P.S. Here my full code:
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<h1>
Create Groups Types</h1>
<p>
<asp:DetailsView ID="uxCreateGroupsTypesDisplayer" runat="server" AutoGenerateRows="False"
DataKeyNames="GroupTypeId" DataSourceID="uxEntityDataSourceCreateGroupsTypes"
DefaultMode="Insert"
oniteminserted="uxCreateGroupsTypesDisplayer_ItemInserted"
oniteminserting="uxCreateGroupsTypesDisplayer_ItemInserting">
<Fields>
<asp:TemplateField HeaderText="TypeGroup" SortExpression="TypeGroup">
<InsertItemTemplate>
<asp:TextBox ID="uxTypeGroupInput" runat="server" Text='<%# Bind("TypeGroup") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="TypeGroup field is required."
ControlToValidate="uxTypeGroupInput" Text="*">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="uxRegularExpressionTypeGroup" runat="server"
ControlToValidate="uxTypeGroupInput" ErrorMessage="TypeGroup is too long or short. Change the field accordingly."
ValidationExpression="^.{4,40}$">*</asp:RegularExpressionValidator>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Summary" SortExpression="Summary">
<InsertItemTemplate>
<asp:TextBox ID="uxSummaryInput" runat="server" Text='<%# Bind("Summary") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorSummary" runat="server" ErrorMessage="Summary field is required."
ControlToValidate="uxSummaryInput" Text="*">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="uxRegularExpressionSummary" runat="server" ControlToValidate="uxSummaryInput"
ErrorMessage="Summary is too long or short. Change the field accordingly." ValidationExpression="^.{4,256}$">*</asp:RegularExpressionValidator>
</InsertItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
</p>
<p>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" /></p> <!-- The problem is here - This tag disappear if you switch from source view to design view and back to source view -->
<asp:EntityDataSource ID="uxEntityDataSourceCreateGroupsTypes" runat="server"
EnableFlattening="False"
EnableInsert="True" EntitySetName="CmsGroupsTypes">
</asp:EntityDataSource>
</asp:Content>
In VS2008, replaced:
<p>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" /></p>
With:
<p>
<asp:ValidationSummary ID="ValidationSummary1" runat="server"></asp:ValidationSummary></p>
This works, although does not agree with the closing tag in markup. Rendering is correct though in IE8.
Why does this summary control need to be enclosed in a paragraph tag? The Validation Summary renders as a block element.
Don't use Design View to edit markup, always use Source View, i.e. markup itself to edit markup.
IMO Design View just a simple, read-only, verify-purpose view.
Related
I have reapeater in my code and trying to validate the textbox using asp.net required field validator. But validation messsage
not displaying, i opened the developer tools and found that style="visibility:hidden" added into the required field validator.
Below is my code
<asp:Repeater ID="RepeaterCategory" runat="server" DataSource='<%# this.Categories.Count==0 ? null : this.Categories %>'>
<ItemTemplate>
<div>
<asp:Label runat="server" Visible="true" Text="Category" />
<asp:PlaceHolder runat="server" Visible="true">
<asp:TextBox ID="txtCategoryID" runat="server" value="1" />
<asp:TextBox ID="txtCategoryName" runat="server" value="<%# (Container.DataItem as Category).Name %>" />
<asp:RequiredFieldValidator runat="server" ErrorMessage="<br/>This is a required field" ControlToValidate="txtCategoryName" ValidationGroup="NewCategoryGroup"></asp:RequiredFieldValidator>
</asp:PlaceHolder>
<asp:LinkButton runat="server" ToolTip="Save" ValidationGroup="NewCategoryGroup" OnClick="SaveCategory_Click"><img src='<%# some path%>/images/save.gif' /></asp:LinkButton>
<asp:LinkButton runat="server" ToolTip="Close" OnClick="CloseCategory_Click"></asp:LinkButton>
</div>
</ItemTemplate>
</asp:Repeater>
Code behind file
protected void SaveCategory_Click(object o, EventArgs e)
{
Page.Validate("NewCategoryGroup");
if (!Page.IsValid)
return;
//logic
}
Can anyone suggest how to enable it?
The style="visibility:hidden" is default behavior. It changes to style="visibility: visible;" when the error message needs displaying. Therefore you probably don't have an error.
The validator is a 'RequiredFieldValidator', and since the textbox that is being validated is already filled with the value "TestCategory" there are no errors. If you just add text to the Save button (which has no ID tag) so that it becomes visible, remove the value from the txtCategoryName textbox and click the save button you will see the error message.
This works:
<asp:TextBox ID="txtCategoryName" runat="server" value="" />
<asp:RequiredFieldValidator runat="server" ErrorMessage="<br/>This is a required field" ControlToValidate="txtCategoryName" ValidationGroup="NewCategoryGroup"></asp:RequiredFieldValidator>
<asp:LinkButton runat="server" ToolTip="Save" ValidationGroup="NewCategoryGroup" OnClick="SaveCategory_Click" ID="LinkButton1">Save Me!</asp:LinkButton>
You don't need this code
Page.Validate("NewCategoryGroup");
if (!Page.IsValid)
Another advantage is that the validators work now without a postback, this saves a roundtrip to the server.
And ALWAYS do server side validation also, but try to do the first validation without postback.
UPDATE
What you want is probably validation per item. And since your validationgroup is always the same it will fire for all textboxes. Try this:
<asp:Repeater ID="RepeaterCategory" runat="server">
<ItemTemplate>
<div>
<asp:TextBox ID="txtCategoryName" runat="server" ValidationGroup='<%# "myVal_" + Container.ItemIndex %>' Text='<%# Eval("Category") %>' />
<br />
<asp:RequiredFieldValidator runat="server" Display="Dynamic" ErrorMessage="This is a required field<br />" ControlToValidate="txtCategoryName" ValidationGroup='<%# "myVal_" + Container.ItemIndex %>'></asp:RequiredFieldValidator>
<asp:LinkButton runat="server" ValidationGroup='<%# "myVal_" + Container.ItemIndex %>' OnClick="Button1_Click" ID="LinkButton1">Save Me!</asp:LinkButton>
</div>
</ItemTemplate>
</asp:Repeater>
I have a validator with a bound text field (it's set via ControlToValidate). How can I make the validator's error message appear nearby another control (a label above this text field)?
Just put the validator control nearby the control where you want to show the message i.e. wherever you want to show the message just put the validator control there.
In the following example I am showing validation message near some other control not near the text box.
<form id="form1" runat="server">
<asp:Label ID="lblNameRequired" runat="server" Text="*Name :"></asp:Label>
<asp:TextBox ID="txtNameRequired" runat="server" ValidationGroup="Validation"></asp:TextBox>
<br />
<asp:Label ID="lblGenderRequired" runat="server" Text="*Gender :"></asp:Label>
<asp:DropDownList ID="ddlGenderRequired" runat="server" ValidationGroup="Validation">
<asp:ListItem Selected="True" Value="-1">--Select--</asp:ListItem>
<asp:ListItem Value="0">Male</asp:ListItem>
<asp:ListItem Value="1">Female</asp:ListItem>
</asp:DropDownList>
<asp:CompareValidator ID="CompareValidatorGender" runat="server" ControlToValidate="ddlGenderRequired"
Display="Dynamic" ErrorMessage="Gender is Required" Operator="NotEqual" ValidationGroup="Validation"
ValueToCompare="-1"></asp:CompareValidator>
<br />
<asp:Label ID="lblValidation" runat="server" Text="Fields marked with * are required"></asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ControlToValidate="txtNameRequired"
Display="Dynamic" ErrorMessage="Name is Required" ValidationGroup="Validation"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btnValidate" runat="server" Text="Validate Input" ValidationGroup="Validation" />
<br />
</form>
Hope this helps you.
That's it, really. I have a DetailsView and a button on my ASP.NET page, and the button always appears beneath the DetailsView. Using floating DIVs breaks things in other ways, so is there any method to suppress the line break after the DetailsView, and have it display inline with the button?
I've tried applying Display:Inline CSS to the DetailsView, but it didn't work.
EDIT - CODE ADDED BELOW
<asp:DetailsView ID="dvPremisesYardName" runat="server" datasourceid="SQLGeneralDetails" DefaultMode="Edit" AutoGenerateRows="False" FieldHeaderStyle-CssClass="fieldtitleyardname"
GridLines="None" onchange="hideControl('imgGeneralDetailsTick');" >
<Fields>
<asp:TemplateField HeaderText="Yard Name">
<EditItemTemplate>
<asp:DropDownList ID="ddlPremisesYardName" runat="server" DataSourceID="SQLPremisesLookup" DataTextField="PremisesYardName" DataValueField="PremisesID" AutoPostBack="True"
SelectedValue='<%# Bind("AdmissionPremisesID")%>' AppendDataBoundItems="True" >
<asp:ListItem Text="Unknown" Value="" />
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
<asp:Button ID="btnPremisesAdd" runat="server" Text="Add New Premises" />
<asp:Button ID="btnPremisesEdit" runat="server" Text="Edit" />
As you stated, you do not want to use float div. The easiest way will be to use table.
Although we do not like to use table for page layout, I cannot think of other solution.
<table>
<tr>
<td><asp:DetailsView .../></td>
<td><asp:Button.../></td>
<td><asp:Button.../></td>
</tr>
</table>
I am helping my daughter build an asp.net website that has an Access database. We are using a DataList with a couple fields inside the ItemTemplate. The one that is giving me trouble is the Hyperlink. The code is below. When the page loads the hyperlink renders like this:
<a id="ContentPlaceHolder1_DataList1_lnk_6" href="#http://www.washingtonfaire.com/#" target="_blank">Washington Midsummer Renaissance Faire</a>
But when I click on the link, it tries to navigate to "http://localhost:1852/BOOMPiratesB/Raids.aspx#http://www.washingtonfaire.com/#"
What are we doing wrong?
Here is our code.
<asp:DataList ID="DataList1" runat="server" DataSourceID="AccessDataSource1">
<ItemTemplate>
<asp:HyperLink id="lnk" runat="server" NavigateUrl='<%# Eval("Link") %>' Target="_blank" Text='<%# DataBinder.Eval(Container.DataItem, "VenueName")%>'>
</asp:HyperLink>
<br />
<asp:Label ID="DateTextLabel" runat="server" Text='<%# Eval("DateText") %>' />
<br />
<asp:Label ID="CityStateLabel" runat="server" Text='<%# Eval("CityState") %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/my.mdb"
SelectCommand="SELECT [VenueName], [Link], [DateText], [Season], [DateStart], [CityState] FROM [qpgRaid]">
</asp:AccessDataSource>
I cannot see where the extra # signs are coming from. They don't appear to be in the field in the table.
It's very puzzling. And insight would be most appreciated.
Have a look at this question
I guess you are using an access hyperlink column to store the link. If you convert it to a text column it should start working fine, or you may have to strip the '#'s manually.
I have a page that has a listview that is used for inserting and editing records.
Assigning a RequiredFieldValidator and ValidatorCallOutExtender to the InsertItemTemplate works well.
When I try to do the same on the EditItemTemplate the ValidatorCallOut appears but with no text in the box.
Is there something that I'm doing wrong?
My code for the InsertItemTemplate:
<asp:TextBox ID="date_timeTextBox" runat="server" Text='<%# Bind("date_time") %>' />
<asp:RequiredFieldValidator
ControlToValidate="date_timeTextBox"
ID="RequiredFieldValidator1"
runat="server"
ErrorMessage="date_time is required"
Display="None"
ValidationGroup="insert_into">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender1"
runat="server"
TargetControlID="RequiredFieldValidator1">
</cc1:ValidatorCalloutExtender>
And for the EditItemTemplate:
<asp:TextBox
ID="date_timeTextBox"
runat="server"
Text='<%# Bind("date_time","{0:yyyy-MM-dd}") %>' />
<asp:RequiredFieldValidator
ControlToValidate="date_timeTextBox"
ID="reqDTT"
runat="server"
ErrorMessage="date_time is required"
Display="None"
ValidationGroup="edit_validate">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender
ID="val_reqDTT"
runat="server"
TargetControlID="reqDTT">
</cc1:ValidatorCalloutExtender>
Make sure your ID's are unique across your Templates, so the ControlToValidate="date_timeTextBox" is different.
InsertTemplate
<asp:TextBox ID="date_timeTextBoxInsert" runat="server" Text='<%# Bind("date_time") %>' />
EditTemplate
<asp:TextBox ID="date_timeTextBoxEdit" runat="server" Text='<%# Bind("date_time") %>' />