I need to use user controls (.ascx) on a page, it’s a related post user control based in 2 parameters:
1. Current post 2. Relation type
the page needs to have 3 different instances of this control, each having the same Current post parameter, but different relation type (title, author, genre).
The 1st parameter I can get it through url, but what about the second parameter?
I’ve been googling for a while but i haven’t found an answer yet. How can I pass the second parameter so the control can load the information based on these parameters?
I’d rather not to create a control for each parameter, else would be better to build no user control but direct into code 🙁
Thanks!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Create public properties of the user-control like:
public partial class SampleUC : UserControl
{
public string CurrentPost {get;set;}
public string RelationType {get;set;}
//...
//...
}
Assign those from the page using it either from markup like:
<%@ Register TagPrefix="cc" TagName="SampleUC" Src="SampleUC.ascx" %> ... ... <cc:SampleUC id="myUC" runat="server" CurrentPost="Sample Post Title" RelationType="Title" />
or from code-behind (of the page using it):
protected void Page_Load(object sender, EventArgs e)
{
//...
myUC.CurrentPost = "Sample Post Title";
myUC.RelationType = "Title" ;
//...
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0