如何从代码隐藏中隐藏HTML行 [英] How to hide a HTML row from code-behind

查看:82
本文介绍了如何从代码隐藏中隐藏HTML行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从代码隐藏中设置HTML行。相关代码如下:

 --- HTML部分---- 
< id = tblFormNew class = tblStyle >
...
< tr id = upload2 < span class =code-attribute> runat = server >
< td class = style_1 > < / td >
< td colspan = 3 >
< asp:FileUpload ID = FileUpload2 runat = server

< span class =code-attribute> 工具提示 = 导航到数据源 宽度 = 200px

onload = FileUpload2_Load 已启用 = False / >
< / td >
< td >
< asp:标签 ID = LabelFileName2 runat = server < span class =code-attribute>文本 = LabelFileName2 可见 = True ForeColor = #0066FF > < / asp:标签 >
< / td >
< td >
< asp:标签 ID = LabelFileName2 _ runat = server 文本 = 上传: 可见 = False ForeColor = #0066FF > < / asp:标签 >
< / td >
< / tr >
...
< / table >

----代码背后---
upload2.Visible = false; //错误:当前上下文中不存在名称upload2



此处使用的方法是指帖子隐藏代码背后的HTML行ASP.NET论坛 [ ^ ]。但是,它显示错误:不存在。我的代码出了什么问题?谢谢。



我尝试了什么:



参考http: //forums.asp.net/t/1381096.aspx?Hide+a+HTML+row+from+code+behind,以相同的方式编码但得到错误

解决方案

在ASP.NET中,当您将Visible属性设置为false时,该元素将根本不会呈现给客户端(这是一个令人困惑的功能)!

如果要隐藏它只在客户端,你应该添加一些CSS ...

 upload.Style.Add(HtmlTextWriterStyle.Display,   none); 


您可以使用页面.FindControl()在页面中获取HTML控件。尝试下面的代码:

 HtmlControl controlObj =(HtmlControl)Page.FindControl(  upload2\" ); 
if (controlObj!= null
{
controlObj。 Style.Add( display none);
}



HTML code

 <   table     id   =  tblFormNew    class   =  tblStyle >  
< tr < span class =code-attribute> id = upload2 runat = 服务器 >
< td class = style_1 > < / td >
< td colspan = 3 >
< asp:fileupload id = FileUpload2 runat = 服务器 xmlns:asp = #unknown >
ToolTip =导航到数据源Width =200px
onload =FileUpload2_LoadEnabled =False/>
< / asp:fileupload > < / td >
< / tr >
< / table >


I tried set a HTML row from code-behind. The related code is below:

       --- HTML part ----
        <table id="tblFormNew" class="tblStyle"   >
	   ...
            <tr id="upload2"  runat="server" >
                <td class="style_1"></td>
                 <td colspan="3">
                    <asp:FileUpload ID="FileUpload2" runat="server" 

                        ToolTip="Navigate to the Data Source" Width="200px" 

                        onload="FileUpload2_Load" Enabled="False" />
                </td>
                <td >
                    <asp:Label ID="LabelFileName2" runat="server" Text="LabelFileName2" Visible="True" ForeColor="#0066FF" ></asp:Label>
                </td>  
                <td >
                    <asp:Label ID="LabelFileName2_" runat="server" Text="Uploaded: " Visible="False" ForeColor="#0066FF" ></asp:Label>
                </td>        
            </tr>
	   ...
        </table>

----Code behind---
upload2.Visible = false;     // Error: the name 'upload2' does not exist in the current context


The approach used here refers to the post Hide a HTML row from code behind | The ASP.NET Forums[^]. However, it shows the error: does not exist. What's wrong with my code? Thanks.

What I have tried:

Referring to http://forums.asp.net/t/1381096.aspx?Hide+a+HTML+row+from+code+behind, coded in the same way but got error

解决方案

In ASP.NET, when you set Visible property to false, that element will not render at all to the client (it's a somehow confusing 'feature')!
If you want hide it on the client side only, you should add some CSS...

upload.Style.Add(HtmlTextWriterStyle.Display, "none");


You can use Page.FindControl() to get HTML control in a page. Try with below code:

HtmlControl controlObj = (HtmlControl)Page.FindControl("upload2");
if(controlObj != null)
{
	controlObj.Style.Add("display", "none");
}


HTML code

<table id="tblFormNew" class="tblStyle">
	<tr id="upload2"  runat="server">
		<td class="style_1"></td>
		 <td colspan="3">
			<asp:fileupload id="FileUpload2" runat="server" xmlns:asp="#unknown">
				ToolTip="Navigate to the Data Source" Width="200px" 
				onload="FileUpload2_Load" Enabled="False" />
		</asp:fileupload></td>
	</tr>
</table>


这篇关于如何从代码隐藏中隐藏HTML行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆