Wednesday, October 6, 2010

How to get the readonly textbox value on the server

In ASP.NET 2.0 and Newer versions , the readonly TextBox will ignore the submitted text , this change due to security reasons .

However , assume you have a readonly textbox and you are going to change its value on the client side ( via script) ,
Now how you will get the changed value on the server side ? One option is to disable the textbox control ,
and set "SubmitDisabledControls" for the form to true , but this is not always a solution .

For example you want to use the Calendar extendar with the textbox and you don't want to let the user to change the textbox manually ,
instead you want to let the user select the date via the calendar extendar. now the problem is that the selected date will not be
accepted by the textbox when the user submit the form , because the textbox is in readonly mode ,
in this case you need to manually set the submitted textbox value , you can easily use the values that is submitted via form collection ,

something like this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TextBox1.Text = Server.HtmlEncode(Request.Form(TextBox1.UniqueID))
End Sub

There is also many solutions other than setting the textbox as readonly ,
for example you can cancel the onCopy,onPaste,onkeyup all the required javascript events .

Generate a random number:

1. Generate a random number:
Random rnd = new Random();
intRandomNumber = rnd.Next();
rnd =null;

2. Generate a random number between a range(1 to 4)
Random rnd = new Random();
intRandomNumber = rnd.Next(1,5);
rnd =null;

UserControls

User Control: User Control is the custom, reusable controls. User Controls offers a way to partition and reuse User Interface (UI) functionality across ASP.NET Applications.
Advantages of User Control:
• Code Reuse.
• Cache the output of the control independently using technique called fragment caching. This will improve performance if used appropriately.
• Can program against any properties declared in the control, just like ASP.NET web controls.

Disadvantages of User Control:
• User Controls can be instantiated in the pages that resides in the same web application. If you want to use across applications, you need to place same .ascx control in each application.
• Can't hide code of user controls like server controls by compiling into an assembly.

Adding User Controls to an WebForms Page:
At the top of the .aspx page, add the below line above tag.
<!--Register TagPrefix="Test" TagName="TestControl" Src="Test.ascx"-->
This directive registers the control so that it can be recognized when the page is processed. TagPrefix determines the unique namespace of the control, TagName is the name of the user control and Src is the path of the user control.

If your user control is registered into Web.Config then there is no need to register on each page.
Declare user controls like
<testcontrol id="TestControl" runat="Server">>

Accessing and Setting User Controls Values in the .aspx Page:
User can access and set the values of the User Control from .aspx page through properties, using JavaScript and in code-behind of aspx page. The details of it are shown below
1. Using Properties:If the test.ascx control has two textboxes and submit button. You can access the values of the textboxes in the control from an .aspx page by declaring public property in the .ascx page.
Public Property FirstName() As String
Get
    Return txtFirstName.Text
End Get
Set
   txtFirstName.Text = Value
End Set
End Property
In .aspx page,you can access FirstName using "TestControl.FirstName" & You can set the FirstName of the control from aspx page using TestControl.FirstName = "Suzzanne"

2. Using Javascript: You can set the values of the controls declared in the .ascx page by
Private document.forms(0)("TestControl:txtFirstName").value ="Suzzanne"
You can get the values of the controls declared in the .ascx page by
Private document.forms(0)("TestControl:txtFirstName").value

3. In ASPX code behind file:
Private objTestControl As TestControl = CType(Page.FindControl("TestControl"), TestControl)
Private objTextBox As TextBox = objTestControl.FindControl("txtFirstName")Private strFirstName As String = objTextBox.Text

Add a Scrollbar(with defined color)

.ScrollBar
{
overflow: auto;
height: 100%;
scrollbar-face-color: #c7c57b;
scrollbar-highlight-color: #c7c57b;
scrollbar-3dlight-color: #c7c5Db;
scrollbar-darkshadow-color: #c7c57b;
scrollbar-shadow-color: #00000;
scrollbar-arrow-color: #000000;
scrollbar-track-color: #F9F9DF;
}

Tuesday, October 5, 2010

AutoCompleteType = "Disabled" for textbox is not working in firefox.

Use autocomplete="off"  instead of  AutoCompleteType = "Disabled".

AutoCompleteType = "Disabled" - not works in all browsers

autocomplete="off" -  works in all browsers

Friday, July 30, 2010

Regular Expression Examples

  • IP Adress -
    Regular expression - (\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})

Friday, July 16, 2010

Add Serial No in DataControls

1. To add serial No in Repeater:

             <%#  DataBinder.Eval(Container,"ItemIndex") %>

Thursday, April 8, 2010

Populate Date DropDown using Javascript

Set the values of Dropdown list of a date:-

There are three dropdown lists for each Day, Month And Year.  we fill through javascript with following way:

        var date ="11/23/2010"; //Format- MM/DD/YYYY
        var mm = date.substring(0,2);
        var dd = date.substring(3,5);
        var yy = date.substring(6,10);
       
        document.getElementById("ddlDay").value=dd;
        document.getElementById("ddlMonth").value=mm;
        document.getElementById("ddlYear").value=yy;

Saturday, March 27, 2010

Select records from a DataTable

Select recodrs from a datatable:-


DataTable dtLog = Funtion();

DataRow[] drs = dtLog.Select(" UpdatedDate  = '" + dtDate.Rows[i]["Date"].ToString() + "'");
 DataTable dts = dtLog.Clone(); 
dts = drs.CopyToDataTable();

Tuesday, March 23, 2010

Default Button, asp.net

Default Button for a page:
                  <form id="form1" runat="server" defaultbutton="buttonName"></form>
Default Button for a panel:
                  <asp:Panel runat="server" DefaultButton="lbHello"></asp:Panel>
Default in a div:-
                  <div onkeypress="javascript:return WebForm_FireDefaultButton(event, 'buttonID')"></div>


For Linkbutton to make a default page, add below javascript at the end of the page

<script type="text/javascript">
    var __defaultFired = false;

function WebForm_FireDefaultButton(event, target) {
    var element = event.target || event.srcElement;

    if (!__defaultFired && event.keyCode == 13 && !(element && (element.tagName.toLowerCase() == "textarea"))) {
        var defaultButton;

        if (__nonMSDOMBrowser)
            defaultButton = document.getElementById(target);
        else
            defaultButton = document.all[target];

        if (defaultButton) {
            if(typeof(defaultButton.click()) != "undefined")
                defaultButton.click();
            else
                eval(unescape(defaultButton.href.replace("javascript:", "")));

            event.cancelBubble = true;

            if (event.stopPropagation) event.stopPropagation();
            return false;
        }
    }
    return true;
}
</script>