41306

How to make a linkbutton onclick with parameters

HTML :

<asp:LinkButton ID="lnk_productImage" runat="server" Text="select" OnClick="viewProductImage('<%#DataBinder.Eval(Container.DataItem,"Id") %>')" > </asp:LinkButton>

CodeBehind:

protected void viewProductImage(object sender, EventArgs e, int id) { //Load Product Image }

Answer1:

I see you're using a repeater, so you probably could use this code:

In your repeater template:

<asp:Repeater ID="_postsRepeater" runat="server" OnItemCommand="_postsRepeater_ItemCommand"> <ItemTemplate><asp:LinkButton ID="_postDeleteLinkButton" runat="server" CommandName="DeletePost" CommandArgument="<%# ((Post)Container.DataItem).ID %>">Delete</asp:LinkButton></ItemTemplate> </asp:Repeater>

Then handle the repeater's ItemCommand event:

protected void _postsRepeater_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName == "DeletePost") // Replace DeletePost with the name of your command { // Get the passed parameter from e.CommandArgument // e.g. if passed an int use: // int id = Convert.ToInt32(e.CommandArgument); } }

Answer2:

Use CommandArgument property of linkbutton to pass parameters.

CommandArgument property:

Gets or sets an optional argument passed to the Command event handler along with the associated command name property.

LinkButton Members

Recommend

  • Intercept selection in window.onbeforeunload dialog
  • Accessing control from content page with nested master pages
  • Textbox value not getting cleared
  • Get Repeater data with foreach
  • Polymer - dom-repeat & caching of element data in DOM tree
  • The name 'Html' does not exist in the current context
  • Autocomplete with jQuery and asp.net repeater fills ID
  • image auto rotates while reading url from file upload (when it's a big image)?
  • Type Native Wifi.Wlan + WlanReasonCode cannot be marshaled error
  • UIBarButtonItem's action is not called when in a view with a UIGestureRecognizer
  • Exception HRESULT: 0x800455BC in speech recognition in Windows phone 8
  • How to access recipient on sent messages page with mailboxer
  • Click on button in another program - FindWindow, C#
  • Laravel: Getting Session ID oddly truncates when using foreach
  • Can't delete or rename original file after resizing
  • c# open webrowser in many tab
  • Bypass multiple inheritance in Java
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • How to Cache Real-time Data?
  • Python urlparse: small issue
  • FileReader+canvas image loading problem
  • Recording logins for password protected directories
  • how to do an event when i swipe from fragment to the other
  • jquery mobile loadPage not working
  • AT Commands to Send SMS not working in Windows 8.1
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How do I configure my settings file to work with unit tests?
  • How to stop GridView from loading again when I press back button?
  • python draw pie shapes with colour filled
  • Is it possible to post an object from jquery to bottle.py?
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • How can I use threading to 'tick' a timer to be accessed by other threads?