Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Passing variables from Command Argument in ASP.net

  Asked By: Craig    Date: Jun 11    Category: Asp.net    Views: 5774

How to pass a variable from the linkbutton's command argument.I am
able to send a constant value thro' command argument but not able to
pass a variable . Please give some example



1 Answer Found

Answer #1    Answered By: Kay Rodriguez     Answered On: Jun 11

Here's a sample that demonstrates everything I know about the LinkButton:


<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<script runat="server">
Dim OutputString as String
Dim ArgumentString1 as String = "My Value 1" 'Value for first
LinkButton CommandArgument
Dim ArgumentString2 as String = "My Value 2" 'Value for second
LinkButton CommandArgument

Sub Page_Load(sender as Object, e as EventArgs)
'set CommandArgument for LinkButton1
LinkButton1.CommandArgument = ArgumentString1

'LinkButton2 is created programmatically.
Dim LinkButton2 as New LinkButton()
LinkButton2.ID = "LinkButton2"
LinkButton2.Text = "Link Button 2"
LinkButton2.CommandName = "MyCommand2"
LinkButton2.CommandArgument = ArgumentString2 'set CommandArgument
for LinkButton2
AddHandler LinkButton2.Click, AddressOf OnClickMethod
AddHandler LinkButton2.Command, AddressOf OnCommandMethod
End Sub

Sub OnClickMethod(sender as Object, e as EventArgs)
OutputString += "Click Event:<br>"
Select sender.ID
Case = "LinkButton1"
OutputString += "LinkButton1 Clicked<br>"
Case = "LinkButton2"
OutputString += "LinkButton2 Clicked<br>"
Case Else
OutputString += "Error.<br>"
End Select
OutputString += "<br>"
End Sub

Sub OnCommandMethod(sender as Object, e as CommandEventArgs)
OutputString += "Command Event:<br>"
OutputString += "CommandName= " & e.CommandName & "<br>" &
"CommandArgument= " & e.CommandArgument & "<br>"
OutputString += "<br>"
End Sub

Sub Page_PreRender(sender as Object, e as EventArgs)
LabelResults.Text = OutputString
End Sub

<title>LinkButton Sample</title>


<form runat="server">
<asp:LinkButton id="LinkButton1"
Text = "Link Button 1"
<asp:placeholder ID="PlaceholderLinkButton2"
<asp:label ID="LabelResults" runat="server"></asp:label>


Didn't find what you were looking for? Find more on Passing variables from Command Argument in ASP.net Or get search suggestion and latest updates.