Step 1:- Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Get
value of ReadOnly TextBox in ASP.Net .aspx.cs" Inherits="Demo.Get_value_of_ReadOnly_TextBox_in_ASP_Net" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function SetTheValue()
{
var jNum1
= document.getElementById("<%=txtNum1.ClientID%>").value;
var jNum2
= document.getElementById("<%=txtNum2.ClientID%>").value;
var jSum
= document.getElementById("<%=txtSum.ClientID%>");
if (jNum1
== "" || isNaN(jNum1)) {
alert("Input String is Wrong.")
document.getElementById("<%=txtNum1.ClientID%>").focus();
return false;
}
if (jNum2
== "" || isNaN(jNum2)) {
alert("Input String is Wrong.")
document.getElementById("<%=txtNum2.ClientID%>").focus();
return false;
}
document.getElementById("<%=txtSum.ClientID%>").value
= parseInt(jNum1) + parseInt(jNum2);
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Enter
the Num1</td>
<td>
<asp:TextBox ID="txtNum1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Enter
the Num2</td>
<td>
<asp:TextBox ID="txtNum2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Total
(Num1 + Num2)</td>
<td>
<asp:TextBox ID="txtSum" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnCalculate" runat="server" Text="Calculate
the Sum using javascript" />
<asp:Button ID="btnGet" runat="server" Text="Get
the Value" OnClick="btnGet_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Step 2:-Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Demo
{
public partial class Get_value_of_ReadOnly_TextBox_in_ASP_Net :
System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
btnCalculate.Attributes.Add("onclick", "return
SetTheValue()");
txtSum.Attributes.Add("readonly", "readonly");
}
}
protected void btnGet_Click(object sender, EventArgs e)
{
Response.Write("Calculated Value -" +
txtSum.Text);
}
}
}
Note :-
Instead of giving ReadOnly="true” design time to the TextBox in .aspx page
<asp:TextBox ID="txtSum" runat="server" ReadOnly="true"></asp:TextBox>
<asp:TextBox ID="txtSum" runat="server" ReadOnly="true"></asp:TextBox>
Use the TextBox attribute
txtSum.Attributes.Add("readonly", "readonly");
txtSum.Attributes.Add("readonly", "readonly");
in code behind i.e. .aspx.cs page
No comments:
Post a Comment