You are on page 1of 5

Asp.

net C# Gridview linkbutton click event

1 of 5

http://www.dotnetspider.com/forum/279821-Asp-net-C-Gridview-linkbut...

+364 Recommend this on Google

Tutorials

Forum

Resources

Reviews

Jobs

Interview

Videos

Silverlight Games | Bookmarks | Mentor | Code Converter | IT Companies | Peer Appraisal | Members | Polls | Revenue Sharing

Looking for Max Asp? Find Max Asp on Facebook. Sign Up Free Now! www.Facebook.com
Need to translate? PROMT Standard 9.0 - an easy-to-use translation software.Try for free! promt.com
Web Designing Courses Find Top Web Designing Courses & Instiutes in India @ Shiksha.com www.Shiksha.com/Web-Designing
DotNetSpider

Forums .NET ASP.NET

on Facebook
Like
2,327 people like
DotNetSpider.

Shweta

Sharath

Yogesh

Javaid

Abhishek
Shishir
Facebook social plugin

Online Members
Daby Schuurmans
More...

05-Apr-12 09:45 PM

Asp.net C# Gridview linkbutton click event

2 of 5

http://www.dotnetspider.com/forum/279821-Asp-net-C-Gridview-linkbut...

Asp.net C# Gridview linkbutton click event


Posted Date: 26 Feb 2011

Posted By:: vanchinathan

Member Level: Silver

Member Rank: 476

Points: 1 Responses: 3

Hi all,

I am using linkbutton inside the gridview. and when i click the linkbutton i want to go to the next url with the query string of dropdown list that is in ouside of gridview. please anybody tell

below is my design coding:

<asp:GridView ID="GridView2" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyField="CompanyName" EmptyDataText="No List Found"


Height="54px" ShowHeader="False" Width="615px" OnRowDataBound="GridView2_RowDataBound" CssClass="Gv">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<table>
<tr>
<td><asp:Image ID="imgPhoto" runat="server" ImageUrl='../<%#Eval("Picturefile") %>' Width ="120px" Height ="105px"/></td>
<td>
<asp:LinkButton ID="lbtnNext" runat="server" Text='<%#Eval("HotelName") %>' OnClick="lbtnNext_Click" />
<%--<a href='DetailedView.aspx?hn=<%#Eval("HotelName") %>'><%#Eval("HotelName") %></a>--%>
<br />
<%#Eval("Address")%><br />
</td>
<td>Price for <asp:Label ID="lblTotNights" runat="server" Text=''></asp:Label> night:<%#Eval("SingleSell") %></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" />
<EmptyDataRowStyle Font-Bold="True" HorizontalAlign="Center" />
<PagerStyle BackColor="#E0E0E0" Font-Bold="True" ForeColor="White" />
</asp:GridView>

<asp:DropDownList ID="ddlRooms" runat="server" CssClass="inputty2" AutoPostBack="True" OnSelectedIndexChanged="ddlRooms_SelectedIndexChanged">


<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
</asp:DropDownList>

.cs coding:
----------------protected void lbtnNext_Click(object sender, EventArgs e)
{
// [B]here i want to goto the detailed view page with the linkbutton value and querystring value of dropdown list name is ddlRooms that is outside of Gridview.[/B]
Response.Redirect("DetailedView.aspx?hn="+lbtnNext.Text+"&R="+ddlRooms.SelectedItem.ToString());
}

Error 4 The name 'lbtnNext' does not exist in the current context

thanks in advance

05-Apr-12 09:45 PM

Asp.net C# Gridview linkbutton click event

3 of 5

http://www.dotnetspider.com/forum/279821-Asp-net-C-Gridview-linkbut...

CTRL+Z stress, F5 Love Vresorts.in/Properties


Stay at hills & desert properties & save 33% on 3
nights stay. Book now
C, C++, C# courses Aptech www.Aptech-Education.com
50 lakh students trained already 22 years of
expertise
How To Speak English www.eagetutor.com
Learn To Improve Spoken English. Study English
with Online Courses.
100%JOB Oriented Training www.Miracleindia.com
.Net4.0,Java,SQT,Embedded,ERP,PHP
Oracle,Mobile,Telecom, 9311305845/7

Responses
#591158

Author:

Ravindran

Member Level: Diamond

Member Rank: 4

Date: 26/Feb/2011

Rating:

Points: 4

Hi refer my below code then only you clear grid view link button concept

Client side

I use grid view for show employee details..here employee no as link button

<asp:GridView ID="GridView1" runat="server" DataKeyNames="eno"


AutoGenerateColumns="false" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Employee no">
<itemtemplate>
<asp:LinkButton CommandName="cmdBind" runat="server" Text='<%#Eval("eno")%>' ID="hypeno" ToolTip='<%
</asp:LinkButton>
</itemtemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Employee No" DataField="eno" />
<asp:BoundField HeaderText="Employee Name" DataField="empname" />
<asp:BoundField HeaderText="Employee Salary" DataField="sal" />
</Columns>
</asp:GridView>

Server Side

You cannot get button text directly. You can get using gridview row command refer below code how can i get button text

using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Web;
System.Web.UI;
System.Web.UI.WebControls;
System.Data;
System.Data.SqlClient;

public partial class GridHypher : System.Web.UI.Page


{
SqlConnection sqlcon=new SqlConnection(@"Server=SQLEXPRESS;database=test;uid=xxxx;pwd=yyyy;");
SqlCommand sqlcmd;
SqlDataAdapter da;
DataTable dt=new DataTable();
DataTable dt1=new DataTable();
protected void Page_Load(object sender, EventArgs e)

05-Apr-12 09:45 PM

Asp.net C# Gridview linkbutton click event

4 of 5

http://www.dotnetspider.com/forum/279821-Asp-net-C-Gridview-linkbut...

{
sqlcon.Open();
sqlcmd = new SqlCommand("select * from emp", sqlcon);
da = new SqlDataAdapter(sqlcmd);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "cmdBind")
{
LinkButton lb = (LinkButton)e.CommandSource;
Response.Redirect("GridHypher2.aspx?eno=" + lb.Text);
}
}
}

Execute above code "eno" pass to GridHypher2.aspx page

I hope this one clear your doubt

Regards
N.Ravindran
Your Hard work never fails

#591162

Author:

Anup

Member Level: Silver

Member Rank: 451

Date: 26/Feb/2011

Rating:

Points: 2

Hi,

try this >>


dim var1 as string
Dim abc As Label = GridView2.FindControl("OlbtnNext")
var1 = abc.Text

#591247

Author:

Paritosh Mohapatra

Member Level: Diamond

Member Rank: 3

Date: 26/Feb/2011

Rating:

Points: 4

Please check the following code:

protected void lbtnNext_Click(object sender, EventArgs e)


{
// [B]here i want to goto the detailed view page with the linkbutton value and querystring value of dropdown lis
LinkButton lb = sender as LinkButton;
GridViewRow grow = (GridViewRow)lb.NamingContainer;
Response.Redirect("DetailedView.aspx?hn=" + lb.Text + "&R=" + ddlRooms.SelectedItem.ToString());
}

Thanks & Regards

Paritosh Mohapatra
Microsoft MVP (ASP.Net/IIS)
DotNetSpider MVM

05-Apr-12 09:45 PM

Asp.net C# Gridview linkbutton click event

5 of 5

http://www.dotnetspider.com/forum/279821-Asp-net-C-Gridview-linkbut...

Eliminate .NET ViewState www.techsoftinc.com/ViewState


Reduce page load times & bandwidth
requirements. Enhance Security
Part time employee MonsterIndia.com
1000's of Part Time Jobs Submit your Resume
Free. Now!
Jabong - Wear your Style Jabong.com/Online-Shopping
Upto 50% Off on Top Fashion Brands. Free
Shipping & COD. Shop Now!
JobNix.in www.JobNix.in
Blog For Linux Geeks Get Free email Alerts,
Update Daily

Share

Tweet

Share

Like

Sign Up to see what your friends like.

Post Reply

This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.

Next : Calculation in gridview


Related Messages
Previous : How to restrict user while entering data
Validation Controls

Return to Discussion Forum


Post New Message

no of click on advertisement in asp.net application

Category: ASP.NET
Please Please help.................. tooo urgent

Please tell me .Net interview Questions for 2+ Years Exp

To upload Documents like(doc,pdf,etc) to file system

About Us

Trademark Disclaimer

Contact Us

Copyright

Privacy Policy

Terms Of Use

Revenue Sharing sites

Copyright SpiderWorks Technologies Pvt Ltd., Kochi, India


2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.

05-Apr-12 09:45 PM

You might also like