Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 27th, 2007, 03:38 AM
Authorized User
 
Join Date: May 2007
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to swifty_programmer
Default Calling a method from Linkbutton in DataList

Hi all,

I'm trying to find the best way to call a method (defined in CodeBehind) from a linkbutton in a DataList.

An example to clear things up:

DATALIST:

ID NAME
----------------------
123 Mercedes
124 Opel
125 Chevrolet

The linkbutton is for each row the name (Linkbutton1: "Mercedes" ...)
When you click on the linkbutton Mercedes I want to do some work in my code with "123" & "Mercedes" as parameters.

this is the sourcecode:(does not work)

Code:
<asp:DataList ID="_dlCompanyList" runat="server" style="z-index: 106; left: 1px; position: absolute; top: 3px" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" ShowFooter="False" ShowHeader="False" Width="95%">
            <ItemTemplate>
                <asp:Label ID="_lBullet" runat="server" Text="•" Width="5px"></asp:Label>
                <asp:LinkButton ID="LinkButton1" runat="server" Width="100px" Text='<%# Eval("Name") %>' OnClick="CompanyClick( Eval("Id") )"></asp:LinkButton>
<asp:Label ID="_lCompanyVersion" runat="server" Width="20px" Text='<%# Eval("Version") %>'></asp:Label>
            </ItemTemplate>
            <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
            <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
            <AlternatingItemStyle BackColor="#F7F7F7" />
            <ItemStyle BackColor="#E7E7FF" Font-Size="Small" ForeColor="#4A3C8C" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
        </asp:DataList>


And this the method in the codebehind:

Code:
        public void CompanyClick(Object sender, EventArgs e, string companyId)
        {
           //do some work with companyId
        }


I read you can do something with OnCommand and CommandArgs in the Source Code but I'm realy looking for the best solution here.
thx in advance for any help.

grtz
 
Old July 28th, 2007, 05:32 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You can indeed use the CommandArgument. For example, add this LinkButton in the markup:
Code:
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("Name") %>' 
     OnClick="LinkButton1_Click" CommandArgument='<%# Eval("Id") %>' />
     This binds the company ID to the CommandArgument of the LinkButton. You can then retrieve it like this in the code behind:
Code:
protected void LinkButton1_Click(object sender, EventArgs e)
Code:
{
  LinkButton myButton = sender as LinkButton;
  if (myButton != null)
  {
    int id = Convert.ToInt32(myButton.CommandArgument);
  }
}
The sender holds a reference to the LinkButton that triggered the event handler. You can cast the object into a LinkButton and then retrieve its CommandArgument and cast that into an int.

Hope this helps,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Use LinkButton to pull record from DataList rdove84 ASP.NET 3.5 Basics 0 June 4th, 2008 08:53 AM
Calling a btn_click method dhirajDac ASP.NET 2.0 Professional 2 April 18th, 2008 07:52 AM
Calling a function on every method call Takashi321 General .NET 1 September 21st, 2007 09:33 AM
hyperlink in nested datalist calling a repeater debjanib C# 1 May 25th, 2006 08:17 AM
calling private method from another class jack_3 C# 3 December 16th, 2005 07:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.