C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET GridView »

Gridview row command event


Posted Date: 30 Jun 2009    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: ElzMember Level: Gold    
Rating: 1 out of 5Points: 3



We can redirect gridview columntext by clicking column names in two ways.



< asp:HyperLink ID="hlink" runat="server" NavigateUrl= '<%#Eval("ID","~/Firstpage.aspx?pno={0}") %>' Text='<%#Eval("NAME") %>' >
//here querystring is given in single command

< asp:LinkButton ID="lnk" runat="server" CommandName="name" CausesValidation="false"
CommandArgument='<%#Eval("[ID]") %>' Text='<%#Eval("[NAME]") %>' >
//here we have to define details in gridview_rowcommand event



Detailed code is given below


< asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="ID" >
< Columns >
< asp:TemplateField >
< ItemTemplate >

< asp:HyperLink ID="hlink" runat="server" NavigateUrl= '<%#Eval("ID","~/Firstpage.aspx?pno={0}") %>' Text='<%#Eval("NAME") %>' >
asp:HyperLink
< asp:LinkButton ID="lnk" runat="server" CommandName="name" CausesValidation="false" CommandArgument='<%#Eval("[ID]") %>' Text='<%#Eval("[NAME]") %>' >
asp:LinkButton

ItemTemplate
asp:TemplateField


< asp:CommandField ShowEditButton="True" />
< asp:BoundField DataField="PROJECT_NAME" HeaderText="Name" />
< asp:BoundField DataField="PROJECT_NUMBER" HeaderText="Number" />
< asp:BoundField DataField="Total_Tasks" HeaderText="Tasks" />

Columns
asp:GridView




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


public void bind()
{
SqlConnection con=new SqlConnection(@"server=xx;dbname=xx;user id=xx;password=xx");
SqlCommand cmd=new SqlCommand("Select * from tablename",con);
SqlDataAdpter ada=new SqlDataAdpter(cmd);
DataSet ds=new DataSet();
con.Open();
ada.Fill(ds);
con.Close();
gridView1.DataSource=ds;
gridView1.DataBind();

}



protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "number")
{
String cellval = "";
LinkButton lkbtn;
foreach (GridViewRow gvr in GridView1.Rows)
{
lkbtn = (LinkButton)gvr.FindControl("LinkButton1");
if (lkbtn.CommandArgument.ToString() == e.CommandArgument.ToString())
{
cellval = lkbtn.Text.ToString();

}
}
Response.Redirect("Firstpage.aspx");
}
}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Gridview row command  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Gridview
Previous Resource: Binding Gridview to a generic list
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use