views:

79

answers:

1

I'm trying to implement a onclick event to select an item in a asp:ListViewControl.

 <ItemTemplate>
        <tr runat="server" id="MemberRow" onclick='<%#ClientScript.GetPostBackClientHyperlink(LvMembers, string.Format("Select${0}", Container.DataItemIndex)) %>'>
            <td>
                <asp:Literal runat="server" ID="LtlMembershipNumber" Text='<%#Eval("MembershipNo") %>' />
            </td>
            <td>
                <asp:Literal runat="server" ID="LtlName" Text='<%#Eval("FullName") %>' />
            </td>
            <td>
                <asp:Literal runat="server" ID="LtlCompany" Text='<%#Eval("Company") %>' />
            </td>
            <td>
                <asp:Literal runat="server" ID="LtlNotes" Text='<%#Eval("Notes") %>' />
            </td>
            <td runat="server">
                <asp:Literal runat="server" ID="LtlMobile" Text='<%#Eval("MobilePhone") %>' /><asp:LinkButton
                    runat="server" ID="lnl1" CommandName="Select" Text="test" />
            </td>
        </tr>
    </ItemTemplate>

The page loads and binds as expected. It also outputs the HTML I would expect.

<tr id="ContentPlaceHolder1_LvMembers_MemberRow_1" onclick="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LvMembers','Select$1')">
        <td>
                10000018
            </td>
        <td>
                Axel Rose
            </td>
        <td>

            </td>
        <td>

            </td>
        <td>
                <a id="ContentPlaceHolder1_LvMembers_lnl1_1" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LvMembers$ctrl1$lnl1','')">test</a>
            </td>
    </tr>

However the onclick does not fire the ListView.Command or ListView.SelectedIndexChanging event? If I add a button with CommandName="Select" it works as expected? The POST data is the same!

{_EVENTTARGET=ctl00%24ContentPlaceHolder1%24LvMembers&_EVENTARGUMENT=Select%245&__VIEWSTATE=...}

A: 

Kind of answered this myself. The ListView class does not implement the IPostBackEventHandler interface.

This means that it does not have the RaisePostBackEvents method and does not accept the EventTarget.

I've overloaded it to implement this interface.

 public class ListViewEvent : ListView, IPostBackEventHandler
    {
        public void RaisePostBackEvent(string eventArgument)
        {
            if (!eventArgument.Contains("$"))
                return;

            string[] splitEventArgument = eventArgument.Split('$');

            switch (splitEventArgument[0])
            {
                case "Select":
                    {
                        SelectItem(Convert.ToInt32(splitEventArgument[1]));
                        break;
                    }
                default:
                    {
                        break;

                    }
            }
        }
    }
iain