Sunday, July 19, 2009

Calling a function when button is clicked in datagrid

So you have a datagrid that's automatically populated by your data, including dynamically generated links.

For example, you allow people to search for books, and you want them to be able to click on a link to the books that are returned in the search...well you need to dynamically generate the links based on the books ID. www.YourSite.com/books.aspx?id=1234 etc...

Well sometimes you also need to be able to call a function to do some additional processing when the list is generated. Here's the code necessary to do this:

ASP.NET Code:
<asp:TemplateField HeaderText="Video Links" >
<ItemTemplate>
<asp:LinkButton OnCommand="lbEdit_Click" ID="lbEdit" CommandName="Edit" CommandArgument='<%# Eval("VideoID").ToString() %>)' runat="server" Text="Edit Video">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>


In the C# behind code file:
protected void lbEdit_Click(object sender, CommandEventArgs e)
{
//Pass the contextual info to the next function
ProcessInfoFunction(e.CommandArgument.ToString());
}

No comments:

Post a Comment

There was an error in this gadget