I have used MVC MVC 2.0 Client Side validation, but it does not work as expected. Now I am trying to find out, what I did wrong.
How does it work?
I have this rendered form:
<form method="post" action="/Sprint/Edit/68d4886b-a86a-4f0b-b713-39219febddf3">
<fieldset>
<legend>Sprint</legend>
<table>
<tbody><tr>
<td><label for="sprint_Title">Title</label></td>
<td><input type="text" value="Wichtige Private Erledigungen" name="sprint.Title" id="sprint_Title" class="input-box t-input"></td>
<td><span class="error"><span id="sprint_Title_validationMessage" class="field-validation-valid"></span></span></td>
</tr>
<tr>
<td><label for="sprint_Date">Date</label></td>
<td>
<div id="sprint_Date" class="date-picker t-input t-widget t-datepicker"><input value="12.05.2010" name="sprint.Date" id="sprint_Date-input" class="t-input" autocomplete="off"><a title="Open the calendar" tabindex="-1" href="#" class="t-link t-icon t-icon-calendar">select date</a></div>
</td>
<td><span class="error"><span id="sprint_Date_validationMessage" class="field-validation-valid"></span></span></td>
</tr>
<tr>
<td><label for="sprint_Description">Description</label></td>
<td><textarea rows="10" name="sprint.Description" id="sprint_Description" cols="80" class="t-input">dsfs1</textarea></td>
<td><span class="error"><span id="sprint_Description_validationMessage" class="field-validation-valid"></span></span></td>
</tr>
</tbody></table>
</fieldset>
<input type="submit" value="Submit" name="Submit" id="Submit" class="t-button t-state-default">
</form>
And this script is rendered:
<script type="text/javascript">
//<![CDATA[
if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }
window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"sprint.Title","ReplaceValidationMessageContents":true,"ValidationMessageId":"sprint_Title_validationMessage","ValidationRules":[{"ErrorMessage":"Title is required.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"sprint.Date","ReplaceValidationMessageContents":true,"ValidationMessageId":"sprint_Date_validationMessage","ValidationRules":[{"ErrorMessage":"Das Feld \"Date\" ist erforderlich.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"sprint.Description","ReplaceValidationMessageContents":true,"ValidationMessageId":"sprint_Description_validationMessage","ValidationRules":[]}],"FormId":null,"ReplaceValidationSummary":false});
//]]>
</script>
But to my surprise the breakpoint only stops at page load. I think it should be also if a field is tabbed? At least, after I press the post button.
Why does client side validation not work for me?
This just sets up the handlers, it won't actually hit this line when the form is submitted, nor when you tab between fields.
You will get validation errors upon losing focus and/or form submission, however if you have other javascript errors on the page it can cause this to fail.
Also, make sure you've reference the required javascript files in your header:
MicrosoftAjax.js
MicrosoftMvcValidation.js
Related
What i have to create is a form that i migrated out of access. I created the 4 asp pages. Now what they would like is to store the user input values some how until they hit the final page before writing to the SQL database. What is the easiest way to store this data until the final page for submission? I could try an array or maybe even java script?
You could use the session object - http://msdn.microsoft.com/en-us/library/ms525095(v=vs.90).aspx
As suggested in this other answer, you could use session variables. The alternative is to just pass them from page to page using hidden form fields, eg
<input name="yourvariable" type="hidden" value="<%=Request.Form("yourvariable")%>
There's no correct answer, it's a case of which you're most comfortable with
"get_user_info.asp"
<html>
<head></head>
<body>
<form id='frm_user_info' name='frm_user_info' method='post' action='get_user_info_go.asp'>
<table cellspacing='3'>
<tr>
<td align=right>Name</td>
<td><input type='text' id='s_name' name='s_name' size='40'></td>
</tr>
<tr>
<td></td>
<td><input type='submit' value='Next'></td>
</tr>
</table>
</form>
</body>
</html>
"get_user_info_go.asp"
<%
s_name=request.form("s_name")
if (s_name="") then response.redirect("get_user_info.asp")
session("s_name")=s_name
response.redirect("next_page.asp")
%>
I have created web user control for calendar. I.e it has a textbox and a calendar image. By clicking on calendar popup the calendar opens. Then by selecting any date it stores the year of that date. I use this code:
<script language="javaScript" type="text/javascript" src="Scripts/Calendar.js"> </script>
<link href="Styles/Calendar.css" rel="stylesheet" type="text/css" />
<table>
<tr>
<td>
<input type="text" name="datum1"/>
</td>
<td>
<img id="Img1" src="images/calFinal.jpg" alt="" runat="server" onclick="setYears(1947, 2040);
showCalender(this, 'datum1');" />
</td>
</tr>
</table>
<div>
<!-- Calender Script -->
<table id="calenderTable">
<tbody id="calenderTableHead">
<tr>
<td colspan="4" align="center">
<select onchange="showCalenderBody(createCalender(document.getElementById('selectYear').value,
this.selectedIndex, false));" id="selectMonth">
<option value="0">Jan</option>
<option value="1">Feb</option>
<option value="2">Mar</option>
<option value="3">Apr</option>
<option value="4">May</option>
<option value="5">Jun</option>
<option value="6">Jul</option>
<option value="7">Aug</option>
<option value="8">Sep</option>
<option value="9">Oct</option>
<option value="10">Nov</option>
<option value="11">Dec</option>
</select>
</td>
<td colspan="2" align="center">
<select onchange="showCalenderBody(createCalender(this.value,
document.getElementById('selectMonth').selectedIndex, false));" id="selectYear">
</select>
</td>
<td align="center">
<font color="#003333" size="+1">X</font>
</td>
</tr>
</tbody>
<tbody id="calenderTableDays">
<tr style="">
<td>Sun</td>
<td>Mon</td>
<td>Tue</td>
<td>Wed</td>
<td>Thu</td>
<td>Fri</td>
<td>Sat</td>
</tr>
</tbody>
<tbody id="calender">
</tbody>
</table>
<!-- End Calender Script -->
</div>
The problem occurs when I use the user control twice on the same web form. I have registered the control. When I use it once, it works proper. But I want it at two places. So, please give some solution on how to use the user control more than one time on the same web form. I have given different IDs for the user control, but it still does not work.
Code for user control:
<tr>
<td class="directorytdWidth directorylabel">
School Name:
</td>
<td class="directoryTdPadding">
<asp:TextBox ID="txtSchoolName" runat="server" Width="120px" ForeColor="#FF9F00"></asp:TextBox>
</td>
</tr>
<tr>
<td class="directorytdWidth directorylabel">
School Passout Year:
</td>
<td class="directoryTdPadding">
<uc1:calendar ID="schoolPassout" runat="server" />
</td>
</tr>
<tr>
<td class="directorytdWidth directorylabel">
College Name:
</td>
<td class="directoryTdPadding">
<asp:TextBox ID="txtCollegeName" runat="server" Width="120px" ForeColor="#FF9F00"></asp:TextBox>
</td>
</tr>
<tr>
<td class="directorytdWidth directorylabel">
College Passout Year:
</td>
<td class="directoryTdPadding">
<uc1:calendar ID="collegePassout" runat="server" />
</td>
</tr>
Use the Same User control but With diferent ID. For example (See code below)
Reg Your User control in you Web page
<%# Register Src="~/Controls/SearchAffiliated.ascx" TagPrefix="uc" TagName="SearchAffiliated" %>
First User Control call
<uc:SearchAffiliated runat="server" ID="ucSearchAffiliated" />
Second User Control Call
<uc:SearchAffiliated runat="server" ID="SearchAffiliated1" />
See The Users Controls have diferents IDs, That Works For call The same UC in one web form.
Cheers.
Use the loop of how many times that u want the usercontrol to display:
for(int i=0;i < n; i++) //Define the number of time that u want to display
{
usercontrolname.id="UC"+i;//Assign the id for that usercontrol
//add line of code,that controls in placeholder or panel
}
You can use same user control in same page in N number of times. either it dynamically or statically. Only the thing is we need to give the different ID for the usercontrol
This need to add in your Reg
<%# Register Src="~/UserControls/Hello.ascx" TagPrefix="uc" TagName="Hello" %>
calling the user control 1
<uc:Hello runat="server" ID="ucHello1" />
calling the user control 2
<uc:Hello runat="server" ID="ucHello2" />
I faced same problem but the problem was not related with the ID you prefer to use inside the asp.net page. Because I guess asp.net automatically generates ID for each element in page if it doesn't have one.
The problem was I copied ascx files and forget to change their class names. Make sure you use different class names for each user control.
<%# Control Language="C#" ClassName="ucSchema2" %>
I know this i late to the game but maybe someone can use this answer anyway.
Using a static clientside id for a html element in a user control and then using that user control multiple times will results in invalid html.
You are only allowed to use an id once on a html page otherwise getElementById (which returns a single element) will not know which element to get and then you will probably not get any element whatsoever.
You need to make those clientside ids inside the user control truely unique, i.e. selectMonth, selectYear.
This might be done by appending the ClientID of the user control to the element id. Something like this (not sure if this approach really works but you get the idea):
<select onchange="showCalenderBody(createCalender(document.getElementById('selectYear_<%=this.ClientID%>').value,
this.selectedIndex, false));" id="selectMonth_<%=this.ClientID%>">
This way the clientside id should be a combination of locally unique ids and the page unique control ids.
I have a simple password reset dialog, which enables users to set a custom password.
<form id="resetPassword"
action="XXXXX">
<fieldset><input type="hidden" name="userId" value="${user.id}" />
<table>
<tbody>
<tr>
<td><input id="resetCustomPassword" class="ui-widget" type="checkbox"></td>
<td><label>Set Custom Password</label></td>
</tr>
</tbody>
<tbody style="display: none;">
<tr>
<td><label for="newPassword"> New Password: </label></td>
<td><input id="newPassword" class="ui-widget" type="password"></td>
</tr>
<tr>
<td><label for="reTypeNewPassword"> Retype New Password: </label></td>
<td><input id="reTypeNewPassword" class="ui-widget" type="password"></td>
</tr>
</tbody>
Now, if a user checks for "Set Custom password", I dynamically show the new password fields. The problem is in my this label in the above form:
<td><label>Set Custom Password</label></td>
This gets dynamically adjusted to the RIGHT side, so it aligns with the new password fields.
How can I let my above label be static, and not adjust itself dynamically?
Thanks!
Try this:
<td align="left"><label>Set Custom Password</label></td>
You're far better off using styled unordered lists as containers for forms, rather than tables.
See: http://www.alistapart.com/articles/multicolumnlists
For Save button, I have a popup which has 3 buttons inside., Yes, No and Cancel. Yes should call a function and update 2 tables in the database and No should update just one table and cancel should not make any changes in the page.
Used code for popup
<table id="pnlPopupMerchantUpdate" runat="server" style="display:none">
<tr>
<td>
<asp:Panel runat="server" CssClass="modalPopup">
<table width="350" height="80" class="warningPopup">
<tr>
<td>
<!-- <img src="images/warning_blue.gif" alt="Warning" /> -->
</td>
<td colspan="2" align="left" style="padding-left: 75px; padding-top: 10px;">
Do you wish to update the Location Information as well.
</td>
</tr>
<tr>
<td align="center" colspan="4">
<input id="btnYesMerchant" type="button" value="Yes" class="popupButton" causesvalidation="true" onclick="btnYessave_Click"/>
<input id="btnNoMerchant" type="button" value="No" class="popupButton" causesvalidation="true" onclick="btnNosave_Click" />
<input id="btnCancel" type="button" value="Cancel" class="popupButton"/>
</tr>
</table>
</asp:Panel>
</td>
</tr>
Here if the user clicks Yes it has to update Location info along with COmpany Info. If No is clicked, just Company Info. Cancel will not modify anything. There is a function UpdateMerchantInfo() which has to modify according to what is called, i mean yes or No.
I have used Onclick events for both Yes and NO but the problem here is, Yes should call its onclick event before getting into UpdatemerchantInfo(), the same way with NO.
In UpdateMerchantInfo(), i want to use if loop and do accordingly.
I hope i am clear!!
Thank you much!
You can use popup control extender in ajax. Will find more details Here
It has sufficient tags to suport your requirements.
You have to just create a contol(using tags) and in popup controlid set id of popup. Using this method you can define different eventhandlers.
I am having problems getting my form to Post to my Save method in a controller. I am new to MVC, and have followed several examples to try to get this to work. Here is my html markup:
<form action="Edit/Save" method="post">
<fieldset>
<legend>Personal Information</legend>
<table class="editGrid">
<tr>
<td><label for="txtFirstName">First Name:</label></td>
<td><input type="text" id="txtFirstName" value="<%=user.FirstName %>" name="FirstName" /></td>
</tr>
<tr>
<td><label for="txtLastName">Last Name:</label></td>
<td><input type="text" id="txtLastName" value="<%=user.LastName %>" name="LastName" /></td>
</tr>
<tr>
<td><label for="txtNtLogin">NT Login:</label></td>
<td><input type="text" id="txtNtLogin" value="<%=user.NtLogin %>" name="NtLogin" /></td>
</tr>
<tr>
<td><label for="txtHireDate">Hire Date:</label></td>
<td><input type="text" id="txtHireDate" value="<%=string.Format("{0:d}",user.HireDate) %>" name="HireDate" /></td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>Job Information</legend>
<table class="editGrid">
<tr>
<td><label for="CostCenters">Cost Center:</label></td>
<td><%=Html.DropDownList("CostCenters")%></td>
</tr>
<tr>
<td><label for="Managers">Manager:</label></td>
<td><%=Html.DropDownList("Managers")%></td>
</tr>
<tr>
<td><label for="Responsibilities">Responsibility:</label></td>
<td><%=Html.DropDownList("Responsibilities")%></td>
</tr>
<tr>
<td><label for="Departments">Department:</label></td>
<td><%=Html.DropDownList("Departments")%></td>
</tr>
<tr>
<td><label for="Active">Active:</label></td>
<td><%=Html.CheckBox("Active",user.Active) %></td>
</tr>
<tr>
<td><label for="txtHireDate">Hire Date:</label></td>
<td><%=Html.TextBox("txtHireDate",string.Format("{0:d}",user.HireDate)) %></td>
</tr>
<tr>
<td><label for="txtReleaseDate">Release Date:</label></td>
<td><%=Html.TextBox("txtReleaseDate",string.Format("{0:d}",user.ReleaseDate)) %></td>
</tr>
</table>
</fieldset>
<input type="submit" value="Save Changes" />
</form>
This form routes to a Save method in my EditController. Here is the code for my EditController's Save method:
public class EditController : Controller
{
public ActionResult Save()
{
//Save code goes here
}
I have tried using the html form tag, and also the Html helper code:
using (Html.BeginForm("Save", "Edit"))
Here is the entry from my RegisterRoutes method in the Global.asax file:
routes.MapRoute("EditSave", "{controller}/Save",
new { controller = "Edit", action = "Save" });
No matter what I do, the submit button does not trigger the Save method. Yet, if I manually key in the Url, The code breaks right into the Save method.
Edit:
Per Craig Stuntz's comment, I checked the source of the page. The page actually contains 2 forms, although only 1 is coded on the page by myself: Here is the HTML that appears prior to my form tag:
<form name="aspnetForm" method="post" action="44" id="aspnetForm">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNjM3OTAyNTUzZGQrHhVn9+t78aHxN0vHvKUJ8DQWlQ==" />
</div>
<div id="nav">
<span id="navLinks">
Placeholder Link
</span>
<span id="userName">
<span id="ctl00_lblUserName" class="UserName">Welcome, Test User</span>
</span>
</div>
<div id="Content">
<div id="formContainer">
<form action="Edit/Save" method="post">
I didn't think MVC generated viewstate or additional form tags. I am pulling data and populating it into this form using another method from the same controller. Am I doing something wrong here?
Ok, got the answer. And thanks Craig for having me look at the HTML again! My master page had generated a form tag in it without my knowing it, so I essentially had nested forms on the same page. After I removed the form from the Master Page, everything worked perfectly.