starvator
starvator

Reputation: 1017

InnerHtml in c#

I have <td id="StatusPreview" runat="server"></td> and it gets populated by a js function by:

document.getElementById('StatusPreview').innerHTML = Ext.getCmp('TYComboEdit').getRawValue();

Now, I would like to change the content of the td in c# when a button is clicked.

I created the following method:

    protected void hiddentoggletofrenchBackend(object sender, DirectEventArgs e)
    {
        this.StatusPreview.InnerHtml = "aaaaa";
    }

It does not change the content on the td. However, if I place an alert after setting the InnerHtml it alerts aaaaa even though the td content has not changed to reflect this. If I place an alert before setting the InnerHtml the alert is blank.

How can I change the InnerHtml of the div?

Thank you!

UPDATE:

If I change the html to <td id="StatusPreview" runat="server">q</td> the alert shows q if it is placed before setting InnerHtml, and switches to aaaaa if placed after. It is as if InnerHtml is taking the value on pageload, not the current value.

Upvotes: 1

Views: 18864

Answers (2)

Daniil Veriga
Daniil Veriga

Reputation: 1851

To update an ASP.NET control during a DirectEvent, you should call the .Update() method.

protected void hiddentoggletofrenchBackend(object sender, DirectEventArgs e)
{
    this.StatusPreview.InnerHtml = "aaaaa";
    this.StatusPreview.Update();
}

Upvotes: 3

Stephen Kennedy
Stephen Kennedy

Reputation: 21548

Adding runat=server to a td element turns it into a HtmlTableCell control. The relevant property to set the inner text on this control is InnerText.

As this is a server side control, any change is only going to happen after postback to the server. That would mean the entire page is reloaded and re-rendered. You can examine requests to the server and the server responses with the free tool Fiddler. Assuming a postback is actually happening, are you sure you're not overwriting the new inner text with JavaScript which runs on page load?

Do you even need to do a postback for this? If "aaaaa" is not a placeholder for what will become a database or some other lookup, I would render the alternate text into a hidden div or into some JavaScript and do the text change entirely in JavaScript.

Upvotes: 1

Related Questions