By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,691 Members | 1,398 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,691 IT Pros & Developers. It's quick & easy.

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

P: n/a
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:

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:
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,
mnuttMan.Items[0].Selected = true;

protected void mnuttMan_MenuItemClick(object sender, MenuEventArgs e)
mvttMan.ActiveViewIndex = Int32.Parse(e.Item.Value);

<asp:Menu ID="mnuttMan" Width="100%" runat="server"
<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
But if I do it like this, it fails:
// 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 =
"").Replace("_", " ");

if (strDBView == "My Choice")
ctlShowtts = LoadControl("incs/Showtts.ascx");

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

Okay, and the code for the user control is as follows:
<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";

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


grdDB.DataSource = ds;

public void grdDB_PageIndexChanged(Object source, GridViewPageEventArgs
grdDB.PageIndex = e.NewPageIndex;

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

................The End....................

Aug 2 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.