Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to show data associated with a record of a gridview.

I tried to employ the technique used in "http://www.aspsnippets.com/Articles/Nested-GridView-Example-in-ASPNet-using-C-and-VBNet.aspx" replacing the nested gridview with a label, to no avail:
ASP.NET
<table id="tblShiftSumry">
                <tr>
                    <td class="summaryCost">
                        <asp:GridView ID="gvShift" runat="server" AutoGenerateColumns="False" Height="95px"
                            Style="margin-right: 1px" Width="790px" OnRowDataBound="OnRowDataBound">
                            <columns>
                                <asp:TemplateField HeaderText="Frequency">
                                    <itemtemplate>
                                        <img alt="" style="cursor: pointer" src="Resources/Images/plus.png" />
                                        <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                                            <asp:Label ID="lblMsg" runat="server" Text="Testing 1...2...3">
                                        
                                    </itemtemplate>
                                    <HeaderStyle Width="15px" />
                                
                                <asp:TemplateField HeaderText="Shift #">
                                    <itemtemplate>
                                        <asp:TextBox ID="txtShftNo" runat="server" ReadOnly="True" 
                                        Text='<%# DataBinder.Eval(Container.DataItem, "Shift #") %>'
                                        Width="<%# 25 %>" CssClass="summaryCost">
                                    </itemtemplate>
                                    <HeaderStyle Width="75px" />
                                


In the <head> section, I placed:
<script src="~/Scripts/jquery-1.9.1.js" type="text/javascript">
    $("[src*=plus]").live("click", function () {
        $(this).closest("tr").after("" + $(this).next().html() + "")
        $(this).attr("src", "~/resources/images/minus.png");
    });
    $("[src*=minus]").live("click", function () {
        $(this).attr("src", "~/resources/images/edit.png");
        $(this).closest("tr").next().remove();
    });
</script>



In the code behind section, I get an error that pnlOrders does not exist in the current context:

C#
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           pnlOrders.visible = true;
       }
   }


Instead of the label, I need to nest a panel (which will contain a table, radio button, text box and check box) within the gridview. That data will be shown/hidden when the user clicks on the "plus" or "minus" sign in the first column.
Posted
Updated 31-Mar-13 19:05pm
v2
Comments
Sergey Alexandrovich Kryukov 31-Mar-13 23:59pm    
Not a question, sorry.
—SA
FabeCode 1-Apr-13 0:02am    
How do I nest a panel that will contain a table, radio button, text box and check box?
FabeCode 1-Apr-13 9:53am    
This is a nesting issue. I need to have the panel appear/hide when the user clicks on the plus/minus in the first column. I tried to use the script code in conjunction with the OnRowDataBound method to accomplish this task. Thanks.
vinodkumarnie 1-Apr-13 1:32am    
What so big deal in that. You just put controls you needed inside panel as normal. I click of of button find panel control and do necessary action.
FabeCode 1-Apr-13 2:33am    
In the code behind, i get an error: pnlOrders does not exist. Do you have simple code example that works?

You cann't access controls which are inside Gridview as like normal. You have do find those controls and then try to access those.

See example below.

C#
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Panel panel_find = ((Panel)e.Row.FindControl("pnlOrders"));
            panel_find.Visible=true;
        }
    }


Note: You can access all controls which are inside Gridview by finding those as mentioned below.

Hope this works and also helps you..
 
Share this answer
 
Comments
vinodkumarnie 1-Apr-13 23:59pm    
Is this solution worked or not..?
FabeCode 2-Apr-13 10:54am    
No. it has not. The panel does not show once I click on the "plus" in first column of the grid.
vinodkumarnie 2-Apr-13 11:58am    
try to use next() of find() functions of JQuery.
FabeCode 2-Apr-13 12:29pm    
New to JQuery. Can you provide an example?
FabeCode 3-Apr-13 1:32am    
For those lines that begin with ?, I remove the "less than" character. Hope this works...

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sample.aspx.cs" Inherits="Sample" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="~/Scripts/jquery-1.4.1.js" type="text/javascript">
$("[src*=plus]").live("click", function () {
$(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
$(this).attr("src", "~/resources/images/minus.png");
});
$("[src*=minus]").live("click", function () {
$(this).attr("src", "~/resources/images/plus.png");
$(this).closest("tr").next().remove();
});

</script>
</head>
<body>
<form id="form1" runat="server">
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="true">
? cc1:ToolkitScriptManager>
<div>
<asp:Panel ID="AssgnOffcrs" runat="server">
<asp:Label ID="lblShifts" runat="server" Text="Number of Shifts:">
 
<asp:TextBox ID="txtNumShifts" runat="server" OnTextChanged="txtNumShifts_TextChanged"
Width="35px" AutoPostBack="True">
? asp:TextBox>
<asp:ImageButton ID="imgbAdd" runat="server" Visible="false"
ImageUrl="~/Resources/Images/add.png" onclick="imgbAdd_Click" />
<br />
<table id="tblShiftSumry">
<tr>
<td class="summaryCost">
<asp:GridView ID="gvShift" runat="server" AutoGenerateColumns="False" Height="95px"
Style="margin-right: 1px" Width="100px" OnRowDataBound="OnRowDataBound">
? Columns>
<asp:TemplateField HeaderText="Frequency">
? ItemTemplate>
<img alt="" style="cursor: pointer" src="Resources/Images/plus.png" />
<asp:Panel ID="pnlOrders" runat="server" Style="display: none">
<asp:Label ID="lblMsg" Width="100px" runat="server" Text="Testing 1...2...3">
? /asp:Panel>
? /ItemTemplate>
<HeaderStyle Width="15px" />
? /asp:TemplateField>
? /Columns>
? /asp:GridView>
</td>
</tr>
</table>
? /asp:Panel>
</div>
</form>
</body>
</html>
I modified the javascript and defined a class parameter for the panel as follows:



$(document).ready(function () {
$(".plus").toggle(
function () {
$(this).next(".pnlOrders").show();
$(this).attr("src", "Resources/Images/minus.png");
},
function () {
$(this).next(".pnlOrders").hide();
$(this).attr("src", "Resources/Images/plus.png");
});
});


.
.
<asp:panel id="pnlOrders" class="pnlOrders" style="display: none" runat="server" xmlns:asp="#unknown">
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900