NW Web Designs

... keeping it simple

8. January 2013 23:44
by Greg
0 Comments

Custom Validators in Asp.Net C#

8. January 2013 23:44 by Greg | 0 Comments

Your standard Custom Validator:

 

<asp:CustomValidator ID="cvTimeDiff" ValidationGroup="Custom" runat="server"
Display="Dynamic" ErrorMessage="End time should be greater than Start time"
OnServerValidate="CompareTime"></asp:CustomValidator>

 

The attribute "OnServerValidate" is the client-side part of the validator and is what I use by default.
Its value is the name of the server method used to validate something to true or false.

This is the server side code:

 

   protected void CompareTime(object source, ServerValidateEventArgs args)
      {
          *** Do some validation and return the results as boolean b ***
          args.IsValid = b;
      }



Now your ready to fire the custom validator.

First call the validate method:

    Validate("Custom");


Then check if the page is valid and do something:

   if(Page.IsValid)
   {
    Do something when valid
   }

23. December 2012 07:00
by Greg
0 Comments

There is a duplicate ‘system.web.extensions/scripting/scriptResourceHandler’ section defined

23. December 2012 07:00 by Greg | 0 Comments

Lots of things can cause this error to pop-up, but for me it was something very obscure.  I tried everything I could find on the web … then I actually read the compile error.

CVT1108 : cannot open C:/Windows/TEMP/xxx.tmp for writing

So, I went to the ‘Windows\temp’ folder and checked the permissions, and there it was … make sure that the IIS _IUSRS account has read, write and delete rights on the folder. Some how mine disappeared. After I reset them I was back online. How they disappeared I don’t know, but I have a sneaky idea it was the latest update from Microsoft.

23. December 2012 06:59
by Greg
0 Comments

Bin Deploying ASP.NET MVC-3

23. December 2012 06:59 by Greg | 0 Comments

If your server doesn’t have ASP.NET MVC 3 installed, you’ll need to make sure the following set of assemblies are deployed in the bin folder of your web application:

  • Microsoft.Web.Infrastructure.dll
  • System.Web.Helpers.dll
  • System.Web.Mvc.dll
  • System.Web.Razor.dll
  • System.Web.WebPages.Deployment.dll
  • System.Web.WebPages.dll
  • System.Web.WebPages.Razor.dll

23. December 2012 06:54
by Greg
0 Comments

JQuery Datepicker and Min-max dates

23. December 2012 06:54 by Greg | 0 Comments

jQuery date picker ... setting a minimum and maximum date.

Set a TextBox with a class of datepick:

<asp:TextBox id="txtDate" runat="server" CssClass="datepick">
</asp:TextBox>

jQuery script in head or footer:

<script type="text/javascript">
        $(document).ready(function() {
            $('input.datepick');
            $.ajax({
               type: 'POST',
               url: 'ClassTestsAdd.aspx/GetMinDate',
               contentType: 'application/json; charset=utf-8',
               dataType: 'json',
               success: function(data) {
               var msg = data;
               if (msg.hasOwnProperty('d'))
                msg = msg.d;
               $('input.datepick').datepicker({
               maxDate: '0',
               minDate: msg,
               onSelect: function() {
                  $("select[tabindex=" + (this.tabIndex + 1) + "]").focus();
                 }
                });
               },
               error: function(data) {
               $('input.datepick').datepicker({
               maxDate: '0',
               onSelect: function() {
                 $("select[tabindex=" + (this.tabIndex + 1) + "]").focus();
                }
               });
               }
           });
       });
   </script>

23. December 2012 02:32
by Greg
0 Comments

Ajax call for an Asp.Net page

23. December 2012 02:32 by Greg | 0 Comments

Below is a basic Ajax call using jQuery

 

<script type="text/javascript">
 $(document).ready(function() {

// reset input fields

 $("#btnReset, #btnResetTop").click(function() {
 $('input.test').removeClass('check');
 $(':input', '#aspnetForm')
 .not(':button, :submit, :reset, :hidden')
 .val('')
 .removeAttr('checked')
 .removeAttr('selected');
 $('select').val('0');
 $('input.test').addClass('check');
 $('.errorlabel').html('');
 });

// fire ajax call on blur of textbox

 $('input.check').blur(function() {
 //                alert('in blur');
 var idd = this.id;
 var itt = this.value

 $.ajax({
 type: 'POST',
 url: 'EnterTests.aspx/GetStudent',
 data: '{"it": "' + itt + '"}',
 contentType: 'application/json; charset=utf-8',
 dataType: 'html',
 success: function(data) {
 var msg = eval('(' + data + ')');

 if (msg.hasOwnProperty('d'))
 msg = msg.d;
 if (msg == "error") {
 $("[id$=lblError" + idd.substring(36) + "]").attr('Visible', true);
 $("[id$=lblError" + idd.substring(36) + "]").html('No Record For Student SID');
 }
 else {
 $("[id$=lblError" + idd.substring(36) + "]").attr('Visible', true);
 $("[id$=lblError" + idd.substring(36) + "]").html(msg);
 }
 },
 error: function(XMLHttpRequest, textStatus, errorThrown) {
 alert("An error has happened. Please try again later.");
 }
 });
 });
 });
 Sys.Application.add_init(function() {
 });

 </script>

23. December 2012 02:28
by Greg
0 Comments

Finding row data on button click

23. December 2012 02:28 by Greg | 0 Comments

For some one reason I use this a lot. Seems to be a lot easier then grabbing it off a grid event

 

protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lnkbtn = (LinkButton)sender;
// command argument from selected row
string id = lnkbtn.CommandArgument;
GridViewRow row = (GridViewRow)lnkbtn.NamingContainer;
// row selected
int gridRow = row.RowIndex;
DataKey key = GridView1.DataKeys[gridRow];
// data key 'Title' from selected row
string k = (string)key["Title"];
Response.Write("title: " + k + ",  Id: " + id);
}

23. December 2012 02:04
by Greg
0 Comments

The TargetControlID of ‘_AccordionExtender’ is not valid. The value cannot be null or empty

23. December 2012 02:04 by Greg | 0 Comments

You have a page with an Ajax Accordion control and you have something like Adobe Contribute edit the page … and now you have an error.  Easy fix … your missing the ID tag on one of your Accordion panels.  I see this a lot on my work site @SBCTC when Contribute edits a page with the Accordion panels.

 

< cc1:accordion id="accAccordion2" runat="server" autosize="None" contentcssclass="accordionContent"
fadetransitions="false" framespersecond="10" headercssclass="accordionHeader"
headerselectedcssclass="accordionHeaderSelected" height="81px" requireopenedpane="false"
selectedindex="-1" suppressheaderpostbacks="true" transitionduration="400" width="100%" >

23. December 2012 02:01
by Greg
0 Comments

Accessible Dropdown Form Submittal in MVC

23. December 2012 02:01 by Greg | 0 Comments

I was trying to figure out how to submit a form when selecting from a drop down list … and make it keyboard accessible. My first and the obvious choice was the onclick event. Works great with a mouse but not so great when using just the keyboard, as a sightless person would do. Took some digging but the onchange event works like a charm. You can tab into a drop down, use the up/down keys and the enter key to post. So for an accessible drop down selection, don’t use the onclick event use the onchange event as shown below.

 

< % using (Html.BeginForm()) { %>
< %: Html.ValidationSummary(true) %>
< %: Html.AntiForgeryToken() %>
<div class="editor-label" style="padding-top: 10px;">< %: Html.LabelFor(model => model.SelectedProviderId) %></div>
<div class="editor-field">< %: Html.DropDownListFor(model => Model.SelectedProviderId,
Model.Providers, new { onchange = "this.form.submit();" })%></div>
< % } %>

23. December 2012 01:58
by Greg
0 Comments

Telerik MVC Grid – Check All Checkboxes

23. December 2012 01:58 by Greg | 0 Comments

Little jQuery example to add a check all check box to MVC Grid.


< % Html.Telerik().Grid(Model.Records) .Name("GridID") .Columns(columns => { columns.Bound(o => o.Name) .Width(250); columns.Bound(o => o.Transfer) .HtmlAttributes(new {@style = "text-align: center;" }) .HeaderTemplate("Check All <input type='checkbox' id='chkAll' />") .Template(o =>{%>< %: Html.CheckBox(Convert.ToString(o.Transfer))%>< %}) .ClientTemplate("<input type='checkbox' name='Transfer' <#= Transfer? \"checked='checked'\" :\"\" #> />") .Width(100); }) .DataBinding(dataBinding => dataBinding.Ajax().OperationMode(GridOperationMode.Client)) .Render(); %> <script type="text/javascript">// < ![CDATA[ $(function () { $('#chkAll').click(function () { $(this).parents('#GridID').find(':checkbox').attr('checked', this.checked); }); }); // ]]></script>