Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Pass multiple command arguments on a button click in GridView in ASP.NET

Author: Rudy Pinson
Download Source Code : 1290_MultipleCommandArguments.zip

You can pass multiple command arguments in Server Controls like Button or LinkButton and can retrieve these values by using Split() method.

 

  1. Create a new Web Site in Visual Studio 2010 either in C# or VB.NET
  2. Add a Web Form to Web Site
  3. Write code below in Default.aspx page

    <asp:GridView ID="GridView1" runat="server">
            <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" Text="Get Values" OnClick="Button1_Click" CommandArgument='<%#Eval("ProductID") + "," + Eval("ProductName")%>' />
                </ItemTemplate>
            </asp:TemplateField>
            </Columns>
    </asp:GridView>
    <br />
    <asp:Label ID="Label1" runat="server" ></asp:Label>
    <br />
    <asp:Label ID="Label2" runat="server" ></asp:Label>

     
    CommandArgument attribute has two arguments separated with “,” sign. You can pass multiple arguments and you can also use any other separator between arguments.  If you select Visual Basic then make sure that string is concatenated with “&” sign. Also, for this example, set EnableEventValidation attribute to false in “@ Page” directive.
     
  4. Write code below in code behind file. The code behind file for both Visual C# and Visual Basic will be like this:

    C#
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
     
    public partial class _Default : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            GridView1.DataSource = ProductData();
            GridView1.DataBind();
        }
     
        private DataTable ProductData() {
            SqlConnection con = new SqlConnection("Data Source=MyPC;Initial Catalog=C:\\SQL SERVER 2000 SAMPLE DATABASES\\NORTHWND.MDF;Integrated Security=True");
            con.Open();
            string sql = "SELECT * FROM Products WHERE UnitPrice = 10";
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            con.Close();
            return dt;
        }
     
        protected void Button1_Click(object sender, EventArgs e) {
            Button myButton = (Button)sender;
     
            string[] arguments = myButton.CommandArgument.ToString().Split(new char[] {','});
            string productID = arguments[0];
            string productName = arguments[1];
           
            Label1.Text = productID;
            Label2.Text = productName;
     
        }
    }
     
    VB.NET
     
    Imports System.Data
    Imports System.Data.SqlClient
     
    Partial Class _Default
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            GridView1.DataSource = ProductData()
            GridView1.DataBind()
        End Sub
     
        Private Function ProductData() As DataTable
            Dim con As New SqlConnection("Data Source=MyPC;Initial Catalog=C:\SQL SERVER 2000 SAMPLE DATABASES\NORTHWND.MDF;Integrated Security=True")
            con.Open()
            Dim sql As String = "SELECT * FROM Products WHERE UnitPrice = 10"
            Dim cmd As New SqlCommand(sql, con)
            Dim da As New SqlDataAdapter(cmd)
            Dim dt As New DataTable()
            da.Fill(dt)
            con.Close()
            Return dt
        End Function
     
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim myButton As Button = DirectCast(sender, Button)
     
            Dim arguments As String() = myButton.CommandArgument.ToString().Split(New Char() {","})
            Dim productID As String = arguments(0)
            Dim productName As String = arguments(1)
     
            Label1.Text = productID
            Label2.Text = productName
     
        End Sub
    End Class

    Import required namespaces first. Write a function to get data from database and bind GridView to this function. You can bind any data according to your requirements. I have used NORTHWIND Sample Database for this example. Write Button Click event to get Command Argument values. See, I have used a sender object to get command argument values and split these values by using Split() method. Pass the separator as parameter in Split() method.
     
  5. View the Web Site in browser and see the result. Click on Button to see command argument values.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to create HttpHandler in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net