こんにちは仲間のstackoverflowers(それが言葉なら!)、
動的プロパティを使用してユーザーコントロールを作成しようとしていますが、希望どおりに機能させることができません。このコントロールの目的は、開発者がASPX.CSで何もしなくても、設計者がASPX内に配置できる汎用のAddThisウィジェットを用意することです。以前は、ASPXページのコードビハインド内でユーザーコントロールの動的プロパティを設定しただけでしたが、今では、プログラマーとデザイナーの両方をこのような単純なタスクに関与させるのは面倒なようです。
これは私のコードです:
ユーザー制御コード(ASCX):
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AddThisSocial.ascx.cs"
Inherits="MyWebsite.Controls.AddThisSocial" %>
<!-- Begin AddThis Social Share -->
<div class="addthis_toolbox addthis_default_style" addthis:url="<%=Url%>" addthis:title="<%=Title %>"
addthis:description="<%=Description%>">
<%
if (Facebook)
{
%>
<a class="addthis_button_facebook" title="Send to Facebook" style="margin-top: 5px;">
</a>
<%
}
%>
<%
if (Twitter)
{
%>
<a class="addthis_button_twitter" title="Send to Twitter" style="margin-top: 5px;">
</a>
<%
}
%>
<%
if (Google)
{
%>
<a class="addthis_button_google" title="Send to Google" style="margin-top: 5px;">
</a>
<%
}
%>
<%
if (Pinterest)
{
%>
<a class="addthis_button_pinterest_pinit" style="margin-top: 0px;">
</a>
<%
}
%>
<%
if (CompactButton)
{
%>
<a class="addthis_button_compact" style="margin-top: 5px;"></a>
<%
}
%>
</div>
<!-- End AddThis Social Share -->
ユーザー制御コード(ASCX.CS):
namespace MyWebsite.Controls
{
public partial class AddThisSocial : System.Web.UI.UserControl
{
#region Public Properties
public string Url { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string ImageUrl { get; set; }
[DefaultValue("False")]
public Boolean Facebook { get; set; }
[DefaultValue("False")]
public Boolean Twitter { get; set; }
[DefaultValue("False")]
public Boolean Google { get; set; }
[DefaultValue("False")]
public Boolean Pinterest { get; set; }
[DefaultValue("False")]
public Boolean CompactButton { get; set; }
#endregion
#region Page Events
protected void Page_Load(object sender, EventArgs e)
{
// facebook doesn't like addthis attributes - add opengraph attributes seperately
var page = HttpContext.Current.Handler as Page;
if (page == null) return;
if(!String.IsNullOrEmpty(Url))
{
var mUrl = new HtmlMeta();
mUrl.Attributes.Add("property", "og:url");
mUrl.Attributes.Add("content", Url);
page.Header.Controls.Add(mUrl);
}
if (!String.IsNullOrEmpty(Title))
{
var mTitle = new HtmlMeta();
mTitle.Attributes.Add("property", "og:title");
mTitle.Attributes.Add("content", Server.HtmlEncode(Title));
page.Header.Controls.Add(mTitle);
}
if (!String.IsNullOrEmpty(Description))
{
var mDescription = new HtmlMeta();
mDescription.Attributes.Add("property", "og:description");
mDescription.Attributes.Add("content", Server.HtmlEncode(Description));
page.Header.Controls.Add(mDescription);
}
if (!String.IsNullOrEmpty(ImageUrl))
{
var mImageUrl = new HtmlMeta();
mImageUrl.Attributes.Add("property", "og:image");
mImageUrl.Attributes.Add("content", ImageUrl);
page.Header.Controls.Add(mImageUrl);
}
}
#endregion
}
}
ASPXページコードの一部:
<MyWebsite:AddThisSocial runat="server" ID="ProductAddThisSocial" Description="<%# Eval("Description") %>" ImageUrl="<%= GetImageUrl() %>" Facebook="True" Twitter="True" Google="True" CompactButton="True" Pinterest="True" />
上記は機能しませんが、静的文字列をコントロールに設定すると機能します。
<MyWebsite:AddThisSocial runat="server" ID="ProductAddThisSocial" Description="My Description" ImageUrl="My Image Url" Facebook="True" Twitter="True" Google="True" CompactButton="True" Pinterest="True" />
私はこれのデバッグを行いましたが、これについてWebで読んだ後は、これが機能しないことは理にかなっています。コントロールプロパティは、親ページのロードメソッドの時点では初期化されていません。また、プロパティをバインド可能に設定し、page_loadをpage_prerenderメソッドに変更しようとしましたが運がありませんでした。どういうわけか、コードビハインドでコントロールプロパティを設定できないようにする解決策を見つける必要があります。誰か助けてもらえますか?
ありがとう、Hiral