Get Values from a LinkButton

Posted on 2011-10-27
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

    Hi! First, you need to set CommandArgument ( 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
    LVL 1

    Assisted Solution

    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

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

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now