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

J

John Smith

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....................
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top