【问题标题】:Manipulating a control in a child from the parent page从父页面操作子控件中的控件
【发布时间】:2011-06-28 15:07:44
【问题描述】:

答案可能很明显,但我没有看到任何我可以使用的东西,除了相反的——从孩子操纵父母——所以我发布了新的。

我正在开发我的公司购买的购物车系统,而且我是 .NET 的新手,所以我什至可能无法正确地提出这个问题。

我的商店在结帐部分有一个页面,基本上如下所示:

PaymentPage.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PaymentPage.ascx.cs" Inherits="ConLib_PaymentPage" %>
<%@ Register Assembly="CommerceBuilder.Web" Namespace="CommerceBuilder.Web.UI.WebControls" TagPrefix="cb" %>
<%@ Register Src="~/Checkout/PaymentForms/CreditCardPaymentForm.ascx" TagName="CreditCardPaymentForm" TagPrefix="uc" %>
<ajax:UpdatePanel ID="PaymentAjax" runat="server">
    <ContentTemplate>

            On page controls, etc.
            Then a checkbox saying they've read the terms and agree:
    <div id="terms">
        <asp:CheckBox ID="AgreeTerms" runat="server" Text="I agree to the Terms Of Service" CssClass="Terms" AutoPostBack="True" OnCheckedChanged="AgreeTerms_Clicked"/>
    </div>

我要完成的任务是在 OnCheckedChanged 事件中我要启用或禁用 CreditCardPaymentForm 控件内的图像按钮。

该代码如下所示:

<%@ Control Language="C#" ClassName="CreditCardPaymentForm" EnableViewState="false" %>
<%@ Register Assembly="CommerceBuilder.Web" Namespace="CommerceBuilder.Web.UI.WebControls" TagPrefix="cb" %>
<%@ Register assembly="wwhoverpanel" Namespace="Westwind.Web.Controls" TagPrefix="wwh" %>

... a <script> block with some code and then HTML

<span class="TTAActionButton">
    <br>
    <asp:ImageButton ID="CreditCardButton" runat="server" ToolTip="Pay With Card" SkinID="TTAPlaceOrder" OnClick="CreditCardButton_Click" />
    <asp:HiddenField runat="server"  ID="FormIsSubmitted" value="0" />
    <br /><br /><br />
</span>

我希望能够根据是否选中复选框来打开和关闭 CreditCardButton。我会在页面代码隐藏中有一个例程,例如:

public void AgreeTerms_Clicked(object sender, EventArgs e)
{
    if (AgreeTerms.Checked)
        CreditCardButton.Enabled = false;
}

但是我尝试的每一个排列都失败了。

我已经缩写了代码,但如果我遗漏了太多,请告诉我。

感谢您的帮助,如果您认为我对 .NET 一无所知,那么非常感谢! 吉姆

【问题讨论】:

  • “失败”是指它不会编译/抛出关于未知控件 id 的错误,还是不能按您期望的方式工作(即控件仍然可见)?此外,“CreditCardButton”是否与“同意”按钮位于同一个 UpdatePanel 中?
  • 由于我不理解编译错误。它们位于不同的面板中 - CCButton 是一整套链接到同意按钮所在的主页的控件。

标签: c# asp.net


【解决方案1】:

在 CreditCardPaymentForm 控件中创建一个公开 CreditCardButton 可见性的属性,然后在 OnCheckedChanged 函数处理程序中使用父级的可见性。

public bool ShowCreditCardButton
{
   get { return CreditCardButton.Visable; }
   set { CreditCardButton.Visable = value; }
}

【讨论】:

  • 好的,我知道了,但我在 OnCheckedChanged 函数中使用什么语法?像 ShowCreditCardButton = false;说“名称'ShowCreditCardButton'在当前上下文中不存在”所以我认为我必须以某种方式限定它,但我不确定如何。
  • 先不要子控件的名字,比如:MyCreditCardPaymentFormControl.ShowCreditCardButton = false;
【解决方案2】:

CreditCardPaymentForm 是否包含 PaymentPage.ascx?也就是说,表单是否“使用”了 PaymentPage 用户控件?

如果是这样,最好的办法是在 PaymentPage 上发布一个通过选中复选框引发的事件:

public void AgreeTerms_Clicked(object sender, EventArgs e)
{
    if (AgreeTerms.Checked)
        OnTermsAgreed();
}

private void OnTermsAgreed()
{
    // raise TermsAgreed event
    var evt = TermsAgreed;
    if (null != evt)
        evt(this, EventArgs.Empty);
}
public event EventHandler TermsAgreed;

然后 CreditCardPaymentForm 订阅该事件并“做它的事情”,在你的情况下是启用它知道的按钮。

【讨论】:

  • 不,CCPaymentForm 中没有使用/使用声明。我相信这是 PaymentPage 中所谓的 UserControl,至少在物理上是这样。学习 .NET 有点难,不是因为代码,而是因为术语 :)
猜你喜欢
  • 2015-04-30
  • 1970-01-01
  • 2012-03-24
  • 2011-06-29
  • 1970-01-01
  • 2011-05-04
  • 2013-11-09
  • 2017-02-04
  • 1970-01-01
相关资源
最近更新 更多