Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.

21 August 2012

Accessing and Changing TD text at runtime


In this article we will see how to access and change the TD text at runtime.
The TD tag belongs to table (HTML element) which will not be accessible in code behind page. So we will mark the TD tag with runat=”server” and provide an ID to access in code page. To set/get the td text we can use Inner Text property of td element.
 
In the below example we will see how to read/write text of td element.
 
Desing Page(.aspx)
<form id="form1" runat="server">
    <div>
    <h1>
        <table>
            <tr>
                <td runat="server" id="tdAccess">
                Design time td text
                </td>
            </tr>
        </table></h1>
        <asp:Label ID="lblTDText" runat="server"></asp:Label><br />
        <asp:Button ID="btnChangeTD" runat="server" Text="Change TD Text"
            onclick="btnChangeTD_Click" />
    </div>
    </form>
Code behind(.cs)
protected void Page_Load(object sender, EventArgs e)
    {
        lblTDText.Text = tdAccess.InnerText; // get td element text
    }
    protected void btnChangeTD_Click(object sender, EventArgs e)
    {
        tdAccess.InnerText = "Runtime td text from button click"; //// set td element text
    }
Explanation: we have TD tag inside table element which is marked with runat=”server” and id as tdAccess. On page load we are accessing td text (design time text) and assigning it to label control and from the button click, we are changing the td text at runtime.


0 Comments:

Post a Comment