Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6384461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:46:44+00:00 2026-05-25T02:46:44+00:00

Consider this Asp.net page code: <head runat=server> <title></title> <script type=text/javascript> function showhide(master, detail) {

  • 0

Consider this Asp.net page code:

<head runat="server">
<title></title>
<script type="text/javascript">
        function showhide(master, detail) {
            var src = $(master).children()[0].src;
            if (src.endsWith("plus.png"))
                src = src.replace('plus.png', 'minus.png');
            else
                src = src.replace('minus.png', 'plus.png');

            $(master).children()[0].src = src;

            $(detail).slideToggle("normal");
        }
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Scripts>
        <asp:ScriptReference Path="~/scripts/jquery-1.6.2.min.js" ScriptMode="Release" />
    </Scripts>
</asp:ScriptManager>
<div>
<asp:SqlDataSource ID="sqlDsCustomers" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind %>"
    SelectCommand="SELECT [Customers].[CustomerID], [Customers].[CompanyName], COUNT([OrderID]) TotalOrders&#13;&#10;FROM [Customers] INNER JOIN [Orders] ON [Customers].[CustomerID]=[Orders].[CustomerID]&#13;&#10;Group By [Customers].[CustomerID], [Customers].[CompanyName]">
</asp:SqlDataSource>
    <asp:GridView Width="100%" AllowPaging="True" ID="gvCustomers" AutoGenerateColumns="False"
        DataSourceID="sqlDsCustomers" runat="server" ShowHeader="False" OnRowCreated="gvCustomers_RowCreated">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <div class="group" id='<%#String.Format("customer{0}",Container.DataItemIndex) %>'
                        onclick='showhide(<%#String.Format("\"#customer{0}\"",Container.DataItemIndex) %>,<%#String.Format("\"#order{0}\"",Container.DataItemIndex) %>)'>
                        <asp:Image ID="imgCollapsible" CssClass="first" ImageUrl="~/Assets/img/plus.png"
                            Style="margin-right: 5px;" runat="server" /><span class="header">
                                <%#Eval("CustomerID")%>
                                :
                                <%#Eval("CompanyName")%>
                                (<%#Eval("TotalOrders")%>Orders) </span>
                    </div>
                    <div id='<%#String.Format("order{0}",Container.DataItemIndex) %>' class="order">
                        <asp:GridView AutoGenerateColumns="false" CssClass="grid" ID="ddd" runat="server"
                            ShowHeader="true" EnableViewState="false">
                            <RowStyle CssClass="row" />
                            <AlternatingRowStyle CssClass="altrow" />
                            <Columns>
                                <asp:TemplateField ItemStyle-CssClass="rownum">
                                    <ItemTemplate>
                                        <%# Container.DataItemIndex + 1 %>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField HeaderText="Order ID" DataField="OrderID" ItemStyle-Width="80px" />
                                <asp:BoundField HeaderText="Date Ordered" DataField="OrderDate" DataFormatString="{0:MM/dd/yyyy}"
                                    ItemStyle-Width="100px" />
                                <asp:BoundField HeaderText="Date Required" DataField="RequiredDate" DataFormatString="{0:MM/dd/yyyy}"
                                    ItemStyle-Width="110px" />
                                <asp:BoundField HeaderText="Freight" DataField="Freight" DataFormatString="{0:c}"
                                    ItemStyle-Width="50px" ItemStyle-HorizontalAlign="Right" />
                                <asp:BoundField HeaderText="Date Shipped" DataField="ShippedDate" DataFormatString="{0:MM/dd/yyyy}"
                                    ItemStyle-Width="100px" />
                            </Columns>
                        </asp:GridView>
                    </div>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</form>

and the code behind:

protected void Page_Load(object sender, EventArgs e)
{

}
protected void gvCustomers_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string custID = ((DataRowView)e.Row.DataItem)["CustomerID"].ToString();
        using (DataClassesDataContext dc=new DataClassesDataContext())
        {
            List<Order> ord = (from o in dc.Orders
                               where o.CustomerID == custID.Trim()
                               select o).ToList();


            GridView ctrl = e.Row.FindControl("ddd") as GridView;
            ctrl.DataSource = ord;
        }
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    Response.Redirect("Default.aspx");
}

the problem is when I Click on Button1 to redirect to another page gvCustomers_RowCreated raise and I get Null reference error.Why this event raised after postback?


EDIT 1):

I removed SqlDataSource and bind GridView in the code behind like this:

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        using (DataClassesDataContext dc=new DataClassesDataContext())
        {
            var query = dc.ExecuteQuery<clsRetern>("SELECT [Customers].[CustomerID], [Customers].[CompanyName], COUNT([OrderID]) TotalOrders FROM [Customers] INNER JOIN [Orders] ON [Customers].[CustomerID]=[Orders].[CustomerID] Group By [Customers].[CustomerID], [Customers].[CompanyName]").ToList();

            List<clsRetern> ret = new List<clsRetern>();
            foreach (var item in query)
            {
                clsRetern r = new clsRetern();
                r.CompanyName = item.CompanyName;
                r.CustomerID = item.CustomerID;
                r.TotalOrders = item.TotalOrders;

                ret.Add(r);
            }

            gvCustomers.DataSource = ret;
            gvCustomers.DataBind();        
        }
    }
}
protected void gvCustomers_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string custID = ((clsRetern)e.Row.DataItem).CustomerID.Trim();
        using (DataClassesDataContext dc=new DataClassesDataContext())
        {
            List<Order> ord = (from o in dc.Orders
                               where o.CustomerID == custID.Trim()
                               select o).ToList();


            GridView ctrl = e.Row.FindControl("ddd") as GridView;
            ctrl.DataSource = ord;
        }
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    Response.Redirect("Default.aspx",true);
}
}

public class clsRetern
{
    public string CustomerID { get; set; }
    public string CompanyName { get; set; }
   public int TotalOrders { get; set; }
}

I tried Response.Redirect("Default.aspx"); but the problem still remains.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:46:45+00:00Added an answer on May 25, 2026 at 2:46 am

    If you understand ASP.NET page model then this question should not come to you. Every time request goes to ASP.NET page, a new page object is created along with entire control tree and the state is managed using view-state in post-back scenarios. So in your case, whenever post-back happens, a new page and grid-view is created. The data for the gid-view would be persisted in thew view-state and grid would be bound to that data.

    The RowCreated event is raised whenever grid row is created regardless of whether DataBind is explicitly called or not. The intent of the event is so that one can tweak with UI (grid-view cells) – e.g. some controls can be pushed into grid-view cells if required. The same should happen regardless of post-back scenario other wise those dynamic controls will not get created. Typically, these controls will restore their state using view-state and your get your grid-view UI (control tree) back as it was in first page cycle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider an ASP.NET page with this code: while (read) { Response.OutputStream.Write(buffer, 0, buffer.Length); Response.Flush();
Consider this snippet of code... <form runat=server> <asp:TextBox runat=server ID=TextBoxOne /> <asp:Button runat=server ID=SubmitOne
Consider the scenario of upgrading an ASP.NET MVC project. This question is around up-converting
Has anyone ever thought to attempt to modify the default ASP.NET Server error page
Consider the scenario where: an ASP.NET webform page has a DropDownList and a ListView.
Lets consider default ASP.NET MVC application folder structure, so it's looks like this: -App_data
I would like to first load an ASP.NET page and then fire a server-side
Consider an ASP.NET page using the jQuery library Uploadify . Here is the sequence
I'm using VS2010,C#,SQL Server to develop my ASP.NET web app, although this is not
Consider this code... using System.Threading; //... Timer someWork = new Timer( delegate(object state) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.