【问题标题】:Accessing Controls in Header/FooterTemplate C#访问页眉/页脚模板 C# 中的控件
【发布时间】:2017-05-20 01:46:25
【问题描述】:

我的问题是我在 FooterTemplate 和 HeaderTemplate 中有一个按钮(在数据列表中),我需要根据布尔值禁用/启用它们。我知道使用 ItemDataBound,但是页面其余部分的设置方式不起作用,因为它绑定的唯一时间是初始页面加载。

我曾经使用 javascript 进行这项工作,但发生了一些事情,我的 javascript 不再起作用。所以我希望能够通过代码隐藏来做到这一点。那么无论如何要使用 foreach 循环来访问控件吗?

在我尝试这样做的事件中,我有以下循环:

 foreach (DataListItem dli in list.Items)
 {
    int qty = Convert.ToInt32(((TextBox)dli.FindControl("qtyTextBox")).Text);
    int productID = Convert.ToInt32(((Literal)dli.FindControl("prodId")).Text);

    isQtyValid = COMMONACES.GetValues.getTotalQty(sessionID, productID, qty);

    lblError.Visible = !isQtyValid;
    lblError.Text = isQtyValid ? string.Empty : "The total quantity for one or more items exceeds the maximum. The total quantity includes items already in the cart.";

    Page.ClientScript.RegisterStartupScript(this.GetType(), "function", "SetButtonStatus()", true); //used to access javascript
}

Javascript:

function SetButtonStatus() {
    var bb = document.getElementsByClassName('ButtonSubmit');
    for (var i = 0; i < bb.length; i++) {
        bb[i].disabled = <%=(!isQtyValid).ToString().ToLower()%>;
    }
}

如果有用的信息,我会尝试在文本更改事件以及 selectedindexchanged 事件中执行此操作。如果需要任何其他信息,我会尽力提供。

感谢您提供的任何帮助。

【问题讨论】:

    标签: c# asp.net datalist


    【解决方案1】:

    按照您的代码:

     Page.ClientScript.RegisterStartupScript(this.GetType(), "function", "SetButtonStatus();", true); //used to access javascript
    

    并在 SetButtonStatus() 中

    document.getElementById('<%= ControlButton.ClientID %>').disabled = true/false;
    

    如果您的代码以前可以运行,但现在已停止,请确保您没有 JavaScript 错误,即数据列表中显示的数据导致了该问题。

    bool isNotValid= false;
    
    foreach (DataListItem dli in list.Items)
    {
        int qty = Convert.ToInt32(((TextBox)dli.FindControl("qtyTextBox")).Text);
        int productID = Convert.ToInt32(((Literal)dli.FindControl("prodId")).Text);
    
        isQtyValid = COMMONACES.GetValues.getTotalQty(sessionID, productID, qty);
    
        lblError.Visible = !isQtyValid;
        lblError.Text = isQtyValid ? string.Empty : "The total quantity for one or more items exceeds the maximum. The total quantity includes items already in the cart.";
        if(!isQtyValid)
            isNotValid=true;
    }
    
    Page.ClientScript.RegisterStartupScript(this.GetType(), "function", string.Format("SetButtonStatus('{0}')", isNotValid.ToString().ToLower()) , true); //used to access javascript
    
    function SetButtonStatus(isNotValid) {
        var bb = document.getElementsByClassName('ButtonSubmit');
        for (var i = 0; i < bb.length; i++) {
            bb[i].disabled = isNotValid;
        }
    }
    

    【讨论】:

    • 我实际上使用 getElementsByClassName('ButtonStatus') 因为有两个按钮,然后执行一个获取两个按钮的循环,将其编辑到我的问题中以便您可以看到它
    • 似乎可以工作,除非一旦禁用它就不会启用。我对变量发出了警报,以查看它是否正确返回并且 bool 是否正常工作,但按钮仅禁用
    猜你喜欢
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多