[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Get Values from a LinkButton

Posted on 2011-10-27
Medium Priority
Last Modified: 2012-05-12
Hi all,
I am querying a database to get some addresses. If my query returns more than one address result, I would display the results and ask user to click on the one appropriate to them. I would then use the value of the ckicked link for my further queries of other database tables.

Looking at the code below, how would I get the value of the Linkbutton (link) when clicked and then use this value on the same page?

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.Odbc;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Security.Principal;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e)

    protected void Button1_Click(object sender, EventArgs e)
          //check textbox is not empty
        if (roads.Text.Length == 0)
            notext.Text="Please enter your address or postcode";

            notext.Text = "";

            //connect to the mysql db using the connection string in the web.config
            string Authority_cccConnection = ConfigurationManager.ConnectionStrings["myconnectionstring"].ToString();

            string Authority_gisConnection = ConfigurationManager.ConnectionStrings["myconnectionstring"].ToString();

            string road = roads.Text;
            string odbc_query = "SELECT DISTINCT road,schedule FROM waste_confirm WHERE road='" + road + "' OR postcode='" + road + "' ";

            OdbcConnection myConnection = new OdbcConnection(Authority_cccConnection);
            OdbcCommand myCommand = new OdbcCommand(odbc_query, myConnection);

            DataTable myDataTable = new DataTable();
            OdbcDataAdapter mySQLDataAdapter = new OdbcDataAdapter(myCommand);

            if (myDataTable.Rows.Count < 1)//if no data is returned.
                notext.Text = "No data found, please try again";

                if (myDataTable.Rows.Count > 1)
                    foreach (DataRow morethanoneaddress in myDataTable.Rows)
                        notext.Text = "More than one entry was found in our database. Please select your road from the list below:" + "<br />";

                           LinkButton link = new LinkButton();
                            link.Text = morethanoneaddress[0] + "<br />";

            GridView1.DataSource = myDataTable;


Open in new window

Question by:olootu
LVL 18

Accepted Solution

ivan_vagunin earned 500 total points
ID: 37038685
Hi! First, you need to set CommandArgument (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.commandargument.aspx) property to some value.
The you need to handle OnCommand event (not OnClick)

void LinkButton_Command(Object sender, CommandEventArgs e)
      string argument = e.CommandArgument; // this is the value you set for the button

Assisted Solution

by:Ray Phan
Ray Phan earned 500 total points
ID: 37061597
Hi, please try this.
LinkButton link = new LinkButton();
link.Text = morethanoneaddress[0] + "<br />";
link.CommandName  = "your command name";
link.CommandArgument = yourargument;//may be road id
link.OnCommand += new CommandEventHandler(link_Command);

then in link_Command you can receive your value
void link_Command(Object sender, CommandEventArgs e) {
string argValue = e.CommandArgument;
//process code with argValue here

Hope this help.

Author Closing Comment

ID: 37145620
Sorry for the late acceptance. I left that project a while ago for a new one.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month19 days, 22 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question