Question: I have a GridView In which I am dynamically creating Label and Button on gv\_RowDataBound.

.cs

```
Label Tagvalue = new Label();
Tagvalue.ID = "lblTags_"+i;
//Tagvalue.CssClass = "tagBubble tagBubbleDelete";
Tagvalue.Text = tag;

Button delete = new Button();
delete.ID = "btnDelete_" + i;
delete.ToolTip = "Delete";
delete.CssClass = "tagBubbleDelete";
delete.CommandName = "Delete";
delete.CommandArgument = imageID.Text;

```

The problem is that the "RowCommand" function is not getting fired. So I am not able to reach e.CommandName=="Delete".

What can be done to fire the RowCommand event for a Dynamically Created button inside a GridView.

Any help is Appreciated.

\*\*\*\*\*\*\*EDIT

.aspx

```
 <asp:GridView runat="server" ID="myGV" AllowSorting="false" AllowPaging="False"
                        PageSize="20" AutoGenerateColumns="False"  

                        DataSourceID="LDSgv" 
                        ondatabound="gv_DataBound"
                        OnRowDataBound="gv_RowDataBound"
                        OnRowCommand="gv_RowCommand"
                        >
  <asp:TemplateField HeaderText="Details" HeaderStyle-CssClass="noSort"
                                    ItemStyle-CssClass="fieldAlignCenter">
                                    <ItemTemplate>
                                      <div class="alignLeft marginTop5 float-l" id="Tags" runat="server"                         style="clear:both">
                                          //**Here I am adding the dynamic Buttons  
                                        </div>
                                       </ItemTemplate>
                                     </asp:TemplateField>
                                </asp:GridView>

```

Answer: 
Alternatively, you could re-implement it as an integer column and allow (-1, 0, 1) to represent (dislike, neutral, like), which in turn may simplify ranking an item's popularity. For example, to list the top 10 most liked items:

```
  select top (10)
         ItemID, sum(ToLikeOrNotLike)
    from Items
group by ItemID
order by sum(ToLikeOrNotLike) desc;

```

This takes cares of the mutual exclucivity as well.

Alternative
===========

On a suggestion from Spevy (see comments), the **0** (for no like or dislike) could be replaced with **null**, so that it's easier to determine overall activity on an item. For example:

```
  select top (10)
         ItemID, count(ToLikeOrNotLike)
    from Items
group by ItemID
order by count(ToLikeOrNotLike) desc;

```

Would return how many likes *or* dislikes an item has received. This can be useful to distinguish between an item with 2 likes and 1 dislikes, and an item with 10,000 likes and 9,999 dislikes, both of which would be otherwise indistinguishable when summed.
This is based on your first idea of a table having Boolean columns for separate actions that the user has taken regarding the item.

Use a `CHECK` constraint on the table (change the table name to whatever your table is really named):

```
ALTER TABLE UserAction 
ADD CONSTRAINT CK_LikeOrDisLike
CHECK (
    (Like is null AND Dislike is null)
    OR (Like = 1 AND Dislike is null)
    OR (Like is null AND Dislike = 1)
)

```

Or perhaps:

```
ALTER TABLE UserAction 
ADD CONSTRAINT CK_LikeOrDisLike
CHECK (
    (ISNULL(Like, 0) = 0 AND ISNULL(Dislike, 0) = 0)
    OR (Like = 1 AND ISNULL(Dislike, 0) = 0)
    OR (ISNULL(Like, 0) = 0 AND Dislike = 1)
)

```

if you want to allow `0 = false` in addition to `null = false`.

This could then further be changed to:

```
ALTER TABLE UserAction 
ADD CONSTRAINT CK_LikeOrDisLike
CHECK (
    ISNULL(Like, 0) = 0 OR ISNULL(Dislike, 0) = 0
)

```

This last one means that at least one of `Like` and `Dislike` must be either false or null.

This will of course not help you to make consistent updates from your program, you must still handle that separately. But **IF** your program tries to make an invalid insert or update, the database will reject it with an error message.