<span id="span1" runat="server">Test_String</span>
<br><br
<%
Response.Write (span1.ID); // works: span1
Response.Write ("<br>");
Response.Write (span1.InnerHtml); // works: Test_String
Response.Write ("<br>");
Response.Write (FindControl("span1").ID); // works: span1
Response.Write ("<br>");
Response.Write (FindControl("span1").InnerHtml); // doesn't work, why??
Response.Write ("<br>");
%Hi,
FindControl returns back something of type control so you have to cast it first to the correct type.
Grz, Kris.
thx XIII
but how? any hint?
Change:
Response.Write (FindControl("span1").InnerHtml);to: Response.Write (((HtmlGenericControl)FindControl("span1")).InnerHtml);To make this a bit less cryptic, here's how it works.First, we use FindControl to actually grab the control:
FindControl("span")However, this will give us a Control. As your error states, Control does not have an InnerHtml property. So, we first cast from a Control to a HtmlGenericControl (which is what the <span> is):(HtmlGenericControl)FindControl("span")Now, we put brackets around this combination, to give us our resulting HtmlGenericControl object:((HtmlGenericControl)FindControl("span"))Now that we have an HtmlGenericControl object, we can specify its InnerHtml property:((HtmlGenericControl)FindControl("span1")).InnerHtmlI hope this helps.
hi, SomeNewKid,
thanks for yr help.
I tried yr way, but still not work :(
can you run this program ok in your machine?
> can you run this program ok in your machine?
Yes. With that single line change, your sample runs fine. Remember that C# is case-sensitive ... so you must type it exactly as shown.
When you say it still doesn't work, what exactly do you mean?
Do you get an error? If so, what does the error say?
Do you get an output that you did not expect? If so, what output were you expecting?
thanks to SomeNewKid, I got it! it runs ok now.
here is another probem, I added two 'Reponse' lines, first & second run ok, but the third line got a error. Is this 'HtmlGenericControl' not for a 'TD' tag?
<%@. Page Language="C#" %
<span id="id_span" runat="server">span_string</span>
<div id="id_div" runat="server">div_string</div
<form id="form1" runat="server">
<table id="table1" border="1" runat="server">
<tr id="id_tr" runat="server">
<td id="id_td" runat="server">td_string</td>
</tr>
</table>
</form
<%
Response.Write (((HtmlGenericControl)FindControl("id_span")).InnerHtml); // ok
Response.Write (((HtmlGenericControl)FindControl("id_div")).InnerHtml); //ok
Response.Write (((HtmlGenericControl)FindControl("id_td")).InnerHtml); // got error!
%>
> Is this 'HtmlGenericControl' not for a 'TD' tag?
Correct.
Here's a loose description of why:
The HtmlGenericControl is used for any control that is not "special" to ASP.NET.
A <span> tag is nothing special. It is just a tag sitting by itself, with no required parent or child tags. So, it's just a HtmlGenericControl.
A <td> tag, on the other hand, is not a tag sitting by itself. It must have a parent <tr> tag, and a grandparent <table> tag. This required structure makes tables "special" to ASP.NET. So, a <table> is an HtmlTable control, a <tr> is an HtmlTableRow control, and a <td> is a HtmlTableCell control.
Hence, you can get your code working by changing:
Response.Write (((HtmlGenericControl)FindControl("id_td")).InnerHtml);to: Response.Write (((HtmlTableCell)FindControl("id_td")).InnerHtml);So, how on earth can you know which tags are generic, and which are special?As always, the answer is in the documentation:
System.Web.UI.HtmlControls Namespace
oh, SomeNewKid! You are a good teacher! I love you so mucn~~~~
I think now I understood a lot about it, thank you very much!
p.s.
and I found 'HtmlContainerControl' can get work too :)
You're welcome.
0 comments:
Post a Comment