We use that in the ItemCommand of the DataCotnrol to get a reference to the control that causes the event to be fired(the button that was clicked by the user).
If the control type is button,we can use that code to get a reference to it.
If it's LinkButton, we use :
LinkButton lnkBtn=e.CommandSource as LinkButton
To understand the mean of "as" c# keyword,see this link.
dhavalshah
Member
8 Points
17 Posts
e.Commandsource???
Nov 06, 2009 10:34 AM|LINK
hey ppl,
Button change = e.CommandSource as Button;
Can you make me understand what does this mean and why do v do it??
Thanks in advance
button grid view GridViewCommandEventArgs command source
Dhaval
anas
All-Star
73649 Points
7914 Posts
Moderator
Re: e.Commandsource???
Nov 06, 2009 10:46 AM|LINK
We use that in the ItemCommand of the DataCotnrol to get a reference to the control that causes the event to be fired(the button that was clicked by the user).
If the control type is button,we can use that code to get a reference to it.
If it's LinkButton, we use :
LinkButton lnkBtn=e.CommandSource as LinkButton
To understand the mean of "as" c# keyword,see this link.
http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx
naveenj
Contributor
6164 Points
1130 Posts
Re: e.Commandsource???
Nov 06, 2009 01:24 PM|LINK
Hi dhavalshah,
An elaboration on what anas said
Lets consider a simple scenario.
We have a GridView(ID="GridView1") and among other fields two template field
1. a Button
<asp:TemplateField> <ItemTemplate> <asp:Button ID="ClickButton" runat="server" Text="Click Me" CommandName="Click" /> </ItemTemplate> </asp:TemplateField>2. a LinkButton
<asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="UrlButton" runat="server" Text="Click Me" CommandName="Url" /> </ItemTemplate> </asp:TemplateField>Now both of them can be accessed under a single command in code-behind like this.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Click") { Response.Write("Button Clicked"); } else if(e.CommandName=="Url") { Response.Write("LinkButton Clicked"); } }Here e.CommandName has the value of CommandName we assigned in ASPX
Ok?
Now, e.CommandSource.
As we saw, both LinkButton and Button fires the event RowCommand.
e.CommandSource tells us which control has called the RowCommand event
e.CommanSource is used in situations where
1. we need to change the attribute of the control which fired the RowCommand event
protected void gvwProducts_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Click") { Response.Write("Button Clicked"); //here the button is disabled Button btn = e.CommandSource as Button; btn.Enabled = false; } else if(e.CommandName=="Url") { Response.Write("Link Button Clicked"); LinkButton lbtn = e.CommandSource as LinkButton; lbtn.Text = "Clicked"; } }2. when we want to obtain rowindex of the control which fired the RowCommand
protected void gvwProducts_RowCommand(object sender, GridViewCommandEventArgs e) { GridViewRow gvr = ((Control)e.CommandSource).Parent.Parent as GridViewRow; Response.Write(gvr.RowIndex); }And
It just is a type conversion.
Hope this helps!
Naveen Jose
ASP.NET Freelancer, Consultant
Please remember to click Mark as Answer on the post that helps you
dhavalshah
Member
8 Points
17 Posts
Re: e.Commandsource???
Nov 07, 2009 04:23 AM|LINK
hey anas ..thanks for ur rply..i no as is used for type cast..but neways..thanks
Dhaval
dhavalshah
Member
8 Points
17 Posts
Re: e.Commandsource???
Nov 07, 2009 04:28 AM|LINK
Hey naveen...man thanks for the detailed explanation..its really nice...
Dhaval