显示/隐藏DIV时出现NullReferenceException [英] NullReferenceException while showing /hiding a DIV

查看:71
本文介绍了显示/隐藏DIV时出现NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <   div    样式  =  margin-top :2%;宽度:100%;背景:#FFBF02; >  
< ; div id < span class =code-keyword> = blocks runat = 服务器 >
< span id = snbox1 onclick = < span class =code-keyword>返回ShowClick('Company'); class = span-box1 style = position:relative; cursor:pointer; >
< span id = snplaceholderFirma < span class =code-attribute> class = image-placeholderCompany > < img id = img1 src = Images / images.png style = height: 90px; / > < / span >
< span < span class =code-attribute> id = snFirma style = background-color:#808080 class = text-content > Firma < / span >
< span style = clear:both; > < / span >
< / span >

< span id = snbox2 onclick = class = span-box2 style = < span class =code-keyword> position:relative; cursor:pointer; >
< span id = snplaceholderBenutzer class = image-placeholderUser > < img id = img2 src = 图像/图像.png style = height:90px / > < / span >
< ; span id < span class =code-keyword> = snBenutzer 样式 = background-color:#808080 class = text-content > < / span >
< span style = clear:both; > < span class =code-keyword><
/ span >
< / span >

< span id = snbox3 < span class =code-attribute> onclick = class = span-box3 style = position:relative; cursor:pointer; >
< span id = snplaceholderFirmaMitarbeiter class = image-placeholderCompanyEmployee > < img id = img3 src = 图像/图像.png style = height:90px / > < / span >
< span id = snFirmaMitarbeiter style = background-color:#808080 class = text-content > < / span >
< span 样式 = clear:both; > < / span >
< / span >
< / div >
< div id = blocksTile runat = 服务器 >
< span id = snboxs1 onclick = class = span-box1 style = position:re lative; cursor:pointer; >
< span id = snplaceholderFirmas < span class =code-attribute> class = image-placeholderCompany > < img id = imgs1 src = Images / images.png style = height: 90px; / > < / span >
< span < span class =code-attribute> id = snFirmas style = background-color:#808080 class = text-content > 测试1 < / span >
< span style = clear:both; > < / span < span class =code-keyword>>

< / span >

< span id = snboxs2 onclick = class = span -box2 style = position:relative; cursor:pointer; >
< span id = snplaceholderBenutzers class = image-placeholderUser > < img id = imgs2 src = 图像/图像.png style = height:90px / > < / span >
< ; span id < span class =code-keyword> = snBenutzers 样式 = background-color:#808080 class = text-content > 测试2 < / span >
< span style = clear:both; > < / span >
< / span >

< span id = snboxs3 onclick = class = span-box3 style = position:相对的; cursor:pointer; >
< span id = snplaceholderFirmaMitarbeiters class = image-placeholderCompanyEmployee > < img id = imgs3 src = 图像/图像.png style = height:90px / > < / span >
< span id = snFirmaMitarbeiters style = background-color:#808080 class = text-content > 测试3 < span class =code-keyword>< / span >
< span 样式 = clear:both; > < / span >
< / span >
< / div >

< / div >







这是HTML代码。基于我想要隐藏/显示div的条件。



 如果(Convert.ToInt32(GlobalValues [ 0 ])==  1 
{
blocks.Visible = true ;
blocksTile.Visible = false ;

}
else if (Convert.ToInt32(GlobalValues [ 0 ])== 2
{
blocksTile.Visible = ;
blocks.Visible = false ;
}
else
{
blocks.Visible = true ;
blocksTile.Visible = false ;
}



但结果是NULL EXCEPTION ERROR ...请帮我解决这个问题

解决方案

< blockquote>调试代码。并使用HtmlGenericControl进行HTML控制。

for ex。

 HtmlGenericControl div =(HtmlGenericControl)this.FindControl(div1); 


我发现你在另一个不是runat =Server的div里面有这个div。所以让父母也运行=服务器并尝试。



 <   div     runat   = 服务器   样式  =  margin-top:2%;宽度:100%;背景:#FFBF02 ; >  
< div id = blocks runat = 服务器 > < / div >
< / div >





希望有所帮助:)


<div style="margin-top:2%; width: 100%; background: #FFBF02;">
      <div id="blocks" runat ="server"  >
          <span id="snbox1" onclick="return ShowClick('Company');" class="span-box1"style="position: relative; cursor: pointer;">
                 <span id="snplaceholderFirma"  class="image-placeholderCompany"> <img id="img1" src="Images/images.png" style="height: 90px;" /> </span>
                 <span id="snFirma" style="background-color: #808080" class="text-content">Firma</span>
                 <span style="clear: both;"></span>
             </span>

            <span id="snbox2" onclick="" class="span-box2" style="position: relative; cursor: pointer;">
                <span id="snplaceholderBenutzer" class="image-placeholderUser"> <img id="img2" src="Images/images.png" style="height: 90px" /> </span>
                 <span id="snBenutzer" style="background-color: #808080" class="text-content"></span>
                 <span style="clear: both;"></span>
            </span>

            <span id="snbox3" onclick="" class="span-box3" style="position: relative; cursor: pointer;">
                <span id="snplaceholderFirmaMitarbeiter" class="image-placeholderCompanyEmployee"> <img id="img3" src="Images/images.png" style="height: 90px" /> </span>
                <span id="snFirmaMitarbeiter" style="background-color: #808080" class="text-content"> </span>
                <span style="clear: both;"></span>
            </span>
      </div>
    <div id="blocksTile"  runat ="server">
             <span id="snboxs1" onclick="" class="span-box1"style="position: relative; cursor: pointer;">
                 <span id="snplaceholderFirmas"  class="image-placeholderCompany"> <img id="imgs1" src="Images/images.png" style="height: 90px;" /> </span>
                 <span id="snFirmas" style="background-color: #808080" class="text-content">Test 1</span>
                 <span style="clear: both;"></span>
             </span>

            <span id="snboxs2" onclick="" class="span-box2" style="position: relative; cursor: pointer;">
                <span id="snplaceholderBenutzers" class="image-placeholderUser"> <img id="imgs2" src="Images/images.png" style="height: 90px" /> </span>
                 <span id="snBenutzers" style="background-color: #808080" class="text-content">Test 2</span>
                 <span style="clear: both;"></span>
            </span>

            <span id="snboxs3" onclick="" class="span-box3" style="position: relative; cursor: pointer;">
                <span id="snplaceholderFirmaMitarbeiters" class="image-placeholderCompanyEmployee"> <img id="imgs3" src="Images/images.png" style="height: 90px" /> </span>
                <span id="snFirmaMitarbeiters" style="background-color: #808080" class="text-content">Test 3</span>
                <span style="clear: both;"></span>
            </span>
      </div>

  </div>




This is the HTML Code. Based on a condition I want to hide/show the div.

if (Convert.ToInt32(GlobalValues[0]) == 1)
                {
                   blocks.Visible = true;
                   blocksTile.Visible = false ;

                }
                else if (Convert.ToInt32(GlobalValues[0]) == 2)
                {
                    blocksTile.Visible = true;
                    blocks.Visible = false;
                }
                else
                {
                    blocks.Visible = true;
                    blocksTile.Visible = false;
                }


But it results NULL EXCEPTION ERROR... Please help me to solve this

解决方案

debugging your code. And use HtmlGenericControl for HTML Control .
for ex.

HtmlGenericControl div = (HtmlGenericControl)this.FindControl("div1");


I found that you have this div inside another div that is not runat="Server". So make the parent also runat="server" and try.

<div runat="server" style="margin-top:2%; width: 100%; background: #FFBF02;">
      <div id="blocks" runat="server"></div>
</div>



Hope it helps :)


这篇关于显示/隐藏DIV时出现NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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