Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net DataList problem

Tags:

c#

.net

asp.net

I have declaratively created a LinqDataSource and DataList and bound them in markup. I have created an ItemTemplate and Edit Template.

I have bound the DataLists oneditcommand and oncancelcommand to methods in the code behind.

<asp:DataList ID="MyDataList" runat="server" DataSourceID="LinqDataSource1" RepeatDirection="Horizontal"
    Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
    Font-Underline="False" HorizontalAlign="Center" RepeatColumns="4" 
    oneditcommand="MyDataList_EditCommand" 
    oncancelcommand="MyDataList_CancelCommand" 
    >

<ItemTemplate>
    <div style="margin: 5px;">
        <asp:LinkButton Text="Edit" CommandName="Edit" style="float:right" runat="server" />
    // Other markup    
    </div>
</ItemTemplate>

When I click the LinkButton in the ItemTemplate, it runs the following code:

protected void DataList_EditCommand(object source, DataListCommandEventArgs e)
        {
            MyDataList.EditItemIndex = e.Item.ItemIndex;
            MyDataList.DataBind();
        }

This works fine and puts the selected item in the DataList into edit mode. The Edit Template:

    <EditItemTemplate>
        <div style="margin: 5px;">
            <asp:LinkButton Text="Cancel" style="float:right" 
                runat="server" CommandName="cancel" CausesValidation="false"/>
      //other markup
        </div>
    </EditItemTemplate>

When I click the cancel button in the edit template, It does not fire the method in the code behind (the breakpoint doesn't get hit).

The Code that should be run when cancelling never get's run, so I can not exit the edit mode back into normal read mode:

protected void MyDataList_CancelCommand(object source, DataListCommandEventArgs e)
    {
        PhotoDataList.EditItemIndex = -1;
        PhotoDataList.DataBind();
    }

Can anyone think of a reason for this?

---- UPDATE

It seems that it is just the second firing of an event on the DataList that doesn't work, as I have know bound to the ItemCommand event, and was going to intercept the DataListCommandEventArgs.CommandName property and do something based on that. If you click the Edit link button, the ItemCommand method fires (with no code body at all), but the second time you click the edit link button, the ItemCommand method does not get hit.

like image 809
Ben Avatar asked Jul 18 '26 22:07

Ben


1 Answers

The name of your DataList is : "MyDataList"

but Cancel event calls PhotoDataList!

protected void MyDataList_CancelCommand(object source, DataListCommandEventArgs e)
    {
        PhotoDataList.EditItemIndex = -1;
        PhotoDataList.DataBind();
    }
like image 182
nayung Avatar answered Jul 21 '26 12:07

nayung



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!