Search code examples
c#asp.netdatalist

Accessing Controls in Header/FooterTemplate C#


My problem is I have a button in the FooterTemplate and HeaderTemplate (In a datalist) and I need to disable them / enable them based on a bool. I know about using the ItemDataBound, but with the way the rest of the page is setup that does not work because the only time it is bound is the initial page load.

I at one time had this working using javascript, but something happened and my javascript is no longer doing the job. So I would like to just be able to do it through the codebehind. So is there anyway to use a foreach loop to access the control?

In the events I am trying to do this I have the following loop:

 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()%>;
    }
}

I am trying to do this in a text changed event as well as in a selectedindexchanged event if that is helpful info. If any other info is requested I will do my best to provide it.

Thank you for any help you can offer.


Solution

  • Following your code:

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

    and in SetButtonStatus()

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

    If your code previously worked and has now stopped, make sure you don't have a JavaScript error, i.e. displayed data from data list is causing that problem.

    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;
        }
    }