problem with user control containing GridView control and paging...

Discussion in 'ASP .Net' started by John Smith, Aug 2, 2006.

  1. John Smith

    John Smith Guest

    ASP.Net 2.0 / C# / IIS 6

    I have 2 pages. The master page consists of a tabbed menu created using the
    Menu and MultiView controls. Something like this:

    http://fredrik.nsquared2.com/viewpost.aspx?PostID=344&showfeedback=true

    The second page is a user control that contains a GridView control linked to
    a database table. What's supposed to happen is that the user clicks on a
    menu choice and an appropriate user control will load. For example, if one
    of the menu choices is "User Data", the GridView will connect to the
    database and show the appropriate data. No problem there. I have the
    GridView set for paging up to 15 records per page. When I load the user
    control at design time (i.e. <myControl:dataGrid id="blah" runat="server"
    />), everything seems to work fine.

    However, I need to dynamically load the controls at run-time based on a menu
    choice. This is what is driving me bonkers. What happens now is that the
    datagrid won't bind. If I call the databinding function outside of the
    !IsPostBack if-clause, it will show the data. But when you click on the
    next page, the GridView goes away. I have tried everything I could think
    of. What am I missing?

    Below is the relevant code. I appreciate any suggestions!!!!!!!! Like I
    said, if I load the control at design time, the app works like a charm. But
    if I load it as needed, it doesn't work right. Hopefully, you will
    understand what I'm trying to do.

    When I do it like this, it works:
    master.aspx
    <script>
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!this.IsPostBack)
    {
    for (int index = 0; index < mvttMan.Views.Count; index++)
    {
    strMenuLabel = mvttMan.Views[index].ID.ToString().Replace("vw",
    "").Replace("_", " ");
    mnuttMan.Items.Add(new MenuItem(strMenuLabel,
    index.ToString()));
    }
    mnuttMan.Items[0].Selected = true;
    }
    }

    protected void mnuttMan_MenuItemClick(object sender, MenuEventArgs e)
    {
    mvttMan.ActiveViewIndex = Int32.Parse(e.Item.Value);
    }
    </script>

    <asp:Menu ID="mnuttMan" Width="100%" runat="server"
    OnMenuItemClick="mnuttMan_MenuItemClick">
    <asp:MultiView ID="mvttMan" runat="server" ActiveViewIndex="0">

    <asp:View ID="vwMy_Choice" runat="server" >
    <cnt:showtts ID="cntShowtts" runat="server" /> <----it
    works like this
    </asp:View>
    </asp:MultiView>
    ........................................
    But if I do it like this, it fails:
    master.aspx
    // code mostly the same above...just loading the control at design time and
    binding to a placeholder
    protected void mnuttMan_MenuItemClick(object sender, MenuEventArgs e)
    {
    string strDBView;

    mvttMan.ActiveViewIndex = Int32.Parse(e.Item.Value);
    strDBView =
    mvttMan.Views[Int32.Parse(e.Item.Value)].ID.ToString().Replace("vw",
    "").Replace("_", " ");

    if (strDBView == "My Choice")
    {
    ctlShowtts = LoadControl("incs/Showtts.ascx");
    plhShowtts.Controls.Add(ctlShowtts);
    }
    }

    <asp:View ID="vwMy_tts" runat="server" >
    <asp:placeHolder ID="plhShowtts" runat="server" />
    </asp:View>

    Okay, and the code for the user control is as follows:
    usercontrol.ascx
    <script runat="server">

    MySqlConnection conMain;
    string strSortDirection;

    public void Page_Load(Object source, EventArgs e)
    {
    conMain = new MySqlConnection("connection");

    //BindDataGrid("Ticket_Number"); //if I uncomment this, it will
    obviously show data on first load but nothing afterwards

    if (!this.IsPostBack)
    {
    BindDataGrid(); // this doesn't appear to be happening when I
    load the control dynamically
    }
    }

    public void BindDataGrid()
    {
    string sql = "sql goes here";

    conMain.Open();
    MySqlDataAdapter ad = new MySqlDataAdapter(sql, conMain);
    DataSet ds = new DataSet();

    ad.Fill(ds);

    grdDB.DataSource = ds;
    grdDB.DataBind();
    conMain.Close();
    }

    public void grdDB_PageIndexChanged(Object source, GridViewPageEventArgs
    e)
    {
    grdDB.PageIndex = e.NewPageIndex;
    BindDataGrid();
    }
    </script>

    <asp:GridView ID="grdDB" runat="server" AllowPaging="true"
    OnPageIndexChanging="grdDB_PageIndexChanged" PageSize="15">
    <AlternatingRowStyle BackColor="Silver" />
    </asp:GridView>

    ................The End....................
     
    John Smith, Aug 2, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGF0cmljay5PLklnZQ==?=

    DataSet paging vs Datareader paging

    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=, Oct 8, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    10,708
    Scott Allen
    Oct 8, 2004
  2. Red
    Replies:
    1
    Views:
    726
  3. Replies:
    0
    Views:
    1,473
  4. Replies:
    0
    Views:
    157
  5. Cole Trickle

    template column containing user control & paging query

    Cole Trickle, Feb 3, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    110
    Cole Trickle
    Feb 3, 2005
Loading...

Share This Page