简体   繁体   中英

CommandName not visible in source code

I have this code

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Command1">
                    <img src="../images/image1.png" alt="" />
                </asp:LinkButton>                                         
            </ItemTemplate>
        </asp:DataList>
    </ContentTemplate>
</asp:UpdatePanel>

and this code

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "Command1")
    {      
        // Never drops here
    }           
}

The event is being triggered.
But when I debug, LinkButton1's CommandName is not visible in source code.
So, the if statement doesn't work.

Any ideas ?

Edit :

I realised that I have an other error my page that belongs this situation.
Then I used GridView rather than DataList and used the GridView's RowCommand Event and fixed this.

The reason is because your attempting to obtain the CommandName from the DataList not the LinkButton . Your code would work if you did the following:

protected void btnSample_Click(object sender, EventArgs e)
{
     // Instantiate:
     var command = ((LinkButton)sender).CommandName;

     // Do additional logic.
}

I believe your intent is to actually do something on the Click Event. If that isn't your case, you would need to FindControl on an event within your DataList , then instantiate the LinkButton to obtain the CommandName . Hopefully this points you in the proper direction.

I would do a sample within the DataList , but without exact implementation and additional information I wouldn't be able to. It would be similar to the above, just allocating the Control within the current control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM