Monday 19 November 2012

How to make TextBox only read value in Asp.Net.


Here is the solution.
If you want to TextBox has only read value then write  the simple code in Page_load  or the code where you want  the TextBox has only read value. The Code is given
Textbox_id.Attributes.Add(“readonly”,”readonly”);
Example:
Default.aspx:-
<div>

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" >
            </asp:ScriptManager>

<asp:TextBox ID="txt_Student_Date_Of_Birth" runat="server"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" runat="server"
                    TargetControlID="txt_Student_Date_Of_Birth" Format="dd-MMM-yyyy"
                    PopupPosition="Right"  DefaultView="Years">
           </asp:CalendarExtender>

</div>

Default.aspx.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
            txt_Student_Date_Of_Birth.Attributes.Add("readonly", "readonly");
           
        }
    }
}

Run this code. When you pick up the date from the calendar , textbox will take a date but it is not editable means it is read only.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.