开发者

Adding LinkButtons to DataGrid rows in ItemDataBound

开发者 https://www.devze.com 2023-03-06 04:55 出处:网络
I have a fairly standard DataGrid. It contains a few BoundColumns. I\'m overloading one of these columns to contain either text or a LinkButton depending on some characteristics of the bound item.

I have a fairly standard DataGrid. It contains a few BoundColumns. I'm overloading one of these columns to contain either text or a LinkButton depending on some characteristics of the bound item.

So.. in the ItemDataBound event for the Grid, I check a few things and add either a Label Control, or LinkButton Control to the proper Cell.

If I click o开发者_StackOverflown one of these LinkButtons, a postback occurs, but it does not call the method in the event handler of the button.

I have seen some people say that events will not work if you create controls AFTER the PreInit page event. Is that true? If so, there must be a way I can manually wire these up?

I have tried creating all of the buttons in PreInit and only adding them in ItemDataBound which unsurprisingly did not work.

I have heard others say the buttons and each parent control must have a unique ID. Is that true?

Rather than blindly swing away at this, I'd like to understand exactly why I can't do this.

Thanks


As far as I understand it, if you put a link button inside a DataGrid you need to use the RowCommand, i.e.

<asp:LinkButton ID="btnUpdate" CommandName="Something" CommandArgument='<%# Bind("something") %>' runat="server">Update</asp:LinkButton>

Then you need to bind the OnRowCommand event to a function in your DataGrid. This will pass the Command Argument that was given to link button and the name of the command in an Event Argument. You can use this to re-act appropriately to the event.

0

精彩评论

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

关注公众号