Using two asp:ButtonColumns in one row of datagrid

来源:http://www.imcoder.org/starter/198821.htm

 

Q:

Hi All

I am having trouble distinguishing between two "buttons"/"links" in my datagrid. It seems to bring back the same information to the ItemCommand. Is there any other way to do this? (having two linked columns on every line of a datagrid). this is a demonstation:

<asp:BoundColumn Visible="False" DataField="orders_orderno" SortExpression="orders_orderno"></asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="orders_itemno" SortExpression="orders_itemno"></asp:ButtonColumn>

<asp:ButtonColumn Text="orders_orderno" DataTextField="orders_orderno" HeaderText="Order No" CommandName="orders_orderno"></asp:ButtonColumn>
<asp:ButtonColumn Text="orders_itemno" DataTextField="orders_itemno" HeaderText="Item No" CommandName="orders_itemno"></asp:ButtonColumn>

<asp:BoundColumn DataField="orders_account" SortExpression="orders_account" HeaderText="Account"></asp:BoundColumn>

A:

Instead of Bound Field try to use TemplateField

 

                            <asp:TemplateField>                                <ItemTemplate>                                    <asp:Button ID="btnClick" runat="server" Text="Click" />                                </ItemTemplate>                            </asp:TemplateField>
  
A:

when you use the buttoncolumn, i believe it fires the rowcommand event, so what you can do is hook onto the rowcommand event of the gridview, and you can inspect the GridViewCommandEventArgs event arguments as it has a property called CommandName which you can use to distinguish which button click was raised.

A:

 hi your can add two button in itemtemplate and handle its click event on code side

 <asp:TemplateField>
<ItemTemplate>
                                    <asp:Button ID="btnClick" runat="server" Text="Click" onclick="button1_click" />
<asp:Button ID="btnClick2" runat="server" Text="Click" onclick="button2_click" />
</ItemTemplate>
</asp:TemplateField>

 

A:

Thanks for the quick response, but this is not how I want to do things.(Dont want to display buttons in my grid - I want a link from the value being displayed.)

eg. orderno01  |  item001 | account002 | ...    

A:

You can use HyperLink field to give link

 <asp:HyperLink id="hypLink1" runat="server" NavigationUrl="Where U Want To Transfer" Text='<%# Eval("DataBaseFiledName")%>' />

  
A:

Found it! thanks to the help of jimmy
You can check this by simply using the e.CommandName in the DataGridCommandEventHandler. easy.

Thanks guys!

posted @ 2008-08-20 13:40  KK in cnblog  阅读(204)  评论(0编辑  收藏  举报