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

How to send parameter to user control postback

P: n/a
I have a user control being used instead of a frame page. when the user
clicks on a menu item I need to send the ID (integer value) of that menu as
a parameter in the postback of the user control which will be used to query
sql server to repopulate the datagrid in the user control.

I also wrapped the user control in a panel element so I could position it on
the page.

I'm clueless on how to get started with this.

1) how do cause the user control to postback?
2) how do I send a parameter in that postback?

Thanks.

--
mo*******@nospam.com
Nov 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
<<I'm clueless on how to get started with this.>>

I'd like to recommend the ASP.NET Commerce starter kit for you (just follow
this link).
http://www.asp.net/Default.aspx?tabindex=9&tabid=47

The starter kits are complete small applications written in ASP.NET (with
either VB or C#, code inline or code behind - you chose which you want to
evaluate). The documentation is excellent - explaining and demonstrating
most basic ASP.NET development tasks and typical application features. You
can install the starter kits on your computer, run the final application,
and dive into the code.

HTH

"moondaddy" <mo*******@nospam.com> wrote in message
news:O9**************@tk2msftngp13.phx.gbl...
I have a user control being used instead of a frame page. when the user
clicks on a menu item I need to send the ID (integer value) of that menu as a parameter in the postback of the user control which will be used to query sql server to repopulate the datagrid in the user control.

I also wrapped the user control in a panel element so I could position it on the page.

I'm clueless on how to get started with this.

1) how do cause the user control to postback?
2) how do I send a parameter in that postback?

Thanks.

--
mo*******@nospam.com

Nov 18 '05 #2

P: n/a
Hi moondaddy,
Thank you for using MSDN Newsgroup! My name is Steven, and I'll be
assisting you on this issue.
From your description, you're using a UserControl in an ASP.NET page. You'd
like to add some control for firing some post back events. Also, you want
to transmit some paramters together with the postback event?
If there is anything I misunderstood, please feel free to let me know.

1. As for the first question, since you're generating a menu via the
UserControl, you can use some "LinkButton" control to act as the hyperlink
of the menus, then because the "LinkButton" has its own postback event, you
can deal with each link's post back event separately, needn't send any
other parameter, how do you think of this?
For example, here is a sample UserControl to show this:
-----------------ascx file--------------------

<table width="500" align="center">
<tr>
<td style="HEIGHT: 20px"><FONT face="宋体">
<asp:LinkButton id="lnk1" runat="server">Menu1</asp:LinkButton></FONT>
</td>
<td style="HEIGHT: 20px">
<asp:LinkButton id="lnk2" runat="server">Menu2</asp:LinkButton>
</td>
</tr>
<tr>
<td><FONT face="宋体">
<asp:LinkButton id="lnk3"
runat="server">Menu3</asp:LinkButton></FONT></td>
<td><FONT face="宋体">
<asp:LinkButton id="lnk4"
runat="server">Menu4</asp:LinkButton></FONT></td>
</tr>
<tr>
<td><FONT face="宋体"></FONT></td>
<td></td>
</tr>
<tr>
<td><FONT face="宋体"></FONT>&nbsp;<FONT face="宋体">
<asp:Label id="lblUC" runat="server"></asp:Label></FONT>
</td>
<td><FONT face="宋体">&nbsp; </FONT>
</td>
</tr>
</table>
------------------------------control code behind -------------------------
public class GenericUC : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label lblUC;
protected System.Web.UI.WebControls.LinkButton lnk1;
protected System.Web.UI.WebControls.LinkButton lnk2;
protected System.Web.UI.WebControls.LinkButton lnk3;
protected System.Web.UI.WebControls.LinkButton lnk4;

private void Page_Load(object sender, System.EventArgs e)
{

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.lnk1.Click += new System.EventHandler(this.lnk1_Click);
this.lnk2.Click += new System.EventHandler(this.lnk2_Click);
this.lnk3.Click += new System.EventHandler(this.lnk3_Click);
this.lnk4.Click += new System.EventHandler(this.lnk4_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnUC_Click(object sender, System.EventArgs e)
{

}

private void lnk1_Click(object sender, System.EventArgs e)
{
lblUC.Text = "Menu1 is clicked!";
}

private void lnk2_Click(object sender, System.EventArgs e)
{
lblUC.Text = "Menu2 is clicked!";
}

private void lnk3_Click(object sender, System.EventArgs e)
{
lblUC.Text = "Menu3 is clicked!";
}

private void lnk4_Click(object sender, System.EventArgs e)
{
lblUC.Text = "Menu4 is clicked!";
}
}

2. Also, if you don't want to use the LinkButton control, or you prefer to
use some general hyperlink such as
"<a href="........">" and you want to post a event to server when the link
is clicked and also send some parameters. I think you can try use a hidden
field control to store the paramter, for example,
the <input type="hidden" runat= server> control is a good one.

When the link is clicked, first set the proper value of the "hidden"
control and then you can manually fire a button's
click() event to call a post back event to server. You can set the Button's
visible as false so as not to display it on the page. To make my
desciprtion clearly, I've also build a simple test control to show ths
method, here is the source:

-------------------------ascx source-------------------------
<table width="500" align="center">
<tr>
<td>
<a href="#" id="menu1" runat="server">Menu1</a>
</td>
<td>
<a href="#" id="menu2" runat="server">Menu2</a>
</td>
</tr>
<tr>
<td>
<a href="#" id="menu3" runat="server">Menu3</a>
</td>
<td>
<a href="#" id="menu4" runat="server">Menu4</a>
</td>
</tr>
<tr>
<td>
<asp:Label id="lblUC" runat="server"></asp:Label>
</td>
<td> <input type="button" runat="server" value="Post Back"
style="DISPLAY:none" id="btnPostBack"
name="Button1">
<input type="hidden" id="hdParam" runat="server">
</td>
</tr>
</table>
---------------------------------control code-behind class
source--------------------------
public class GenericUC : System.Web.UI.UserControl
{
protected System.Web.UI.HtmlControls.HtmlInputButton btnPostBack;
protected System.Web.UI.HtmlControls.HtmlAnchor menu1;
protected System.Web.UI.HtmlControls.HtmlAnchor menu2;
protected System.Web.UI.HtmlControls.HtmlAnchor menu3;
protected System.Web.UI.HtmlControls.HtmlAnchor menu4;
protected System.Web.UI.HtmlControls.HtmlInputHidden hdParam;
protected System.Web.UI.WebControls.Label lblUC;

private void Page_Load(object sender, System.EventArgs e)
{
string script = hdParam.ClientID + ".value= ";
string script2 = btnPostBack.ClientID + ".click();";
menu1.Attributes.Add("onclick", script + "1;" + script2);
menu2.Attributes.Add("onclick", script + "2;" + script2);
menu3.Attributes.Add("onclick", script + "3;" + script2);
menu4.Attributes.Add("onclick", script + "4;" + script2);
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnPostBack.ServerClick += new
System.EventHandler(this.btnPostBack_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion


private void btnPostBack_ServerClick(object sender, System.EventArgs e)
{
lblUC.Text = "Menu" + hdParam.Value + " is clicked!";
}


}
Because the control in UserControl's client id will change when it is set
with in a page. So we need to dynamically generate the javascript code in
the code-hehind code.

Please check out the preceding suggestions. If you have any questions on
them or need any help, please feel free to let me know.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #3

P: n/a
Thank you Steven. I think this is getting close to what will help me. I
need to clarify the page setup.

I have a main page which will have the typical header menus, graphics, etc.
across the top. then down the left hand side I will have a menu control
dynamically generated from data from the database, but for now, I'm going to
just hard code the menus on the left had side. each menu will name a
category such as animals, food, or what ever. when I click on the menu it
will send the ID for that category (say 43 for example) to the user control
which will take up most of the body of the page. this user control will
replace what a frame page normally would do. so the link button as you
described will reside in main.aspx and it will have to send the value of 43
to a hidden button in the user control as you described below.

Lastly, is it possible to provide the example in vb.net? I apologize for
not specifying the environment earlier which is vb.net 1.1, winxp pro, and
sql server 2k.

Thanks for the great replies.
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:wt**************@cpmsftngxa07.phx.gbl...
Hi moondaddy,
Thank you for using MSDN Newsgroup! My name is Steven, and I'll be
assisting you on this issue.
From your description, you're using a UserControl in an ASP.NET page. You'd like to add some control for firing some post back events. Also, you want
to transmit some paramters together with the postback event?
If there is anything I misunderstood, please feel free to let me know.

1. As for the first question, since you're generating a menu via the
UserControl, you can use some "LinkButton" control to act as the hyperlink
of the menus, then because the "LinkButton" has its own postback event, you can deal with each link's post back event separately, needn't send any
other parameter, how do you think of this?
For example, here is a sample UserControl to show this:
-----------------ascx file--------------------

<table width="500" align="center">
<tr>
<td style="HEIGHT: 20px"><FONT face="宋体">
<asp:LinkButton id="lnk1" runat="server">Menu1</asp:LinkButton></FONT>
</td>
<td style="HEIGHT: 20px">
<asp:LinkButton id="lnk2" runat="server">Menu2</asp:LinkButton>
</td>
</tr>
<tr>
<td><FONT face="宋体">
<asp:LinkButton id="lnk3"
runat="server">Menu3</asp:LinkButton></FONT></td>
<td><FONT face="宋体">
<asp:LinkButton id="lnk4"
runat="server">Menu4</asp:LinkButton></FONT></td>
</tr>
<tr>
<td><FONT face="宋体"></FONT></td>
<td></td>
</tr>
<tr>
<td><FONT face="宋体"></FONT>&nbsp;<FONT face="宋体">
<asp:Label id="lblUC" runat="server"></asp:Label></FONT>
</td>
<td><FONT face="宋体">&nbsp; </FONT>
</td>
</tr>
</table>
------------------------------control code behind ------------------------- public class GenericUC : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label lblUC;
protected System.Web.UI.WebControls.LinkButton lnk1;
protected System.Web.UI.WebControls.LinkButton lnk2;
protected System.Web.UI.WebControls.LinkButton lnk3;
protected System.Web.UI.WebControls.LinkButton lnk4;

private void Page_Load(object sender, System.EventArgs e)
{

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.lnk1.Click += new System.EventHandler(this.lnk1_Click);
this.lnk2.Click += new System.EventHandler(this.lnk2_Click);
this.lnk3.Click += new System.EventHandler(this.lnk3_Click);
this.lnk4.Click += new System.EventHandler(this.lnk4_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnUC_Click(object sender, System.EventArgs e)
{

}

private void lnk1_Click(object sender, System.EventArgs e)
{
lblUC.Text = "Menu1 is clicked!";
}

private void lnk2_Click(object sender, System.EventArgs e)
{
lblUC.Text = "Menu2 is clicked!";
}

private void lnk3_Click(object sender, System.EventArgs e)
{
lblUC.Text = "Menu3 is clicked!";
}

private void lnk4_Click(object sender, System.EventArgs e)
{
lblUC.Text = "Menu4 is clicked!";
}
}

2. Also, if you don't want to use the LinkButton control, or you prefer to
use some general hyperlink such as
"<a href="........">" and you want to post a event to server when the link
is clicked and also send some parameters. I think you can try use a hidden
field control to store the paramter, for example,
the <input type="hidden" runat= server> control is a good one.

When the link is clicked, first set the proper value of the "hidden"
control and then you can manually fire a button's
click() event to call a post back event to server. You can set the Button's visible as false so as not to display it on the page. To make my
desciprtion clearly, I've also build a simple test control to show ths
method, here is the source:

-------------------------ascx source-------------------------
<table width="500" align="center">
<tr>
<td>
<a href="#" id="menu1" runat="server">Menu1</a>
</td>
<td>
<a href="#" id="menu2" runat="server">Menu2</a>
</td>
</tr>
<tr>
<td>
<a href="#" id="menu3" runat="server">Menu3</a>
</td>
<td>
<a href="#" id="menu4" runat="server">Menu4</a>
</td>
</tr>
<tr>
<td>
<asp:Label id="lblUC" runat="server"></asp:Label>
</td>
<td> <input type="button" runat="server" value="Post Back"
style="DISPLAY:none" id="btnPostBack"
name="Button1">
<input type="hidden" id="hdParam" runat="server">
</td>
</tr>
</table>
---------------------------------control code-behind class
source--------------------------
public class GenericUC : System.Web.UI.UserControl
{
protected System.Web.UI.HtmlControls.HtmlInputButton btnPostBack;
protected System.Web.UI.HtmlControls.HtmlAnchor menu1;
protected System.Web.UI.HtmlControls.HtmlAnchor menu2;
protected System.Web.UI.HtmlControls.HtmlAnchor menu3;
protected System.Web.UI.HtmlControls.HtmlAnchor menu4;
protected System.Web.UI.HtmlControls.HtmlInputHidden hdParam;
protected System.Web.UI.WebControls.Label lblUC;

private void Page_Load(object sender, System.EventArgs e)
{
string script = hdParam.ClientID + ".value= ";
string script2 = btnPostBack.ClientID + ".click();";
menu1.Attributes.Add("onclick", script + "1;" + script2);
menu2.Attributes.Add("onclick", script + "2;" + script2);
menu3.Attributes.Add("onclick", script + "3;" + script2);
menu4.Attributes.Add("onclick", script + "4;" + script2);
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnPostBack.ServerClick += new
System.EventHandler(this.btnPostBack_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion


private void btnPostBack_ServerClick(object sender, System.EventArgs e)
{
lblUC.Text = "Menu" + hdParam.Value + " is clicked!";
}


}
Because the control in UserControl's client id will change when it is set
with in a page. So we need to dynamically generate the javascript code in
the code-hehind code.

Please check out the preceding suggestions. If you have any questions on
them or need any help, please feel free to let me know.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #4

P: n/a
Hi moondaddy,
Thanks for your response. I've got the detialed requirement from your last
reply. I've rewrote my sample page and the UserControl to perform a more
closely example in account to your situation, here is the new code:

-----------------------------------------------------UserControl's ascx
file-------------------------------------
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="GenericUC.ascx.vb" Inherits="GenericUC"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<table width="500" align="center">
<tr>
<td>
<asp:Label id="lblMessage" runat="server"></asp:Label></td>
<td><FONT face="宋体"></FONT></td>
</tr>
<tr>
<td><FONT face="宋体"><INPUT id="hdMenuID" type="hidden" name="hdMenuID"
runat="server"></FONT></td>
<td><FONT face="宋体"><INPUT id="btnPostBack" style="DISPLAY: none"
type="button" value="PostBack" name="btnPostBack"
runat="server"></FONT></td>
</tr>
</table>

-----------------------UserControl's code behind class----------------------
Public Class GenericUC
Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
Protected WithEvents hdMenuID As
System.Web.UI.HtmlControls.HtmlInputHidden
Protected WithEvents btnPostBack As
System.Web.UI.HtmlControls.HtmlInputButton

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub

Private Sub btnPostBack_ServerClick(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnPostBack.ServerClick
lblMessage.Text = "Menu:" + hdMenuID.Value + " is clicked!"
End Sub
End Class

----------------------------------------------------the sample page's aspx
source-----------------------
<%@ Register TagPrefix="uc1" TagName="GenericUC" Src="GenericUC.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Nav.aspx.vb"
Inherits="Nav"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Nav</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language=javascript>
function PostNotify(hd, value, btn)
{
document.all(hd).value = value;
document.all(btn).click();
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td><a href="#" id="lnkMenuA" runat="server">MenuA</a></td>
<td><a href="#" id="lnkMenuB" runat="server">MenuB</a></td>
<td><a href="#" id="lnkMenuC" runat="server">MenuC</a></td>
<td><a href="#" id="lnkMenuD" runat="server">MenuD</a></td>
<td><a href="#" id="lnkMenuE" runat="server">MenuE</a></td>
</tr>
<tr>
<td colspan="5">
<uc1:GenericUC id="GenericUC1" runat="server"></uc1:GenericUC></td>
</tr>
</table>
</form>
</body>
</HTML>

------------------------------------sample page's code behind
class---------------------------------
Public Class Nav
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents lnkMenuA As System.Web.UI.HtmlControls.HtmlAnchor
Protected WithEvents lnkMenuB As System.Web.UI.HtmlControls.HtmlAnchor
Protected WithEvents lnkMenuC As System.Web.UI.HtmlControls.HtmlAnchor
Protected WithEvents lnkMenuD As System.Web.UI.HtmlControls.HtmlAnchor
Protected WithEvents lnkMenuE As System.Web.UI.HtmlControls.HtmlAnchor

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim uc As GenericUC = Page.FindControl("GenericUC1")
Dim hd As HtmlInputHidden = uc.FindControl("hdMenuID")
Dim btn As HtmlInputButton = uc.FindControl("btnPostBack")

lnkMenuA.Attributes.Add("onclick", "PostNotify('" & hd.ClientID &
"','MenuA','" & btn.ClientID & "')")
lnkMenuB.Attributes.Add("onclick", "PostNotify('" & hd.ClientID &
"','MenuB','" & btn.ClientID & "')")
lnkMenuC.Attributes.Add("onclick", "PostNotify('" & hd.ClientID &
"','MenuC','" & btn.ClientID & "')")
lnkMenuD.Attributes.Add("onclick", "PostNotify('" & hd.ClientID &
"','MenuD','" & btn.ClientID & "')")
lnkMenuE.Attributes.Add("onclick", "PostNotify('" & hd.ClientID &
"','MenuE','" & btn.ClientID & "')")
End Sub

End Class

----------------------------------------------------------------------------

Also, I've attached the page file in this message, please check them out to
see whether they'll work for you. Meanwhile, if you have any other
questions ,please feel free to post here.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Nov 18 '05 #5

P: n/a
Big thanks Steven! Your effort in re-writing that is greatly appreciated.
Maybe you can lend some high-level advise. My intent on using the user
control was to replace using a frame page and to force the only the user
control to do a post back and allow it's parent page to not make any
additional roundtrips as there will be a lot of content in it. Am I
approaching this the wrong way? what is the best way to re-load the page in
the user control with the best overall performance? An associate of mine
built a similar page with a complex menu going down the left had side (and
at some point I will have a similar menu) that was used to navigate the
pages in the control which made up the body of the page. every time the
control did a postback, this menu reloaded which I think degraded
performance and visually didn't look good. what are your recommendations
here in regards to emulating the performance gained by a frame page
refreshing only one of its panes as apposed to the entire page?
Thanks.
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:M$**************@cpmsftngxa07.phx.gbl...
Hi moondaddy,
Thanks for your response. I've got the detialed requirement from your last
reply. I've rewrote my sample page and the UserControl to perform a more
closely example in account to your situation, here is the new code:

-----------------------------------------------------UserControl's ascx
file-------------------------------------
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="GenericUC.ascx.vb" Inherits="GenericUC"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<table width="500" align="center">
<tr>
<td>
<asp:Label id="lblMessage" runat="server"></asp:Label></td>
<td><FONT face="宋体"></FONT></td>
</tr>
<tr>
<td><FONT face="宋体"><INPUT id="hdMenuID" type="hidden" name="hdMenuID"
runat="server"></FONT></td>
<td><FONT face="宋体"><INPUT id="btnPostBack" style="DISPLAY: none"
type="button" value="PostBack" name="btnPostBack"
runat="server"></FONT></td>
</tr>
</table>

-----------------------UserControl's code behind class---------------------- Public Class GenericUC
Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
Protected WithEvents hdMenuID As
System.Web.UI.HtmlControls.HtmlInputHidden
Protected WithEvents btnPostBack As
System.Web.UI.HtmlControls.HtmlInputButton

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub

Private Sub btnPostBack_ServerClick(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnPostBack.ServerClick
lblMessage.Text = "Menu:" + hdMenuID.Value + " is clicked!"
End Sub
End Class

----------------------------------------------------the sample page's aspx
source-----------------------
<%@ Register TagPrefix="uc1" TagName="GenericUC" Src="GenericUC.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Nav.aspx.vb"
Inherits="Nav"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Nav</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language=javascript>
function PostNotify(hd, value, btn)
{
document.all(hd).value = value;
document.all(btn).click();
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td><a href="#" id="lnkMenuA" runat="server">MenuA</a></td>
<td><a href="#" id="lnkMenuB" runat="server">MenuB</a></td>
<td><a href="#" id="lnkMenuC" runat="server">MenuC</a></td>
<td><a href="#" id="lnkMenuD" runat="server">MenuD</a></td>
<td><a href="#" id="lnkMenuE" runat="server">MenuE</a></td>
</tr>
<tr>
<td colspan="5">
<uc1:GenericUC id="GenericUC1" runat="server"></uc1:GenericUC></td>
</tr>
</table>
</form>
</body>
</HTML>

------------------------------------sample page's code behind
class---------------------------------
Public Class Nav
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents lnkMenuA As System.Web.UI.HtmlControls.HtmlAnchor
Protected WithEvents lnkMenuB As System.Web.UI.HtmlControls.HtmlAnchor
Protected WithEvents lnkMenuC As System.Web.UI.HtmlControls.HtmlAnchor
Protected WithEvents lnkMenuD As System.Web.UI.HtmlControls.HtmlAnchor
Protected WithEvents lnkMenuE As System.Web.UI.HtmlControls.HtmlAnchor

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim uc As GenericUC = Page.FindControl("GenericUC1")
Dim hd As HtmlInputHidden = uc.FindControl("hdMenuID")
Dim btn As HtmlInputButton = uc.FindControl("btnPostBack")

lnkMenuA.Attributes.Add("onclick", "PostNotify('" & hd.ClientID &
"','MenuA','" & btn.ClientID & "')")
lnkMenuB.Attributes.Add("onclick", "PostNotify('" & hd.ClientID &
"','MenuB','" & btn.ClientID & "')")
lnkMenuC.Attributes.Add("onclick", "PostNotify('" & hd.ClientID &
"','MenuC','" & btn.ClientID & "')")
lnkMenuD.Attributes.Add("onclick", "PostNotify('" & hd.ClientID &
"','MenuD','" & btn.ClientID & "')")
lnkMenuE.Attributes.Add("onclick", "PostNotify('" & hd.ClientID &
"','MenuE','" & btn.ClientID & "')")
End Sub

End Class

-------------------------------------------------------------------------- --
Also, I've attached the page file in this message, please check them out to see whether they'll work for you. Meanwhile, if you have any other
questions ,please feel free to post here.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #6

P: n/a
Hi moondaddy,
Thanks for your response. From your further description in the last reply,
why you used the user control is to let the UserControl be posted back to
server and do dome operations with out post back the mainpage(container
page) ,yes? If so, I'm afraid, you have to use the a <iframe> or frame
structure pages to implement this. Because a UserControl though has its own
html template (ascx file) and codebehind class(ascx.vb), it is still a part
of its container page when be used in a certain main page(container page).
So when the UserControl do post back event, the container page will be
posted back,too. If you'd like the container page not to be posted back
also, I think you still need to use the <iframe> solution. How do you think
of this? Please let me know if my understanding is correct. Also if you
have any other questions please also feel free to post here. I'll be
willing to help you.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #7

P: n/a
Hi moondaddy,
Have you viewed my suggestions in the last reply or have you got any ideas
on this issue. If you have any questions or need any help, please feel free
to let me know. Thanks.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.