英文:
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;
<div class="form-group col" runat="server" id="CorpCol" visible="false">
code behind;
System.Web.UI.Control CorpCol = FindControlRecursive(this.Master, "CorpCol");
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 的可见性。上述操作的结果如下图所示:
英文:
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:
<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>
And the code for the user control:
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; }
}
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:
<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"
/>
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:
答案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, "CorpCol");
CorpCol.Style.Add("display", "none");
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:
<script>
$("#CorpCol").click(function(){
var CorpCol = document.getElementById("CorpCol").style;
if(CorpCol.display == 'none'){
CorpCol.display = 'block';
}
else{
CorpCol.display = 'none';
}
});
</script>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论