访问母版页中控件的隐藏代码

发布于 2024-09-14 05:52:54 字数 1068 浏览 2 评论 0原文

我需要在一组页面上一致地显示控件。因此,我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卖梦商人 2024-09-21 05:52:54

如果您希望这两个控件立即触发该页面的更改,那么您需要将 AutoPostBack 属性设置为 true两者:

<div id="verifyInitial" runat="server">
    <asp:CheckBox ID="chkInitialVerify" runat="server"  oncheckedchanged="chkInitialVerify_CheckedChanged" AutoPostBack="true" />
    I agree that my initial data is correct.
</div>

<div id="verifyContinuous" runat="server">
    <asp:CheckBox ID="chkContinuousVerify" runat="server" oncheckedchanged="chkContinuousVerify_CheckedChanged" AutoPostBack="true" />
    I agree that my continuous data is correct
</div>

否则,您需要页面上的 或其他一些控件来触发回发并导致事件处理程序运行。该按钮或其他控件可以位于您的母版页上,也可以位于您的内容页上,选择完全取决于您。

If you're expecting the two controls to trigger a change for that page immediately then you'll need to set the AutoPostBack property to true for both of them:

<div id="verifyInitial" runat="server">
    <asp:CheckBox ID="chkInitialVerify" runat="server"  oncheckedchanged="chkInitialVerify_CheckedChanged" AutoPostBack="true" />
    I agree that my initial data is correct.
</div>

<div id="verifyContinuous" runat="server">
    <asp:CheckBox ID="chkContinuousVerify" runat="server" oncheckedchanged="chkContinuousVerify_CheckedChanged" AutoPostBack="true" />
    I agree that my continuous data is correct
</div>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文