Accessing public methods from a user control - c#

I have a user control with this simple public method:
public void appendHtml(string html)
{
txtTinyMce.InnerText += html;
}
In the page where this control is contained, I have specified it in the page's markup like so:
<QuickBuck:TinyMCE runat="server" ID="tinyMCE" />
When I call tinyMCE.appendHtml, it doesn't find it. What am I doing wrong?

Related

How to access MasterPage1 method in another ContentPage with MasterPage2 in asp.net?

I have Page1.aspx which has MasterPage1.master as its MasterPage and Page2.aspx which has MasterPage2.master as its MasterPage. I am showing Page2.aspx page in iframe (which is in Page1.aspx) Page2.aspx displays some items in ListView. After adding item to Cart(MyCartUserControl in MasterPage1.master) i want to call the Method say 'MyMethod()' which is in MasterPage1.master. in Page2.aspx
In Page2.aspx:
<%# MasterType VirtualPath="~/Sales/MasterPage1.master" %>
In Page2.aspx.cs
protected void UpdateShoppingCart()
{
Sales_MasterPage1 master = (Sales_MasterPage1)this.Master;
master.BindCart();
}
I know the above code will not work using 'this'. What is replaceable to 'this' keyword?
Help Appreciated!
Try below Code :
protected void UpdateShoppingCart()
{
Sales_MasterPage1 master = new Sales_MasterPage1();
master.BindCart();
}
Problem is the access modifier.
You might need to make it a public method because protected wont let you access it outside of that master class
public static void UpdateShoppingCart()
{
Sales_MasterPage1 master = new Sales_MasterPage1();
master.BindCart();
}
to call it from anywhere you can do:
Sales_MasterPage1.UpdateShoppingCart()

is that possible to add controls into WebUserControl from aspx page?

for example the user control i created is a dock panel, and i want to make it to allow add others controls from aspx page.
<uc1:dockPanel ID="dockPanel1" runat="server" >
--add control to here from aspx page--
</uc1:dockPanel>
You load controls dynamically via control.add method. First get an object of user control and then use control.add property for that.
See following link for the reference.
http://msdn.microsoft.com/en-us/library/aa287574(v=vs.71).aspx
in my research i found something like this.. so we can add an inner property inside the WebUserControl, for example.. but the problem that i facing now is the control is not render inside WebUserControl, its render in the page.
HTML
<uc1:dockPanel ID="dockPanel1" runat="server">
<ContentTemplate>
<dx:ASPxButton ID="ASPxButton1" runat="server" Text="ASPxButton">
</dx:ASPxButton>
</ContentTemplate>
</uc1:dockPanel>
ASCX
[ParseChildren(true)]
public partial class dockPanel :System.Web.UI.UserControl, INamingContainer
{
[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateContainer(typeof(dockPanel))]
public ITemplate ContentTemplate { get; set; }
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (ContentTemplate != null)
ContentTemplate.InstantiateIn(this);
}
}

How do you put custom markup in a sitecore sc field?

I need to add some custom markup for the sc:Image field to enchance SEO. This markup is not a property of the field, so from codebehind, I tried something like this:
slideImage.Attributes.Add("ControlType", "C4Image");
slideImage.Attributes.Add("rel", relString);
But this isn't working, and I see nothing in the rendered output. Is there any way to do this?
You need to use the "Parameters" property for setting extra properties on both the and control.
You can to it like this :
<sc:FieldRenderer ID="PageImage" runat="server" FieldName="ContentImage" Parameters="ControlType=C4Image&rel=relString" />
<sc:Image ID="SCPageImage" runat="server" Field="ContentImage" Parameters="ControlType=C4Image&rel=relString" />
That will be rendered like this :
<img width="1232" height="637" controltype="C4Image" rel="relString" alt="" src="~/media/Images/DEMO backgrounds/background2.ashx">
You can create you own class inheriting from the Sitecore.Web.UI.WebControls.Image and override it like this:
namespace My.Assembly.Namespace
{
public class MyImage : Sitecore.Web.UI.WebControls.Image
{
public virtual string RelAttribute { get; set; }
protected override void PopulateParameters(Sitecore.Collections.SafeDictionary<string> parameters)
{
base.PopulateParameters(parameters);
if (!String.IsNullOrEmpty(RelAttribute))
{
parameters.Add("rel", RelAttribute);
}
}
}
}
And then register the namespace and use the MyImage class:
<%# Register tagPrefix="my" namespace="My.Assembly.Namespace" assembly="My.Assembly" %>
<my:MyImage runat="server" RelAttribute="reltest" Field="logo"/>
You can use all the standard attributes from sc:Image on the my:MyImage as well.
The code will generate img tag with rel <img rel="reltest" src="logo.jpg" ... />.
You can easily extend the code above to support ControlType attribute as well.
For screnario's like this, I'd ditch the FieldRenderers and revert to a regular html tag with databinding to the (the url of the) image.
The LinkManager is your friend here.

Access ASPX elements inside classes

I have a bunch of methods which manipulate the ASPX page elements and at this point it makes sense to encapsulate them into their own static object. However, it seems like I do not have access into the form elements outside of the ASPX page. Any ideas on how to go about this?
You need to pass the Page itself into the class, see the example below:
ASPX page
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtTest" runat="server" Text="Test" />
</div>
</form>
Code-Behind
protected void Page_Load(object sender, EventArgs e)
{
Process p = new Process(this);
string s = p.GetTextBoxValue();
}
Class
public class Process
{
public Page thePage { get; set; }
public Process(Page page)
{
thePage = page;
}
public string GetTextBoxValue()
{
TextBox tb = (TextBox)thePage.FindControl("txtTest");
return tb.Text;
}
}
Process is probably not the best name for the class, but this is purely a demo.
Also, passing the Page object into another class tight couples that class to the Page object. I would recommend reconsidering your design of the class you're trying to make to not rely on the Page object entirely.
If you really want to encapsulate functionality, I guess you best create a class in which you pass the relevant elements to the constructor.
If you are aiming on reuse in other pages, you could create a base page from which you inherit. Another option is to do stuff in a master page that you refer from your pages.
I think a more detailed question is required to give a more detailed answer.
You need to pass Page object as one of the parameters to your class methods, this way its elements will be accessible inside the class.
For example if you have a class like:
public class CMyDataClass {
public bool CompareText(System.Web.UI.Page i_oPage) {
TextBox oTextBox = i_oPage.FindControl("TextBox1");
return (oTextBox.Text == "My Data");
}
}
You can use it like this from the page:
CMyDataClass oMyDataClass = new CMyDataClass();
if (oMyDataClass.CompareText(this)) {
Response.Write("Ok!");
}

Dynamic user control controls are null on load

I have a base usercontrol in my ASP.Net app, There are HTML markup within this usercontrol that are set to runat="server" and id's. The problem I am having is when the usercontrol is loaded, the HTML markup is being returned as null
ASP.Net C# code:
public partial class webonlinecustombase : System.Web.UI.UserControl
{
public Event Event { get; set; }
public OnlineSystemPageCustom custompage { get; set; }
public OnlineSystemPageCustom.OnlineSystemPageHdr.OnlineSystemPageModule custommodule { get; set; }
public void Page_Load(object sender, EventArgs e)
{
string typeName = custommodule.ModuleInternetFile;
inpagelink.HRef = "#" + custommodule.ModuleName.Replace(" ", "").Replace("/", "");
modtitle.InnerText = custommodule.ModuleName;
Type child = Type.GetType(typeName);
UserControl ctl = Activator.CreateInstance(child) as UserControl;
if (ctl != null)
{
this.modsection.Controls.Add(ctl);
}
}
}
Here is the HTML Markup:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="webonlinecustombase.ascx.cs" Inherits="IPAMIntranet.IPAM_Controls.webtemplatecontrols.webonlinecustombase" %>
<a id="inpagelink" runat="server"></a>
<span id="modtitle" runat="server" style="width:100%;text-align:left">Scientific Overview</span>
<div id="modsection" runat="server" style="width:100%;">
</div>
<p>Back to Top</p>
Why is the inpagelink and modtitle being returned as null?
I have seen this happen in web applications (not web sites) when changes are made to the source (especially setting runat=server on items that were not previously runat=server), but you don't switch to the designer view.
The way that I resolve this issue is to switch to design view and dirty a property in one of the fields. This usually causes VS to update the designer code-behind file.
You can double-check this file to ensure the controls have been added. If you check it prior to doing this, you should see that they are missing.
asp.net does'n have span class, so you cant do anything in code behind with it.
use LinkButton or HyperLink instead of
the other solution is to create span or a in code, something like this
var span = new HtmlGenericControl("span");
span.InnerHtml = "From<br/>Date";
span.Attributes["class"] = "blue";
placeholder.Controls.Add(span);
hope I helped :))

Categories

Resources