You must Sign In to post a response.
  • Category: ASP.NET

    I want to change label text

    Hi this is jyoti. I want to change the text of label at my form by using database and edited text is shown automatically on default page . Form is made in asp.net using c#
  • #765340
    There are couple of ways to change Label Text
    1. Using Javascript
    2. Using Code Behind
    see below snippet to know how to set value to label using Javascript
    <script lanugage=javascript runat=server>
    function ontextchange()
    {
    // check if browser is IE
    if(ie)
    document.getElementById("lableid").innerHTML = "change in text or whatever";
    else
    document.all('labelid').innerHTML ="hello word"
    }
    </script>

    //code behind
    lblCheckThis.Text = "Updated code";

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765341
    Are you using Server side control or Client side contril

    if it is the client side control you can call the javascript and change it.

    function changeText()
    {
    document.getElementById("MyLabel").innerHTML = "Your Text";
    }

    If you are using the server side control. you can directly assign the value.

    MyLable.Text = "Your Text";

    By Nathan
    Direction is important than speed

  • #765342
    Hi

    try this Code in your pageload in Webapplication . if you have windows application means try to code form_load


    Datatable Dt=new Datatable();
    label1.Text=Dt.Defaultview[0][0];

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #765373
    Hi Jyoti,

    If you want to show database value in your label then first get the database result into one datatable and bind the datatable result into label control.


    DataTable dt=//get data from database;
    if(dt.Rows.Count > 0)
    {
    lbl.Text=dt.Rows[0][0].ToString();
    }

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #765416
    This can be done by writing your code in the code-behind but it can also done by javascript also.But the better way is to placing your code in code-behind.

    i. For Code-Behind :- Firstly, you have to fetch the data from the field of the DB table and then keep the resultset or record in form of datatable or dataset and then, set the appropriate value in the label.

    ii. Javascript :- i. For Code-Behind :- Firstly, you have to fetch the data from the field of the DB table and then keep the resultset or record in form of datatable or dataset and then, set the appropriate value in the hidden field and set the hidden field value to the label control.

    Try with the below code (Code-Behind) :

    SqlCommand com = new SqlCommand("sqlquey", conn) ; // here "sqlquey" = place your sql query , conn = connection object
    com.CommandType = CommandType.Text;
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = com;
    DataTable dt = new DataTable();
    da.Fill(dt);
    if(dt.Rows.Count > 0)
    {
    label1.Text=Convert.ToString(dt.Rows[0][0]);
    }

    Hope this will resolve your problem.

  • #765482
    You can do it by using latest client side scripting languages like angularjs,knockoutjs which supports two way binding..
    Refer below links for more information..
    http://learn.knockoutjs.com/
    http://www.tutorialspoint.com/angularjs/

  • #765484
    Using jQuery, we can change text value
    $('#lableid').val('AAA')

    Regards,
    Dipti Choudhari


Sign In to post your comments