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> <FONT face="ËÎÌå">
<asp:Label id="lblUC" runat="server"></asp:Label></FONT>
</td>
<td><FONT face="ËÎÌå"> </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.)