Wednesday, November 14, 2012

Find GridView SelectedRow in OnRowCommand Event

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="DUMMY_Default3" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1"%>


<!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 id="Head1" runat="server">
<title>Collapsible Panel Example</title>
<style type="text/css">
.pnlCSS{
font-weight: bold;
cursor: pointer;
border: solid 1px #c0c0c0;
width:30%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
 
<div>

<asp:GridView ID="GridView1" runat="server" DataKeyNames="AnnouncementID"
 AutoGenerateColumns="false" OnRowDataBound="GridView1_OnRowDataBound"
  OnRowCommand="GridView1_OnRowCommand">
  <Columns>
 <asp:TemplateField>
 <ItemTemplate>
  <asp:Label ID="lblItemIndex" runat="server" Text='<%#Container.DataItemIndex + 1 %>'>
 
 </asp:Label>
 </ItemTemplate>
 </asp:TemplateField>
  
   <asp:TemplateField>
  <ItemTemplate>
  <asp:Label ID="lblAnnouncementID" runat="server" Text='<%#Eval("AnnouncementID") %>' Visible="false"></asp:Label>
  <asp:LinkButton ID="lnkAnnouncementID" runat="server" Text="Edit" CommandName="Editing" CommandArgument='<%#Eval("AnnouncementID") %>'></asp:LinkButton>
  </ItemTemplate>
  </asp:TemplateField>


  <asp:TemplateField>
  <ItemTemplate>
  <asp:Label ID="lblAnnouncementType" runat="server"></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>

  <asp:TemplateField>
  <ItemTemplate>
  <asp:Label ID="lblTitle" runat="server"></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>

  <asp:TemplateField>
  <ItemTemplate>
  <asp:Label ID="lblMessage" runat="server"></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>



  </Columns>
  </asp:GridView>

  <asp:Label ID="lblRow" runat="server"></asp:Label>
</div>
</form>
</body>
</html>




Default.aspx.cs
-----------------



protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}


public void BindGrid()
{

TList objAnnouncement = //get the datas here...
GridView1.DataSource = objAnnouncement;
GridView1.DataBind();
}


protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ReadAnnouncement rowData = (ReadAnnouncement)e.Row.DataItem;
LinkButton lnkAnnouncementID = (LinkButton)e.Row.FindControl("lnkAnnouncementID");
Label lblAnnouncementType = (Label)e.Row.FindControl("lblAnnouncementType");
Label lblTitle = (Label)e.Row.FindControl("lblTitle");
Label lblMessage = (Label)e.Row.FindControl("lblMessage");

Label lblItemIndex = (Label)e.Row.FindControl("lblItemIndex");

lnkAnnouncementID.Text = Convert.ToString(rowData.AnnouncementId);
lblAnnouncementType.Text = Convert.ToString(rowData.AnnouncementType);
lblTitle.Text = Convert.ToString(rowData.Title);
lblMessage.Text = Convert.ToString(rowData.Message);
string MessageRead = Convert.ToString(rowData.MessageRead);

}
}


protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
//// here we are finding Selected Row......
GridViewRow gvRow = (GridViewRow)((Control)(e.CommandSource)).Parent.Parent;
Label lblTitle = (Label)gvRow.FindControl("lblTitle");

string URL = string.Format("~/Default4.aspx?AnnouncementID={0}", lblTitle);
Response.Redirect(URL);
BindGrid();

}

0 comments:

Post a Comment