Archive

Archive for the ‘ASP .Net’ Category

jQuery – enable / disable (readonly) and how to reference .Net Server side Controls

8 September, 2010 Leave a comment

Ok, I am just starting to play around with jQuery and these are a couple of tips that I have found useful.

1. How to access .net server side controls
The code to get the value from a html input control <input id=”name”> might be as follow:
var name = $(‘#name’).val();

For a server side control like <asp:TextBox ID=”txtName” runAt=”sever” /> you can reference it as follows:
var name = $(‘#<%=txtName.ClientID%>’).val();

I have a user control for date selection called the DateSelector, and it has a control inside it called txtDate which stores the selected date.
If I want to get the value from within a user control like this I can do:

var dte = $(‘#<%=dteDateSelector.ClientID%>_txtDate’).val();

2. Disable a control
In this case I want to disable a text box from the txtName example above:
$(‘#<%=txtName.ClientID%>’).attr(“disabled”, true);
to clear it
$(‘#<%=txtName.ClientID%>’).removeAttr(“disabled”)

A problem with disabling controls in this way is that their values cannot be read in javascript, so another way is to use the “ReadOnly” attribute
$(‘#<%=txtName.ClientID%>’).attr(“readonly”, true);
to clear it
$(‘#<%=txtName.ClientID%>’).removeAttr(“readonly”)

Cheers

Categories: ASP .Net, jQuery

ASP.Net Membership Provider – Install Database

30 August, 2010 Leave a comment

In asp.net the membership provider model is a cool way of easily getting a security datamodel with role access.

The database can be installed on your own specific database in the following way.

Some architecture based sugestions:
1. The membership provider model is actually keyed by application. So you can provide user lists for multiple applications from a single database. This means that instead of creating the model inside you application database you can store this in a central applicaiton security database and provide a single database that manages users for all of your applications.

Pre-Requisites
1. Ensure you have SQL server installed on your system. This could be SQL Server Express.

Installation
1. Open a command window.
2. cd to the following directory [drive:]\%windir%\Microsoft.NET\Framework\version
3. run aspnet_regsql.exe ( by default this will run in the Wizard Mode)

In the wizard:
1. Click Next
2. Choose “Configure sql server for application services” and click next
3. Select the Server and Database set your database connection details (note change to \sqlExpress named instance)
4. Choose Next Next Finish

Your database has now been created and is ready to be used.

Categories: ASP .Net

RequiredFieldValidator conditional validation via client side script

Interesting Problem.

I created a user edit page on an ASP.Net website. A user when looking at their own record got a “Change Password” panel. It is optional and only updated if data is entered in the text boxes. I wanted to use the CompareValidator to compare the txtPassword and txtConfirmPassword textboxes.

This worked great, I set up the CompareValidator to validate the txtConfirmPassword against the txtPassword. Problem is the ConfirmValidator does not fire when there is nothing entered in the txtConfirmPassword textbox. So the user could enter a password and click submit and the compare validator would not fire and the page would submit.

So I needed a RequiredFieldValidator on txtConfirmPassword but this is only needed to be enabled after some text is entered in the txtPassword text box.

I found this article:
http://www.aspdotnetfaq.com/Faq/How-to-control-ASP-NET-Validator-Controls-Client-Side-validation-from-JavaScript.aspx

and implemented this javascript. Note I am using a little jQuery to get the controls. (1.4.1)

And using this information was able to write the following script:

<script type="text/javascript">
    $(document).ready(function() {
        $('#<%=txtPassword.ClientID %>').change(function() {
            var value = $('#<%=txtPassword.ClientID %>').val();
            if(value.length > 0){
                EnableValidators(true);
            } else {
                EnableValidators(false);
            }
        });
    });

    function EnableValidators(enable)
    {
        ValidatorEnable(document.getElementById('<%=rfvConfirmPassword.ClientID%>'), enable);
    }
</script>

The interesting thing is “ValidatorEnable” is a Asp.Net function that client-side enables and disables the validators.

Nice 🙂

Categories: ASP .Net, c#

Textbox – supress previous entries – auto complete off

1 April, 2010 Leave a comment

Hi,

I am developing a website and noticed that all my text boxes were repeating the content that had been entered previously.

Found this blog here;
http://ryanfarley.com/blog/archive/2005/02/23/1739.aspx (I like ryans stuff).

In short you can turn this off via the autocomplete=”off” property.
This can be set in a number of ways:

  1. On the form, this disables it for all content on the form. This is really handy if you are using master pages as add it to your master page and job done, your entire site now stops prompting.
  2. On a control by control basis;
    <asp:TextBox ID="txtText" runat="server" autocomplete="off" />

Note: Visual studio will complain that it does not know this property but it works none the less so just ignore VS.

Cheers

Categories: ASP .Net

AJAX PopupControlExtender and Dynamic Controls

28 February, 2008 3 comments

I was attempting to use the PopupControl Extender the other day, to create myself a web user control for colour selection.

Here is what I ended up creating..

Figure 1

The problem I had was that the colour palette panel is being created dynamically. In this example it is a 25×16 matrix made up of 375 dynamically created link buttons with their backcolour set to the colour from the palette. (Note: Used a link button due to postback.Asynch javascript errors that occur with PopupControl Extender in a modal dialog box as documented elsewhere on the web).

When a user clicked on one of the link buttons it performed a postback, problem was it was doing a full postback and closing the popup window.

Solution: When creating your controls dynamically to exist in your panel to be displayed by the popup extender you need to register them with the UpdatePanel triggers collection like so:

LinkButton btnColour = new LinkButton();
... setup you link button
btnColour.ID = colurHexCode; // colurHexCode string variable containing the current colour I am setting as the backgroud
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = btnColour.ID;
UpdatePanel1.Triggers.Add(trigger);

This forces the dynamic control to be included in the update panel and when clicked only performs a partial update panel postback.

Sweet

Categories: AJAX, ASP .Net

Setting the Default Button on Pressing Enter

27 February, 2008 1 comment

Issue
On a page with a single text box and a button, place your cursor in the text box and press enter. The page will submit back to itself but not hit the click event behind your submit button as expected.

See here: http://aspnet.4guysfromrolla.com/articles/060805-1.aspx for a full discussion of the issue.

Solution
The fix for this simple problem is to add another text box like so:

<asp:textbox runat="server"  
style="visibility:hidden; display:none;">

This however only fixes the page posting to itself, we now need to get the page to fire the default button’s click event.My problem is further complicated by these facts:

1. The page is using a master page with the main content area containing the text box and a footer content area containing the button.

2. The button is not an  <asp:button…  but a custom button control that uses an asp:hyperlink control.

There is a DefaultButton property that can be set on the form. The caveat being when using master pages you must use the uniqueID of the control. So in my page onload event the following line of code needs to be added.


this.Page.Form.DefaultButton = btnOK.UniqueID;

This causes the btnOk “Click” event to be fired.

Yeah!

Categories: ASP .Net