我建议你修改用户控制,并将“div”作为用户控制的公共财产。
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.
因此,说用户控制选择旅馆,然后是开端,最后日期。
让我们在这种用户控制中拥有“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; }
}
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,现在我们的网页。 让用户控制下降2次,然后有2个县。 隐藏(或显示)第一个用户控制区。 第2纽顿将隐藏/how第二用户控制区。
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"
/>
以及2个州的法典:
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;
}
因此,我们注意到,我们如何能够在法典背后建立美容酒店。 MyDiv.Visible = 伪造。
以上结果如下: