 |
| ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 3.5 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

September 24th, 2010, 08:44 AM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 74
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
I need some help with dynamically loaded usercontrols, thanks
Good day,
Here's my situation. I have a aspx page with a menu control and a placeholder which is itself in a ajax updatepanel.
When the menu control is clicked, a usercontrol is dynamically loaded into the place holder.
On the usercontrol, I have a div which contains a table and a asp button, the display property of the div tag is set to "none". Therefore a link on the usercontrol is used to display the div by setting its display to "block" when the link is clicked.
Here is the Problem: when the asp button on the div is clicked, the div disappears. please how can I make the div remain visible even when the button is clicked. I believe this is due to the asyncronous post back that occurs. note the the user control does not disappear, only the div does.
I need detailed explanation with c# code, Thanks...
|
|

September 24th, 2010, 09:12 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
I need detailed explanation with c# code
|
It's the other way around. *We* need a detailed explanation with C# code or we won't be able to help you....
Imar
|
|

September 24th, 2010, 12:37 PM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 74
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Detailed description.
Thank you.
I created a smaller app to explain the problem.
Here is the description:
////////////////////////////////////////////////////////////////////////////////////
I have two pages, the first is the parent page called "Test.aspx".
The second is a usercontrol called Members.ascx
Test.aspx contains a menu control, an update panel and a placeholder placed inside the update panel.When the menu item is clicked, the usercontrol is dynamically loaded into the placeholder located on Test.aspx (parent page)
Here is the mark up for Test.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="General_Test_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:Menu ID="Menu2" runat="server" OnMenuItemClick="Menu1_MenuItemClick" StaticDisplayLevels="3"
BorderWidth="0px">
<Items>
<asp:MenuItem Text="Create Membership" Value="~/General/Test/Members.ascx">
</asp:MenuItem>
</Items>
</asp:Menu>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Menu2" EventName="MenuItemClick" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
////////////////////////////////////////////////////////////////////////////////
2) Usercontrol description
The usercontrol contains a div tag within which I have placed an asp button in a table,the div's display property is set to none. Therefore the div is displayed when a hyperlink is clicked, this hyper link calls a javascript (which has been registered on the code behind of "Text.aspx-see code at lower end of page").The javascript code displays the div by setting display property to "block"
Here is the mark up of the usercontrol:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Members.ascx.cs" Inherits="General_Test_WebUserControl" %>
<style type="text/css">
#ShowDiv
{
display: none;
width: 300px;
position: fixed;
top: 30px;
left: 100px;
padding: 10px;
background-color: Silver;
}
</style>
<div id="ShowDiv">
<table bgcolor="#EDF5FA">
<tr>
<td>
<asp:Button ID="Button1" Text="Click Me" runat="server" />
</td>
<td>
<a href="javascript:fnCloseDiv();">Close</a>
</td>
</tr>
</table>
</div>
<a href="javascript:fnShowDiv();">Show Div</a>
////////////////////////////////////////////////////////////
PROBELM: Here is the description of my probem
Located on the div tag that I want to display when hyperlink is clicked (see usercontrol markup above) , I have a button control with id "Button1",
when this button is clicked, the postback causes the div to disappear.
This is not what I want however, I would like the div to remain visible even
when the post back occurs. The div should only disappear when I click the
"close" hyper link,on the usercontrol.Please how can I solve this?..
The code below is for the code behind of the parent page Text.aspx
it contains code that registers the clientscript and it also contains code
that reloads the usercontrol when a postback occours so u can ignore it.
Please how can I solve the problem I have described?
is my explanation clear? Thank you.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
public partial class General_Test_Default : System.Web.UI.Page
{
static string userControl;
protected void Page_Load(object sender, EventArgs e)
{
#region;//Register Client Scripts
Type type = this.GetType();
string key1 = "key1";
string key2 = "key2";
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
string text = "alert('Hello man');";
// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(type, key1))
{
cs.RegisterStartupScript(type, key1, text, true);
}
// Check to see if the client script is already registered.
if (!cs.IsClientScriptBlockRegistered(type, key2))
{
StringBuilder cstext = new StringBuilder();
cstext.Append("<script type=\"text/javascript\">");
cstext.Append("function fnShowDiv(){var vrShowDiv = document.getElementById('ShowDiv');vrShowDiv.style .display = 'block';}");
cstext.Append("function fnCloseDiv(){var vrShowDiv = document.getElementById('ShowDiv');vrShowDiv.style .display = 'none';}");
cstext.Append("</script>");
cs.RegisterClientScriptBlock(type, key2, cstext.ToString(), false);
}
#endregion;
if (!IsPostingFromMenu() && IsPostBack)
{
ReloadContent();
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
foreach (AsyncPostBackTrigger apbt in UpdatePanel1.Triggers)
{
if (Menu2.UniqueID.EndsWith(apbt.ControlID))
apbt.ControlID = Menu2.UniqueID;
}
}
public string TrackedUserControl
{
get { return ViewState["TrackedUserControl"] as string; }
set { ViewState["TrackedUserControl"] = value; }
}
protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
userControl = e.Item.Value;
LoadContent(e.Item.Value);
}
//
private void LoadContent(string menuItemName)
{
UserControl uc = null;
try
{
uc = Page.LoadControl(menuItemName) as UserControl;
}
catch (Exception ex)
{
}
//Set the property, tracked userControl
if (uc != null)
{
PlaceHolder1.Controls.Clear();
PlaceHolder1.Controls.Add(uc);
TrackedUserControl = menuItemName;
}
}
void ReloadContent()
{
UserControl uc = null;
try
{
uc = this.LoadControl(TrackedUserControl) as UserControl;
}
catch (Exception ex)
{
}
if (uc != null)
{
PlaceHolder1.Controls.Clear();
PlaceHolder1.Controls.Add(uc);
}
}
bool IsPostingFromMenu()
{
ScriptManager sm = ScriptManager.GetCurrent(this);
string ctlID = sm.AsyncPostBackSourceElementID;
Control c = Page.FindControl(ctlID);
//or
// Control c = this.FindControl(ctlID);
if (c == null)
return false;
return (c.ID == "Menu2");
}
}
Last edited by ysfkay; September 24th, 2010 at 12:51 PM..
Reason: Correction
|
|

September 24th, 2010, 03:17 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
The div should only disappear when I click the close hyper link.Please how can I solve this?..
|
I don't think it's the div that's disappearing, but the entire User Control. The result is the same, but it may help you look in the right direction.
Quote:
|
it also contains code that reloads the usercontrol when a postback occours so u can ignore it.
|
On the contrary. I believe that's exactly where the problem lies. In order to participate in stuff like post backs, dynamically loaded controls must be loaded early, in Page Init.
Search Google for some idea: http://www.google.com/search?hl=en&b...l=&oq=&gs_rfai=
Cheers,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|
 |