Tuesday, September 6, 2011

How to pass multiple value in command argument

<radg:GridTemplateColumn UniqueName="TemplateColumn" HeaderText="Zip Code Count" DataField="ZipCount" >

   <ItemTemplate>

     <asp:LinkButton runat="server" ID="lnkZipCount" Text='<%#Eval("ZipCount") %>' CommandArgument='<%#Eval("CityID") + ";" +Eval("Sname")%>' OnClick="lnkZipCount_Click"></asp:LinkButton>

   </ItemTemplate>

 </radg:GridTemplateColumn>



protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Test")
        {
            string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });

       Label1.Text= commandArgs[0];
       Label2.Text = commandArgs[1];

        }
    }












-------------------------------------------------------------------------------------
how to extract separate month 

string yyyy = dt.Year.ToString();

        int mon = dt.Month;
        string mont = null;
        String[] Months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
        for (int i = 0; i < Months.Length; i++)
        {
            if (i == mon - 1)
            {
                mont = Months[i].ToString();
                break;
            }
        }

        string str = mont;

***********************************************************************************



Complete Demo



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultipleCommandArgument.aspx.cs"
   Inherits="MultipleCommandArgument" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
       <div>
           <asp:GridView ID="GridView1" runat="Server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand">
               <Columns>
                   <asp:BoundField DataField="carid" HeaderText="Card Id" />
                   <asp:BoundField DataField="Year" HeaderText="year" />
                   <asp:TemplateField>
                       <ItemTemplate>
                           <asp:Button ID="btnTest" runat="Server" CommandName="Test" Text="Select" CommandArgument='<%#Eval("carid") + ","+Eval("year") %>' />
                       </ItemTemplate>
                   </asp:TemplateField>
               </Columns>
           </asp:GridView>
           <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
           <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
   </form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MultipleCommandArgument : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            GridView1.DataSource = GetData();
            GridView1.DataBind();
        }
      

    }
  
    DataTable data;
    DataTable GetData()
    {
        data = Session["data"] as DataTable;
        if (data != null)
        {
            return data;
        }
        data = new DataTable();
        DataColumn primaryColumn
        = new DataColumn("carid", typeof(Int32));
        data.Columns.Add(primaryColumn);
        data.Columns.Add(new DataColumn("year", typeof(Int32)));
        data.Columns.Add(new DataColumn("make", typeof(string)));
        data.Columns.Add(new DataColumn("model", typeof(string)));
        DataRow dr;
        dr = data.NewRow();
        dr[0] = 1;
        dr[1] = 1998;
        dr[2] = "Isuzu";
        dr[3] = "Trooper";
        data.Rows.Add(dr);
        dr = data.NewRow();
        dr[0] = 2;
        dr[1] = 2000;
        dr[2] = "Honda";
        dr[3] = "Civic";
        data.Rows.Add(dr);
        dr = data.NewRow();
        dr[0] = 3;
        dr[1] = 2000;
        dr[2] = "BMW";
        dr[3] = "GM";
        data.Rows.Add(dr);
        dr = data.NewRow();
        dr[0] = 4;
        dr[1] = 2000;
        dr[2] = "Swift";
        dr[3] = "Tata";
        data.Rows.Add(dr);
        DataColumn[] primaryColumns = new DataColumn[1];
        primaryColumns[0] = primaryColumn;
        data.PrimaryKey = primaryColumns;
        Session["data"] = data;
        return data;
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Test")
        {
            string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });

       Label1.Text= commandArgs[0];
       Label2.Text = commandArgs[1];

        }
    }
}



No comments:

Post a Comment