Thursday, 21 July 2016

how to get disabled TextBox value in c#

how to get disabled TextBox value in c#

how to get disabled TextBox value in c#

how to get disabled TextBox value in c#
Description:

In this example we explain that how to fetch value of disabled textbox in asp.net.or how to retrieving value of disabled Asp.Net textbox. Or how to get disabled textbox value in C#.

.net cannot get value from disabled or read-only textbox control so how to get new updated value of disabled textbox in asp.net in code behind.

For example suppose I have 3 textbox price, quantity and total. The value of the total textbox is disabled because of the total is automatically calculated based on the quantity and price.so when we update the quantity or price the total will be calculated proper but when we click on save button it will retrieve or fetch always its old or previous values because of the textbox disabled.so to overcome this problem we have to add one more HiddenField that will store the updated value of the disabled textbox(total textbox value) and when user click on save button at that time we have to insert HiddenField value so it will always save the latest or updated value of the disabled textbox.

Below is the example that will demonstrate the how to retrieved or fetch the updated value of the disabled or read-only textbox and save it into the database.

DisbaledTextbox.aspx: 

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

<!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">
        function CalculateTotal() {

            var price = document.getElementById('<%= txtprice.ClientID %>').value;

            var qty = document.getElementById('<%= txtquantity.ClientID %>').value;
            if (qty != "") {
                var total = (parseInt(price) * parseInt(qty)).toString();
                document.getElementById('<%= txttotal.ClientID %>').value = total;
                document.getElementById('<%= hdntotal.ClientID %>').value = total;
            }
        }
    </script>
    <title>Retrieving value of disabled ASP.NET textbox</title>
</head>
<body>
    <form id="form1" runat="server">
    Price:
    <asp:TextBox ID="txtprice" runat="server" onblur="javascript:CalculateTotal();"></asp:TextBox>
    Qty:
    <asp:TextBox ID="txtquantity" runat="server"></asp:TextBox>
    Total:
    <asp:TextBox ID="txttotal" Enabled="false" runat="server"></asp:TextBox>
    <asp:HiddenField ID="hdntotal" runat="server" />
    <asp:Button ID="btnsubmit" runat="server" Text="Save" OnClick="btnsubmit_Click" />
    </form>
</body>
</html>

DisbaledTextbox.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 DisabledTextbox : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
     
        ClientScript.RegisterStartupScript(this.Page.GetType(), "alert", "alert('Value of Hiddenfield ="+hdntotal.Value + "');", true);
        ClientScript.RegisterStartupScript(this.Page.GetType(), "alert1", "alert('Value of Disabled TextBox =" + txttotal.Text + "');", true);
    }
}




2 comments:

  1. your blog is really good i like this article and it is every well done thanks for giving such a awesome blog i fell over whelmed by learning your blog.




    Online Reputation Management

    ReplyDelete
    Replies
    1. thanks you very much you can also view my updated post regularly.

      Delete