Saturday 6 May 2017

How to Get Read Only TextBox values in asp.net using C#


How to Get Read Only TextBox values in asp.net using C#
Description:
In this example we explain that how to get read only textbox values in asp.net using C#.or how to get read only textbox values or disabled textbox value in codebehind using asp.net.

Get value of ReadOnly TextBox in asp.net and retain its value across postback.because after postback the readonly textbox value is not get the last updated value.so to get last updated value below is the code to get read only textbox value in C#.

by simply adding one line of code to get the value of readonly and disabled textbox in asp.net.

TextBoxId.Attributes.Add("readonly", "readonly"); 


Aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="readonlyTextBox.aspx.cs"
    Inherits="readonlyTextBox" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        function calculate(_this) {

            var qty = $("#<%=txtqty.ClientID%>");
            var price = _this.value;
            $("#<%=txttotal.ClientID%>").val((parseInt(qty.val()) * parseInt(price)).toString());
        }
    </script>
    <title>how to get read only textbox values in asp.net using C</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        QTY:
        <asp:TextBox ID="txtqty" runat="server"></asp:TextBox>
        Price:
        <asp:TextBox ID="txtprice" runat="server"></asp:TextBox>
        Total:
        <asp:TextBox ID="txttotal" runat="server"></asp:TextBox>
      
        <asp:Button ID="getreadOnlyvalue" runat="server" Text="save" OnClick="getreadOnlyvalue_Click" />
         <asp:Label ID="lblmsg" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

Aspx.cs: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class readonlyTextBox : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txttotal.Attributes.Add("readonly", "readonly");
        txtprice.Attributes.Add("onblur", "calculate(this);");
        }
    protected void getreadOnlyvalue_Click(object sender, EventArgs e)
    {
        int total = Convert.ToInt32(txttotal.Text);
        lblmsg.Text = "Total is " + total.ToString();
   
    }
}


0 comments:

Post a Comment