开发者

Find controls in listview item template of the same type

I am working on claim expenses application for the staff where I work. Part of the process contains a listview, part of a new requirement is that if an expense type is mileage the user will not be able to edit the item, only delete and resubmit as part of business rules and UK tax reasons etc.

Anyway, I want to be able to find a control in each item of the listview that has a certain text value.

I thought something like the following but this is not correct and I know why.

        Label ExpenseTypeLabel = (Label)Expenses.FindControl("ExpenseTypeLabel");
        string ExpenseType = (ExpenseTypeLabel.Text.ToString());

        if (ExpenseType == "Mileage")
        {
            foreach (ListViewDataItem thisItem in Expenses.Items)
            {

                ImageButton btnEdit = (ImageButton)thisItem.FindControl("btnEdit");
                btnEdit.Enabled = false;


            }
        }

The expenses are based on weekending and as the page loads it throws my excepion as It cannot bind to a particular individual control as there are many ExpenseTypeLabels associated with the expense for the current weekending (which loads first).

What I am trying to accomplish here is to find all ExpenseTypeLabels in both the item template and the alternating item template and disable the edit function of that expense item. FYI incase you're wondering the weekending is the expense, and the children are the individual e开发者_开发百科xpense items.

Could one of you lovely people please educate me on the best way to accomplish this?

Thanks

Matt


Binding order, and timing for accessing bound items, is extremely important; this is especially true when you have sub controls that have binding items also.

If you want to affect the the display for these bound controls, you can usually do it from the aspx end.

Create a link from the front end to a function on the server end, then pass it all the necessary parameters:

<asp:listview id='lstExpense'>
   ...
   <asp:button id='btnEdit' enabled='<%#= isEnabled(((Expense)Container.DataItem).ExpenseType) %>' ...
   ...
<asp:listview>

On the server end, make a public function to return that value:

public boolean IsEnabled(string ExpenseType) {
    return ('Mileage' != ExpenseType);
}

Best solution though, is to use jQuery. Not exaggerating, but you can accomplish all of that with something as simple as:

$('.rowClass').each(function() {
    if ($(this).find('.expenseTypeClass').val() == 'Mileage')) 
        $(this).find('.btnEditClass').attr('disabled','disabled');
})


use OnItemDataBound event as follows

OnItemDataBound="Expenses_ItemDataBound"

protected void Expenses_ItemDataBound(object sender, ListViewItemEventArgs e)
{

    if (e.Item.ItemType == ListViewItemType.DataItem)
    {

        Label ExpenseTypeLabel = (Label)e.Item.FindControl("ExpenseTypeLabel");

        string ExpenseType = (ExpenseTypeLabel.Text.ToString());

        if (ExpenseType == "Mileage")
        {
            // disable button
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜