Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am using ASP.net web application.
in that i have gridview.
In grid view i am using two / there CommandFields

once i click on CommandField1/CommandField2... i have to know what is that column headtext.

any one help me in this problem.
Posted
Updated 26-Jul-12 23:42pm
v2

1 solution

Hi,

Set the CommandArgument Property of the button or linkbutton as your column header text. Like:
ASP.NET
<asp:linkbutton id="lnkAddNew" runat="server" text="Add" commandname="AddNew" commandargument="MyColumnHeaderText" xmlns:asp="#unknown" />

And Handle it like:
C#
if (e.CommandName == "AddNew")
{
    lbl1.Text=e.CommandArgument.ToString();
    // Returns MyColumnHeaderText
}


Or
C#
if (e.CommandName == "AddNew")
{
    CommandField cf=sender as CommandField;
    lbl1.Text=cf.HeaderText;
    // Returns MyColumnHeaderText
}



--Amit
 
Share this answer
 
v3
Comments
Member 8715996 27-Jul-12 5:53am    
Hi Mr.Amit

no i am giving example. i am using vb

<asp:HyperLinkField HeaderText="Plan1" Text="Plan1" NavigateUrl="~/PlanPopup1.aspx" Visible="False" />

<asp:CommandField ShowSelectButton="True" HeaderText="Plan1" SelectText="Plan1" ControlStyle-Width="80px" ItemStyle-HorizontalAlign ="Center" HeaderStyle-HorizontalAlign="Center"/>


<asp:HyperLinkField HeaderText="Plan2" Text="Plan2" NavigateUrl="~/PlanPopup1.aspx" Visible="False" />

<asp:CommandField ShowSelectButton="True" HeaderText="Plan2" SelectText="Plan2" ControlStyle-Width="80px" ItemStyle-HorizontalAlign ="Center" HeaderStyle-HorizontalAlign="Center"/>

In this i have two commandFields. i.e is plan1 and plan2
once i click on plan1 column i should get the plan1 header text.
_Amy 27-Jul-12 6:01am    
Use This:

CommandField cf=(CommandField)e.CommandSource;
lbl1.Text = cf.HeaderText;
Member 8715996 27-Jul-12 6:07am    
Hi Amit

it is showing :CommandField is a type and cannot be use as an expression
_Amy 27-Jul-12 6:15am    
Try my updated answer.
Member 8715996 27-Jul-12 7:57am    
hi Amy ok i changed the code to Dim cf As CommandField = DirectCast(e.CommandSource, CommandField) txtNOB.Text = cf.HeaderText but error is showing at e.CommandSource err.msg is: CommandSource is not a member of system.Web.UI.WebControls.GridViewSelectEventArgs

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900