Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more: , +
I have a gridview control that is bind to a value from database,inside the column tag I included an itemtemplate and within it an asp button control with the text property(Text ='<%#eval("somecolumnname") %>'). on the onclick attribute I want it to call another sub procedure(in my case its f) with some parameter. these parameter values should be the id of that particular row being selected.
How should I do that? below is the code:
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="act_id"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="act_id" HeaderText="act_id" ReadOnly="True" SortExpression="act_id" />
                <asp:BoundField DataField="heading" HeaderText="heading" SortExpression="heading" />
                <asp:BoundField DataField="category_id" HeaderText="category_id" SortExpression="category_id" />
                <asp:TemplateField >
                <ItemTemplate >
                <asp:button runat ="server" ID="button" Text ='<%#eval("heading") %>' OnClick ="f" > </asp:button>
                </ItemTemplate>
                </asp:TemplateField>
              </Columns>
        </asp:GridView>

Please help!!

Thank you
Posted
Updated 2-Mar-11 22:33pm
v2

1 solution

You can use OnROwCommand Event like this...

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="act_id"
            DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand">
            <Columns>
                <asp:BoundField DataField="act_id" HeaderText="act_id" ReadOnly="True" SortExpression="act_id" />
                <asp:BoundField DataField="heading" HeaderText="heading" SortExpression="heading" />
                <asp:BoundField DataField="category_id" HeaderText="category_id" SortExpression="category_id" />
                <asp:TemplateField >
                <ItemTemplate >
                <asp:button CommandName="myButton1" CommandArgument='<%# Eval("act_id") %>' runat ="server" ID="button" Text ='<%#eval("heading") %>' OnClick ="f" > </asp:button>
                </ItemTemplate>
                </asp:TemplateField>
              </Columns>
        </asp:GridView>



In the code behind..

C#
protected void GridView1_RowCommand(object sender,  GridViewCommandEventArgs e)
{
    int act_id = Convert.ToInt32(e.CommandArgument);
    if (e.CommandName == "myButton1")
    {
         string textOnTheButton=Convert.ToString(e.CommandSource);
         //your code here
    }
}


Hope this helps
 
Share this answer
 
Comments
Wanlambok 3-Mar-11 5:19am    
Thank you so much AlbinAbel!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900