Home > Forum > ASP.NET > Grid view events fired

Grid view events fired

by Thamil   on Oct 01, 2013   Category: ASP.NET  |  Views: 1503    |  Points: 5   |  Gold 
  Reply
Hi,

In a grid view there are four columns are there.Where first three column data's are populated from DB.And in the fourth column link button is there it was dynamically added for each row.Suppose if i click the link button nothing is happened.How to fire the event if i click the link button.Can any one advise me.
And I have attached my codings
<ItemTemplate>
<asp:LinkButton ID="CompanyID" runat="server" OnClick="lnkView_Click" CommandName="Open" Text="Open"></asp:LinkButton>
</ItemTemplate>

try
{
if (e.CommandName.Equals("Open"))
{
}
}
catch (Exception ex)
{
}

Thanks

Question posted on behalf of Ranjith Kumar.


Bookmark and Share:
 
  User Reply  | Ask a question  |   Reply 
  Re :Grid view events fired   
by Thamil
on Oct 1 2013 12:26PM
Points : 10
Gold 
Hi Ranjtih kumar,

It's simple and easy, create the rowcommand event and when you click on link button in gridview it will fire rowcommand event and do you functionality on this command as show below.

<asp:GridView ID="GVSample" runat="server" AutoGenerateColumns="False" OnRowCommand="GVSample_RowCommand">
<Columns>
<asp:BoundField DataField="DepName" HeaderText="DepName" />
<asp:TemplateField HeaderText="Userprofile">
<ItemTemplate>
<asp:LinkButton ID="CompanyID" runat="server" CommandName="Open" Text="Open"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GVSample.DataSource = GetEmpDept();
GVSample.DataBind();
}

}

protected void GVSample_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Open"))
{
//Your code goes here..
}
}

private DataTable GetEmpDept()
{
//Get Employee department
DataTable dt = new DataTable();
dt.Columns.Add("DepName");

DataRow rw1 = dt.NewRow();
rw1[0] = "IT";
dt.Rows.Add(rw1);

DataRow rw2 = dt.NewRow();
rw2[0] = "Finance";
dt.Rows.Add(rw2);

DataRow rw3 = dt.NewRow();
rw3[0] = "Security";
dt.Rows.Add(rw3);

return dt;
}

i hope this will help you.

Recent Post

Latest Posts