ASP.NET - asp.net C# Gridview linkbutton click event

Asked By vanchi nathan on 26-Feb-11 02:43 AM
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 the coding?

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
Jatin Prajapati replied to vanchi nathan on 26-Feb-11 02:54 AM
Hi,
For this type of requirements you have to use RowCommand event of the gridview. Please refer below code example.

01.<%@ Page Language="C#" %>
02. 
03.<%@ Import Namespace="System" %>
04.<%@ Import Namespace="System.Data" %>
05.<%@ Import Namespace="System.Data.SqlClient" %>
06.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
07.<script runat="server">
08.  protected void Page_Load(object sender, EventArgs e)
09.  {
10.    if (!IsPostBack)
11.      LoadProducts();
12.  }
13. 
14.   
15.  private void LoadProducts()
16.  {
17.    using (SqlConnection connection = new SqlConnection("ConnectionString"))
18.    {
19.      using (SqlCommand command = new SqlCommand("Select * from Products", connection))
20.      {
21.        using (SqlDataAdapter da = new SqlDataAdapter(command)) {
22.          DataTable dt = new DataTable();
23.          da.Fill(dt);
24.          GridView1.DataSource = dt;
25.          GridView1.DataBind();
26.        }
27.      }
28.    }
29.  }
30. 
31.  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
32.  {
33.    if (e.CommandName == "goto")
34.    {
35.      string queryString = "hn=" + e.CommandArgument.ToString() + "&R=" + ddl.SelectedItem.Text;
36.      Response.Redirect("DetailedView.aspx?" + queryString);
37.    }
38.  }
39.</script>
40.<html xmlns="http://www.w3.org/1999/xhtml">
41.<head runat="server">
42.  <title></title>
43.</head>
44.<body>
45.  <form id="form1" runat="server">
46.  <div>
47.    <asp:ScriptManager ID="sm" runat="server">
48.    </asp:ScriptManager>
49.    <asp:DropDownList ID="ddl" runat="server">
50.      <asp:ListItem Text="Item1"></asp:ListItem>
51.      <asp:ListItem Text="Item2"></asp:ListItem>
52.      <asp:ListItem Text="Item3"></asp:ListItem>
53.      <asp:ListItem Text="Item4"></asp:ListItem>
54.      <asp:ListItem Text="Item5"></asp:ListItem>
55.    </asp:DropDownList>
56.    <asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand">
57.      <Columns>
58.        <asp:TemplateField>
59.          <ItemTemplate>
60.            <asp:LinkButton ID="btnDelete" runat="server" Text='<%#Eval("ProductName") %>' CommandName="goto" CommandArgument='<%#Eval("ProductName") %>' />
61.          </ItemTemplate>
62.        </asp:TemplateField>
63.      </Columns>
64.    </asp:GridView>
65.    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
66.      SelectCommand="SELECT [ProductID], [ProductName], [QuantityPerUnit], [UnitPrice], [UnitsInStock] FROM [Alphabetical list of products]">
67.    </asp:SqlDataSource>
68.  </div>
69.  </form>
70.</body>
71.</html>
Subhashini Janakiraman replied to vanchi nathan on 26-Feb-11 04:37 AM
Here is the modified code.I have made changes in the lbtnNext_Click event handler.Hope it helps,

protected void lbtnNext_Click(object sender, EventArgs e)
{
  Response.Redirect("DetailedView.aspx?hn="+((LinkButton)(sender)).Text+"&R="+ddlRooms.SelectedItem.ToString());
 }

Also set the attribute AutoEventWireUp="true" in the Page directive of the inline coding.