i am using bootstrap 3.0 form validation JavaScript code from https://github.com/nghuuphuoc/bootstrapvalidator with ASP.NET and it works fine, but when i add runat server to my form controls it does not work please help me on this problem.
RunAt="server" changes names / id's of controls. Instead of:
...
fields: {
firstname: {
... validiation code
}
}
try this:
fields: {
<%= firstname.UniqueID %>: {
... validiation code
}
}
Or if you are using ASP.net 4.0 and you can guarantee uniqueness in the name, you can add ClientIDMode="Static" to the textbox to prevent ASP.net from appending to your Id.
Related
I am using telerik RadTextBox inside asp.net User Control page.
I have writen below Jquery validation code inside main content page as shown below, I am trying to set RadText box name attribute as static inside User Control so that I can use it in Main Content page.
User Control page
<telerik:RadTextBox ID="txtProductPrice" ClientID="txtProductPrice"
runat="server" ClientIDMode="Static" />
Main Content Page:
$("#aspnetForm").validate({
rules: {
ctl00$ContentPlaceHolder1$ucUserInfoI$txtProductPrice: {
required: true,
number: true,
maxlength: 5
},
}
});
I have used dynamically generated name attribute to validate the textbox, which looks ugly.
Since RadTexBox is in UserControl page , I am not able to use RadTexBox with UniqueID in main content page as below:
$("#aspnetForm").validate({
rules: {
<%=txtProductPrice.UniqueID %>:{
required: true,
number: true,
maxlength: 5
},
}
});
Any idea how to I can use RadTexBox with UniqueID in main content page ?
Any help would be great.
These steps can prove helpful to you:
1. For client-side logic, you can try using <%=txtProductPrice.ClientID %> instead of UniqueID.
2. Telerik controls are complex server and script controls and you should not use ClientIDMode="Static" with them. Details
3. Make sure you have the following setting in your web.config file as suggested by Sparky:
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
Here are some Details and More Details.
4. You can always use the $telerik.findControl method to manually access the RadTextBox and execute your custom logic. Details
Can you please show me a sample code of loading image during data binding using telerik grid asp.net mvc Razor.
Thanks in advance.
Check out the client events for OnDataBinding and OnDataBound. I would show a loading spinner when the OnDataBinding starts, then hide it again when the next method is called.
<%= Html.Telerik().Grid(Model)
.Name("Grid")
.ClientEvents(events => events.OnDataBinding("Grid_onDataBinding")
.OnDataBound("Grid_onDataBound")) %>
And then, the javascript would be:
function Grid_onDataBinding(e) {
$("#spinner").show();
}
function Grid_onDataBound(e) {
$("#spinner").hide();
}
Consider sample piece of code in asp.net which has a master page associated with it
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderA" Runat="Server" >
<asp:TextBox ID="TextBoxB" runat="server" CausesValidation="True" Height="96px" Width="426px" />
</asp:Content>
When the page is rendered in browser id generated for textbox with id "TextBoxB" is
ctl00_ContentPlaceHolderA_TextBoxB
Below is the equivalent html code.
<input name="ctl00$ContentPlaceHolderA$TextBoxB" type="text" id="ctl00_ContentPlaceHolderA_TextBoxB" style="height:96px;width:426px;" />
Is it possible to have same id of TextBoxB in both HTML and aspx page.
Thanks in Advance.
No. The ID has to be unique on the HTML page, but there's no way for the ASPX page to be certain that the ID you've given is unique - it could be duplicated in for example the master page, in a user control, etc, and then you end up with two identical IDs in the output.
To get around this, ASP.NET guarantees unique IDs by qualifying an ID with the IDs of containing controls.
If you were using .NET 4.0 (I guess #Nick means the same) you could set ClientIDMode="Static" so control would have the same ID as it has in markup.
If you not using .net 4.0, but you wont to have the same id (on the html page and server side)
try this:
public class MyTextBox : TextBox
{
public override string UniqueID
{
get
{
return this.ID;
}
}
}
MyTextBox textBox= new MyTextBox();
textBox.ID = "id";
textBox.Text = "text";
this.Page.Controls.add(textBox);
My problem is the following:
I'm using client validation function of the MVC 2.0 framework.
Everything is nice, when I use the validation in a simple form.
But when I use an Ajax form, and I update the fields of the ajax form, the client validation doesn't work.
I think about, I have to refresh the validation after the ajax call but I don't know how I should do it.
Anybody can help me?
this happens because the window.mvcClientValidationMetadata fills in a different "scope" than the jquery validation or mvc client validation functions. I have solved this with jquery validation adding the following line before the ajax.begin form. Like this:
<div id="result"></div>
<% Html.EnableClientValidation(); %>
<% using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "result" }))
// here goes the form
<input type="submit" value="Create" />
<% } %>
this is the required code that needs to be added:
<script type="text/javascript">
function RefreshClientValidationMetadata() {
var allFormOptions = window.mvcClientValidationMetadata;
if (allFormOptions) {
while (allFormOptions.length > 0) {
var thisFormOptions = allFormOptions.pop();
__MVC_EnableClientValidation(thisFormOptions);
}
}
}
RefreshClientValidationMetadata();
</script>
Of course the function RefreshClientValidationMetadata() can be added in any place.
Hope this help!
Try this:
$(document).ajaxComplete(function () {
$.validator.unobtrusive.parse(document);
});
This should be really easy but I can't figure out how to make it work...
I have an ASP.NET UserControl (.ascx) with the following property:
public string LabelCssClass
{
get
{
return _labelCssClass;
}
set
{
_labelCssClass = value;
}
}
I want to bind that property into the HTML of the UserControl at run time, using the <%# syntax. I imagine it must be something along these lines:
<td class="<%# Eval("LabelCssClass") %>" >
I've tried all different versions of Eval() and so on ... I'm not getting errors but the binding isn't working, and my breakpoints show that the property is not being accessed.
Whats the correct syntax? cheers
I think what you might want is this:
<td class="<%=LabelCssClass%>">
Kevin's answer is probably closer to what you are trying to achieve; however, you can successfully use the <%# %> syntax in the standard markup if you call DataBind() on the Page itself.