I am supporting a really old legacy application that is about to be taken down which is written in aspx. I have a <div>
element inside a page (Example.aspx) that I am trying to make visible in the code behind (Example.aspx.cs) based on a checkbox checked value. However I keep getting this Cannot get inner content of '' because the contents are not literal
error and thus my div is not being rendered. I referred to this SO post: Cannot get inner content of '' because the contents are not literal which mentions that this will not work on controls that are run on the server i.e runat="server"
. I cannot access the WordPress url referenced in the one answer from this SO post that was suggested: Cannot get inner HTML – and this one (Cannot manage to change the inner html of a div element) is not applicable to my question.
Example.aspx:
<div id="testID" runat="server"> <!--some content ---></div>
Example.aspx.cs:
protected void checkBoxName_CheckedChanged(object sender, EventArgs e)
{
if(!checkBoxName.Checked)
testID.Visible = true; //The exception is thrown here
}
2
Answers
You haven’t shown the code for the
CheckBox
, but considering that you have an event handler for it, I assume you have something like the following:in which case, the following should work:
This has been tested using the following:
Create a new project –
ASP .NET Web Application (.NET Framework)
Open Solution Explorer
Add Web form
default.aspx
default.aspx.cs:
Resources:
Additional Resources:
Ok, this looks to be as simple as possible.
However, DO keep in mind that HTML rendering does NOT occur when you set a control visible = false.
In other words, you set visible = false, then other code (in most cases client side) will not then see this "div" or whatever – since it not at all rendered.
I don’t know what other code is involved here, but I would test/try/consider using style to hide, and thus the HTML is still there – but does not show.
so, try this:
and to display, then
However, perhaps some udpate panel is involved here – and that will change how this works. If the button/checkbox is say inside of a update panel, and the div is not? Then this will not work.
However, if the button/checkbox is outside of the update panel, then it should work ok. So, if update panels are being used, this could effect behavours.