Wrox Programmer Forums
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking 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 Basics 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
 
Old December 9th, 2009, 05:13 PM
Authorized User
 
Join Date: Dec 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default User Control

Hi,

I have created one menu user control. This contorl is referenced by all the web pages part of the web application. The issue i am encountering is that wheni refer this user control and then click on any button, there is no action happening.

e.g I have a button in the application called: Generate Report. When the menu user control is refernced and the button is clicked, no action takes place. Instead i would like it to preform the actions menioned in the button_click event.

when the menu control reference is removed, everything works fine.

Any suggestions please. :(

Thanks
 
Old December 9th, 2009, 05:42 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

As Ken used to say: the error is on line 11.....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old December 9th, 2009, 05:51 PM
Authorized User
 
Join Date: Dec 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default User Control

Quote:
Originally Posted by Imar View Post
As Ken used to say: the error is on line 11.....

Imar
Can u please elaborate on it a bit?

Thanks
 
Old December 9th, 2009, 06:11 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Sorry; it's an old joke. It's better read (and funnier) as follows:


> Exactly!
>> How do you know? I didn't post any code
>>> The error is on line 11

In other words: please post the relevant code for your user control and the part of the page where you include it. Without it, we can't say anything useful about your problem.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old December 9th, 2009, 06:15 PM
Authorized User
 
Join Date: Dec 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default User Control

Quote:
Originally Posted by Imar View Post
Sorry; it's an old joke. It's better read (and funnier) as follows:


> Exactly!
>> How do you know? I didn't post any code
>>> The error is on line 11

In other words: please post the relevant code for your user control and the part of the page where you include it. Without it, we can't say anything useful about your problem.

Cheers,

Imar
User Control is a Menu:

UI Code (MenuControl.aspx):
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MenuControl.ascx.cs" Inherits="TRS.TRSApp.UserControls.MenuControl" %>

<script type="text/javascript" language="javascript"></script>
<body onload='hide();'>
<form name="navigationForm" method="post"
action="/bagfinder/navigateApp.do">
<input type="hidden" name="selectedNavButton" value="4" />
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td><img src="../images/tsca-narrow-banner.jpg"></td><br /></tr>
<tr width="100%" style="background-color:000099">
<td width="100%" style="background-color:000099">
<span id="navbar">
<asp:Menu ID ="mnuMenu" runat ="server" Orientation="Horizontal"
StaticMenuStyle-BackColor="#000099" StaticMenuItemStyle-ForeColor="White"
DynamicMenuItemStyle-BackColor="#000099" DynamicMenuItemStyle-ForeColor="White"
onmenuitemclick="mnuMenu_MenuItemClick" >
</asp:Menu>
</span>
</td>
</tr>
</table>
</form>
</body>

Code Behind (MenuControl.aspx.cs)

using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Data.SqlClient;

namespace TRS.TRSApp.UserControls
{
public partial class MenuControl : System.Web.UI.UserControl
{
SqlConnection objSqlConnection = new SqlConnection("Data Source=hydhtc24115L;Initial Catalog=TSCA;Integrated Security=SSPI");

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
objSqlConnection.Open();
PopulateRootLevel();
}
}

private void PopulateRootLevel()
{
SqlCommand objCommand = new SqlCommand(@"select Id,Title,(select count (*) from TSCA_MenuList where ParentId = menu.Id)
as ChildNodeCount from TSCA_MenuList menu where ParentId is null", objSqlConnection);
SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCommand);
DataTable dt = new DataTable();
objDataAdapter.Fill(dt);
PopulateNodes(dt,mnuMenu.Items);
}

private void PopulateSubLevel(int parentId, MenuItem parentMenu)
{
SqlCommand objCommand = new SqlCommand(@"select Id,Title,(select count (*) from TSCA_MenuList where ParentId = menu.Id)
as ChildNodeCount from TSCA_MenuList menu where ParentId =" + parentId + "", objSqlConnection);
SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCommand);
DataTable dt = new DataTable();
objDataAdapter.Fill(dt);
PopulateNodes(dt,parentMenu.ChildItems);
}

private void PopulateNodes(DataTable dt, MenuItemCollection items)
{
foreach(DataRow dr in dt.Rows)
{
MenuItem item = new MenuItem();
item.Text = dr["Title"].ToString();
item.Value = dr["Id"].ToString();
items.Add(item);

// Node with child nodes
bool flag = ((int)(dr["ChildNodeCount"]) > 0);
if (flag)
{
menuCreate(item);
}
}
}

private void menuCreate(MenuItem menu)
{
MenuEventArgs e = new MenuEventArgs(menu);
PopulateSubLevel(Int32.Parse(e.Item.Value),e.Item) ;
}

protected void mnuMenu_MenuItemClick(object sender, MenuEventArgs e)
{
switch(e.Item.Text)
{
case "Home":
MenuItem HomeItem1 = new MenuItem();
MenuItem HomeItem2 = new MenuItem();
MenuItem HomeItem3 = new MenuItem();
MenuItem HomeItem4 = new MenuItem();
MenuItem HomeItem5 = new MenuItem();
MenuItem HomeItem6 = new MenuItem();
MenuItem HomeItem7 = new MenuItem();
MenuItem HomeItem8 = new MenuItem();
MenuItem HomeItem9 = new MenuItem();

HomeItem1.Text = "Home";
HomeItem2.Text = "Import";
HomeItem3.Text = "Export";
HomeItem4.Text = "Assessment";
HomeItem5.Text = "Search";
HomeItem6.Text = "Reports";
HomeItem7.Text = "Help";
HomeItem8.Text = "Export Summary";
HomeItem9.Text = "Import Summary";

mnuMenu.Items.Clear();
mnuMenu.Items.AddAt(0, HomeItem1);
mnuMenu.Items.AddAt(1, HomeItem2);
mnuMenu.Items.AddAt(2, HomeItem3);
mnuMenu.Items.AddAt(3, HomeItem4);
mnuMenu.Items.AddAt(4, HomeItem5);
mnuMenu.Items.AddAt(5, HomeItem6);
mnuMenu.Items[5].ChildItems.AddAt(0, HomeItem8);
mnuMenu.Items[5].ChildItems.AddAt(1, HomeItem9);
mnuMenu.Items.AddAt(6, HomeItem7);
return;
case "Import":

MenuItem ImportItem1 = new MenuItem();
MenuItem ImportItem2 = new MenuItem();
MenuItem ImportItem3 = new MenuItem();

ImportItem1.Text = "Home";
ImportItem2.Text = "Import";
ImportItem3.Text = "Help";

mnuMenu.Items.Clear();
mnuMenu.Items.AddAt(0, ImportItem1);
mnuMenu.Items.AddAt(1, ImportItem2);
mnuMenu.Items.AddAt(2, ImportItem3);
return;
case "Export":
MenuItem ExportItem1 = new MenuItem();
MenuItem ExportItem2 = new MenuItem();
MenuItem ExportItem3 = new MenuItem();

ExportItem1.Text = "Home";
ExportItem2.Text = "Export";
ExportItem3.Text = "Help";

mnuMenu.Items.Clear();
mnuMenu.Items.AddAt(0, ExportItem1);
mnuMenu.Items.AddAt(1, ExportItem2);
mnuMenu.Items.AddAt(2, ExportItem3);
return;
case "Assessment":
MenuItem AssessItem1 = new MenuItem();
MenuItem AssessItem2 = new MenuItem();
MenuItem AssessItem3 = new MenuItem();

AssessItem1.Text = "Home";
AssessItem2.Text = "Assessment";
AssessItem3.Text = "Help";

mnuMenu.Items.Clear();
mnuMenu.Items.AddAt(0, AssessItem1);
mnuMenu.Items.AddAt(1, AssessItem2);
mnuMenu.Items.AddAt(2, AssessItem3);
return;
case "Search":
MenuItem SearchItem1 = new MenuItem();
MenuItem SearchItem2 = new MenuItem();
MenuItem SearchItem3 = new MenuItem();

SearchItem1.Text = "Home";
SearchItem2.Text = "Search";
SearchItem3.Text = "Help";

mnuMenu.Items.Clear();
mnuMenu.Items.AddAt(0, SearchItem1);
mnuMenu.Items.AddAt(1, SearchItem2);
mnuMenu.Items.AddAt(2, SearchItem3);
return;
case "Reports":
MenuItem ReportsItem1 = new MenuItem();
MenuItem ReportsItem2 = new MenuItem();
MenuItem ReportsItem3 = new MenuItem();
MenuItem ReportsItem4 = new MenuItem();
MenuItem ReportsItem5 = new MenuItem();

ReportsItem1.Text = "Home";
ReportsItem2.Text = "Reports";
ReportsItem3.Text = "Help";
ReportsItem4.Text = "ExportSummary";
ReportsItem5.Text = "ImportSummary";

mnuMenu.Items.Clear();
mnuMenu.Items.AddAt(0, ReportsItem1);
mnuMenu.Items.AddAt(1, ReportsItem2);
mnuMenu.Items[1].ChildItems.AddAt(0, ReportsItem4);
mnuMenu.Items[1].ChildItems.AddAt(1, ReportsItem5);
mnuMenu.Items.AddAt(2, ReportsItem3);
return;
case "Help":
MenuItem HelpItem1 = new MenuItem();
MenuItem HelpItem2 = new MenuItem();

HelpItem1.Text = "Home";
HelpItem2.Text = "Help";

mnuMenu.Items.Clear();
mnuMenu.Items.AddAt(0, HelpItem1);
mnuMenu.Items.AddAt(1, HelpItem2);
return;
}
}
}
}

Used in Reports UI as:

<%@ Register Src="~/UserControls/MenuControl.ascx" TagName="Menu" TagPrefix="ucMenu" %>

<form runat="server">
<ucMenu:Menu ID = "hdrmenu" runat="server" />
</form>

Please suggest?

Thanks
 
Old December 9th, 2009, 06:25 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

I don't see a Button or a click handler in there. Am I overlooking something?

I think the problem is with the code in the UC:

<body onload='hide();'>
<form name="navigationForm" method="post"
...
</form>
</body>

User controls are page fragments; they don't have their own <body> or <form> elements; you can only have 1 form per ASP.NET page.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; December 9th, 2009 at 06:41 PM..
 
Old December 9th, 2009, 06:25 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

BTW: No need to quote the entire message when replying; clutters up the thread unnecessarily....

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old December 9th, 2009, 06:31 PM
Authorized User
 
Join Date: Dec 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default User Control

There is only one form tag in th layut page.

and the button click event is given below. for reference:
protected void btnGenerateReport_Click(object sender, EventArgs e)
{
tblCaption.Visible = true;
btnImportReport.Visible = true;

string ReportType = null;
GridView dgReport = new GridView();
ReportType = "Export";
dgReport = GetReport(ReportType);

if (dgReport.Rows.Count != 0)
{
Label lblNewMessage = new Label();
TRS.TRSApp.UserControls.MessageArea objMessageArea = new MessageArea();
lblNewMessage = objMessageArea.ShowMessage(MessageText, MessageClass);
msgArea.Controls.Add(lblNewMessage);
}
else
{
tblCaption.Visible = false;
dgReport.Visible = false;
btnExcel.Visible = false;
btnImportReport.Visible = false;
}
}

I think there should be some problem with the inclusion of user control in the web page or the aspx page of the user control itself. Its because without the control everthing works fine.

Please suggest?
 
Old December 9th, 2009, 06:40 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It could be me, but this:
Code:
 
 
UI Code (MenuControl.aspx): 
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MenuControl.ascx.cs" Inherits="TRS.TRSApp.UserControls.MenuControl" %>

<script type="text/javascript" language="javascript"></script>
<body onload='hide();'>
<form name="navigationForm" method="post"
action="/bagfinder/navigateApp.do">
seems to imply you have <form> and <body> tags in the User Control. You should have those in the page instead...

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old December 9th, 2009, 06:50 PM
Authorized User
 
Join Date: Dec 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default User Control

Ya, guess you are correct.
Let me check...

Thanks a ton.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Add User Control To The Tab Control cheer ASP.NET 3.5 Basics 1 February 25th, 2009 10:48 PM
Add Windows User control in Web User Control agarwalvidhu C# 0 March 30th, 2006 01:17 AM
Help! Custom Server Control using User Control diehard ASP.NET 1.0 and 1.1 Professional 2 January 4th, 2006 12:33 PM
Help with control initialization in user control mike_remember ASP.NET 1.0 and 1.1 Professional 7 December 19th, 2005 11:08 AM
User Control!! NitinJoshi General .NET 2 January 21st, 2005 07:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.