访问母版页中控件的隐藏代码
我需要在一组页面上一致地显示控件。因此,我使用 MasterPage 在 ASP.NET/C# 中执行此操作。但是,我还需要以编程方式访问此控件,主要提供根据是否单击控件复选框来查看/隐藏的选项。
这是母版页的源代码
<div id="verifyInitial" runat="server">
<asp:CheckBox ID="chkInitialVerify" runat="server"
oncheckedchanged="chkInitialVerify_CheckedChanged" />
I agree that my initial data is correct.
</div>
<div id="verifyContinuous" runat="server">
<asp:CheckBox ID="chkContinuousVerify" runat="server"
oncheckedchanged="chkContinuousVerify_CheckedChanged" />
I agree that my continuous data is correct
</div>
现在在后面的代码中我想执行以下操作。基本上,如果一个人单击初始 div 框的复选框,那么初始框就会消失并显示连续框。然而,每当我单击复选框时,MasterPage 背后的代码似乎都不会激活。 MasterPages 就是这样设计的吗?我一直认为除了利用母版页上的页面加载之外,您还可以添加某种控制功能。
protected void chkInitialVerify_CheckedChanged(object sender, EventArgs e)
{
verifyContinuous.Visible = true;
verifyInitial.Visible = false;
}
protected void chkContinuousVerify_CheckedChanged(object sender, EventArgs e)
{
verifyContinuous.Visible = false;
}
I need to display a control consistently across a set of pages. So I'm using a MasterPage to do that in ASP.NET/C#. However I also need to programmatically access this control, mostly provide options to view/hide depending on whether the controls checkbox is clicked.
Here is the Source for the MasterPage
<div id="verifyInitial" runat="server">
<asp:CheckBox ID="chkInitialVerify" runat="server"
oncheckedchanged="chkInitialVerify_CheckedChanged" />
I agree that my initial data is correct.
</div>
<div id="verifyContinuous" runat="server">
<asp:CheckBox ID="chkContinuousVerify" runat="server"
oncheckedchanged="chkContinuousVerify_CheckedChanged" />
I agree that my continuous data is correct
</div>
Now in the code behind I want to perform the following operations. Basically if a person clicks on the checkbox for the initial div box, then the initial box disappears and the continous box shows up. However it seems that the code behind for the MasterPage does not activate whenver I click on the checkbox. Is that just the way MasterPages are designed? I always thought you could do add some sort of control functionality beyond utilizing the Page Load on the Master Page.
protected void chkInitialVerify_CheckedChanged(object sender, EventArgs e)
{
verifyContinuous.Visible = true;
verifyInitial.Visible = false;
}
protected void chkContinuousVerify_CheckedChanged(object sender, EventArgs e)
{
verifyContinuous.Visible = false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望这两个控件立即触发该页面的更改,那么您需要将
AutoPostBack
属性设置为true
两者:否则,您需要页面上的
或其他一些控件来触发回发并导致事件处理程序运行。该按钮或其他控件可以位于您的母版页上,也可以位于您的内容页上,选择完全取决于您。If you're expecting the two controls to trigger a change for that page immediately then you'll need to set the
AutoPostBack
property totrue
for both of them:Otherwise, you need an
<asp:button />
or some other control on the page to trigger a postback and cause your event handlers to run. The button, or other control, can either be on your masterpage or on your content page, the choice is entirely yours.