views:

246

answers:

2

I have two buttons in a list view that adjust the position of that item, basically, moves it up or moves it down. Both buttons have the CommandName="Select" so I need to know if their ID is somewhere in the EventArgs so I can tell if the Up or the Down button was pressed.

This is my temporary sol'n, maybe it'll give you a better idea of what I mean to do.

int s;
    public void iBtnUp_Click( object sender, EventArgs e )
    {
        s = 1;
    }

    public void iBtnDown_Click( object sender, EventArgs e )
    {
        s = 0;
    }

    public void lvSteps_SelectedIndexChanged( object sender, EventArgs e )
    {
        DataKey currentDataKey = this.lvSteps.DataKeys[ lvSteps.SelectedIndex ];

        int ID      = (int)currentDataKey.Values[0];
        int Step    = (int)currentDataKey.Values[1];

        clsProcessStep newProcessStep = new clsProcessStep()
        {
            ProcessStepID   = ID,
            StepNumber      = Step
        };

        newProcessStep.swapSteps( s );
    }
+1  A: 

Could you not just use CommandParameter to distinguish between the two? I.e. CommandParameter="Up" for one button and "Down" for the other.

EDIT

Here is an example of using CommandParameter, but I see now that it might not fit in with your existing code. Maybe you can combine the two approaches into something that works.

<asp:ListView runat="server" ID="ListView1" OnItemCommand="ListView1_ItemCommand" OnSelectedIndexChanging="ListView1_SelectedIndexChanging">
            <LayoutTemplate>
                <table runat="server" id="table1" runat="server">
                    <tr runat="server" id="itemPlaceholder">
                    </tr>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr runat="server">
                    <td id="Td1" runat="server">
                        <p>
                            <asp:Label ID="Label1" Text="Item" runat="server"></asp:Label>
                            <asp:Button ID="Button1" runat="server" CommandName="Select" CommandArgument="Up"
                                Text="Up" />
                            <asp:Button ID="Button2" runat="server" CommandName="Select" CommandArgument="Down"
                                Text="Down" />
                        </p>
                    </td>
                </tr>
            </ItemTemplate>
            <SelectedItemTemplate>
                <tr id="Tr2" runat="server">
                    <td id="Td2" runat="server">
                        <p style="background-color:Red;">
                            <asp:Label ID="Label1" Text="Item" runat="server"></asp:Label>
                            <asp:Button ID="Button1" runat="server" CommandName="Select" CommandArgument="Up"
                                Text="Up" />
                            <asp:Button ID="Button2" runat="server" CommandName="Select" CommandArgument="Down"
                                Text="Down" />
                        </p>
                    </td>
                </tr>
            </SelectedItemTemplate>
        </asp:ListView>

And the code-behind:

public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ListView1.DataSource = new List<int>() { 1, 2, 3 };
                ListView1.DataBind();
            }
        }

        public void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                var isGoingUp = (e.CommandArgument.ToString() == "Up");
            }
        }

        protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
        {
            this.ListView1.SelectedIndex = e.NewSelectedIndex;


            ListView1.DataSource = new List<int>() { 1, 2, 3 };
            ListView1.DataBind();
        }
    }
Henrik Söderlund
Hmm maybe? I'm new to web development so I'm not aware of all the parameters I could set up. How would I retrieve that command parameter? I've added some source code to my OP to give you an idea of how I have it set up right now.
Justen
+1  A: 

Along with CommandName, you can specify CommandArgument on the buttons. If you apply a unique command argument to each button, then in your event handler you can determine which button was clicked.

This won't work however, if using SelectedIndexChanged event. The reason for this is that the EventArgs parameter does not include command name and argument details in it. You may need to switch to the OnItemCommand handler instead. It uses ListViewCommandEventArgs in the signature, so you can get the CommandName and CommandArgument properties out of it.

Example markup:

<asp:ListView ID="myListView" runat="server" OnItemCommand="myListView_ItemCommand" ... />

And corresponding code-behind:

protected void myListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    //e.CommandName gives you the command name specified on the button
    //e.CommandArgument gives you the command argument specified on the button
}

Here's the MSDN documentation on the ListView ItemCommand Event.

Hope this might help you a little.

KP
Does the index change before this event raises? So I can get the current selected index of where the button was pushed.
Justen
Off the top of my head, I can't remember. An easy way to find out is to debug and put a breakpoint on each handler (the item command and selected index changed). If you need to pass or 'remember' values between the two event handlers, you can easily declare a class-level variable and set it during the first event, retrieve it during the second... Also google asp.net order of events. You may get some good resources..
KP