如何从代码后台隐藏ascx中的div。

huangapple go评论82阅读模式
英文:

How to hide div of ascx from the code behind

问题

我正在尝试隐藏一个在 ascx 中编写的 div。我是在 aspx 页面的代码后台中隐藏它的,这个 ascx 已经被注册。

div 代码:

<div class="form-group col" runat="server" id="CorpCol" visible="false">

后台代码:

System.Web.UI.Control CorpCol = FindControlRecursive(this.Master, "CorpCol");
CorpCol.Visible = false;
英文:

I am trying to hide a div which written in a ascx. I am hiding it from code behind of aspx page where this ascx is registered.<br>

div code;

&lt;div class=&quot;form-group col&quot; runat=&quot;server&quot; id=&quot;CorpCol&quot; visible=&quot;false&quot;&gt;

code behind;

System.Web.UI.Control CorpCol = FindControlRecursive(this.Master, &quot;CorpCol&quot;);
CorpCol.Visible = false; 

答案1

得分: 1

我建议您修改用户控件,并将该 "div" 公开为用户控件的公共属性。

这样做不仅使您的代码变得非常简单,而且当您在页面上放置多个该用户控件的实例时,每个 "div" 实例的隐藏/显示变得非常清晰和易于编写。

例如,假设有一个用于选择酒店、开始日期和结束日期的用户控件。

我们在用户控件中有一个名为 "div" 的元素,我们想要隐藏(或显示)它。

那么,我们简单的用户控件标记如下:

<div style="float:left">
    <h3>Select Hotel</h3>
    <asp:DropDownList ID="cboHotel" runat="server" Width="200px" DataValueField="ID" DataTextField="HotelName" AutoPostBack="true">
    </asp:DropDownList>
</div>

<div style="float:left;margin-left:30px;height:100px">
    <h3>Select Start Date</h3>
    <asp:TextBox ID="txtStartDate" runat="server" TextMode="Date">
    </asp:TextBox>
</div>

<div style="float:left;margin-left:30px">
    <h3>Select Start Date</h3>
    <asp:TextBox ID="txtEndDate" runat="server" TextMode="Date">
    </asp:TextBox>
</div>

<div id="mydiv" runat="server" style="float:left;margin-left:30px">
    <h3>My div area</h3>
    <img src='<%= ResolveClientUrl("~/Content/Rhotel.jpg") %>' width="40" height="40" />
</div>

用户控件的代码如下:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        cboHotel.DataSource = General.MyRst("SELECT ID, HotelName FROM tblhotelsA ORDER BY HotelName");
        cboHotel.DataBind();
        cboHotel.Items.Insert(0, "Select Hotel");
    }
}

public HtmlGenericControl MyDiv
{
    get { return mydiv; }
}

请注意,我们只是将 "div" 公开为公共属性。由于它是一个对象,因此不需要 "setter",只需要上面的 "get"。同时注意 "id" 和 runat="server" 标记用于该 div。

现在,让我们在网页中放置该用户控件两次,并添加两个按钮。一个按钮用于隐藏(或显示)第一个用户控件的 div,第二个按钮用于隐藏/显示第二个用户控件的 div。

以下是页面标记:

<h3>First user control</h3>
<uc1:USelectHotel runat="server" ID="USelectHotel" />

<div style="clear:both"></div>
<hr style="border:solid 2px" />

<h3>Second user control</h3>
<uc1:USelectHotel runat="server" ID="USelectHotel1" />

<div style="clear:both"></div>
<hr style="border:solid 2px" />

<asp:Button ID="cmdDiv1" runat="server" Text="Hide show div 1" CssClass="btn btn-info" OnClick="cmdDiv1_Click" />

<asp:Button ID="cmdDiv2" runat="server" Text="Hide show div 2" CssClass="btn btn-info" style="margin-left:35px" OnClick="cmdDiv2_Click" />

以下是两个按钮的代码后台:

protected void cmdDiv1_Click(object sender, EventArgs e)
{
    USelectHotel.MyDiv.Visible = !USelectHotel.MyDiv.Visible;
}

protected void cmdDiv2_Click(object sender, EventArgs e)
{
    USelectHotel1.MyDiv.Visible = !USelectHotel1.MyDiv.Visible;
}

请注意,我们可以在后台代码中使用 USelectHotel1.MyDiv.Visible = false 来设置 div 的可见性。上述操作的结果如下图所示:

如何从代码后台隐藏ascx中的div。

英文:

I suggest you modify the user control, and expose that "div" as a public property of the user control.

Not only then does your code become rather simple, but you also find when you drop multiple instances of that user control on a page, then the hide/show of each "instance" of that div becomes nice clean and easy code.

So, say a user control to select a hotel, then a start, and end date.

And let’s have "div" in that user control that we want to hide (or show).

So, our simple user control markup is this:

&lt;div style=&quot;float:left&quot;&gt;

    &lt;h3&gt;Select Hotel&lt;/h3&gt;
    &lt;asp:DropDownList ID=&quot;cboHotel&quot; runat=&quot;server&quot; 
        Width=&quot;200px&quot;
        DataValueField=&quot;ID&quot;
        DataTextField=&quot;HotelName&quot; 
        AutoPostBack=&quot;true&quot; &gt;
    &lt;/asp:DropDownList&gt;

&lt;/div&gt;

&lt;div style=&quot;float:left;margin-left:30px;height:100px&quot;&gt;
    &lt;h3&gt;Select Start Date&lt;/h3&gt;
    &lt;asp:TextBox ID=&quot;txtStartDate&quot; runat=&quot;server&quot; TextMode=&quot;Date&quot;&gt;
    &lt;/asp:TextBox&gt;
&lt;/div&gt;

&lt;div style=&quot;float:left;margin-left:30px&quot;&gt;
    &lt;h3&gt;Select Start Date&lt;/h3&gt;
    &lt;asp:TextBox ID=&quot;txtEndDate&quot; runat=&quot;server&quot; TextMode=&quot;Date&quot;&gt;
    &lt;/asp:TextBox&gt;
&lt;/div&gt;

&lt;div id=&quot;mydiv&quot; runat=&quot;server&quot; style=&quot;float:left;margin-left:30px&quot;&gt;
    &lt;h3&gt;My div area&lt;/h3&gt;
    &lt;img src=&#39;&lt;%= ResolveClientUrl(@&quot;~/Content/Rhotel.jpg&quot;) %&gt;&#39; 
        width=&quot;40&quot; height=&quot;40&quot; /&gt;
&lt;/div&gt;

And the code for the user control:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            cboHotel.DataSource =
                General.MyRst(&quot;SELECT ID, HotelName FROM tblhotelsA ORDER BY HotelName&quot;);
            cboHotel.DataBind();
            cboHotel.Items.Insert(0, &quot;Select Hotel&quot;);
        }
    }

    public HtmlGenericControl MyDiv
    {
        get { return mydiv; }
    }

Note how we just expose the "div" as a public property. And since it is an object, then no "setter" is required, only the above "get". And note the “id" and runat="server" tag for that div.

Ok, now our web page. Let’s drop in the user control 2 times, and then have 2 buttons. One to hide (or show) the first user control div. The 2nd button will hide/show the 2nd user control div.

So, this markup:

        &lt;h3&gt;First user control&lt;/h3&gt;
        &lt;uc1:USelectHotel runat=&quot;server&quot; ID=&quot;USelectHotel&quot; /&gt;

        &lt;div style=&quot;clear:both&quot;&gt;&lt;/div&gt;
        &lt;hr style=&quot;border:solid 2px&quot; /&gt;

        &lt;h3&gt;Second user control&lt;/h3&gt;
        &lt;uc1:USelectHotel runat=&quot;server&quot; ID=&quot;USelectHotel1&quot; /&gt;

                    &lt;div style=&quot;clear:both&quot;&gt;&lt;/div&gt;
        &lt;hr style=&quot;border:solid 2px&quot; /&gt;

        &lt;asp:Button ID=&quot;cmdDiv1&quot; runat=&quot;server&quot; 
            Text=&quot;Hide show div 1&quot; CssClass=&quot;btn btn-info&quot; OnClick=&quot;cmdDiv1_Click&quot;  /&gt;

        &lt;asp:Button ID=&quot;cmdDiv2&quot; runat=&quot;server&quot; 
            Text=&quot;Hide show div 2&quot; CssClass=&quot;btn btn-info&quot;
            style=&quot;margin-left:35px&quot; OnClick=&quot;cmdDiv2_Click&quot;
            /&gt;

And the code behind for the 2 buttons:

    protected void cmdDiv1_Click(object sender, EventArgs e)
    {
        USelectHotel.MyDiv.Visible = !USelectHotel.MyDiv.Visible;
    }

    protected void cmdDiv2_Click(object sender, EventArgs e)
    {
        USelectHotel1.MyDiv.Visible = !USelectHotel1.MyDiv.Visible;
    }

So note how we can in code behind set USelecthotel1.MyDiv.Visible = false in code.

The result of above looks like this:

如何从代码后台隐藏ascx中的div。

答案2

得分: 0

你必须在代码后端更改样式:

System.Web.UI.Control CorpCol = FindControlRecursive(this.Master, "CorpCol");
CorpCol.Style.Add("display", "none");

但最简单的方法是使用 JavaScript。
您可以将 jQuery 添加到项目中,并将以下代码放在页面底部:

<script>
$("#CorpCol").click(function(){
 var CorpCol =  document.getElementById("CorpCol").style;
 if(CorpCol.display == 'none'){
    CorpCol.display = 'block';
 }
 else{
    CorpCol.display = 'none';
 }
});
</script>
英文:

you must chaning style in code behind:

System.Web.UI.Control CorpCol = FindControlRecursive(this.Master, &quot;CorpCol&quot;);
CorpCol.Style.Add(&quot;display&quot;, &quot;none&quot;);

But the easiest way is to use JavaScript.
You can add jQuery to project and put the following code at the bottom of your page:

&lt;script&gt;
$(&quot;#CorpCol&quot;).click(function(){
 var CorpCol =  document.getElementById(&quot;CorpCol&quot;).style;
 if(CorpCol.display == &#39;none&#39;){
	CorpCol.display = &#39;block&#39;;
 }
 else{
	CorpCol.display = &#39;none&#39;;
 }
});
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年7月27日 18:34:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定