我有一个自定义的 asp.net 服务器组件,呈现如下:
<div id="divContentRating">
<div id="divAskForRating">
#Question
<br />
<a id="likeIcon"><img src="#PositiveRateIconPath"/></a>
<a id="neutralIcon"><img src="#NeutralRateIconPath"/></a>
<a id="unlikeIcon"><img src="#NegativeRateIconPath"/></a>
</div>
<div id="divPositiveRating">
<div>
<img src="#PositiveRateIconPath"/> #PositiveAnswerMessage <br />
<a href="javascript:void(0)" class="updateRate">Güncelle</a>
</div>
</div>
<div id="divNegativeRating">
<div>
<img src="#NegativeRateIconPath"/> #NegativeAnswerMessage <br />
<a href="javascript:void(0)" class="updateRate">Güncelle</a>
</div>
</div>
<div id="divNeutralRating">
<div>
<img src="#NeutralRateIconPath"/> #NeutralAnswerMessage <br />
<a href="javascript:void(0)" class="updateRate">Güncelle</a>
</div>
</div>
<input type="hidden" id="HasRated" value="#HasRated">
<input type="hidden" id="Rate" value="#Rate">
<input type="hidden" id="ContentKey" value="#ContentKey">
<input type="hidden" id="RatingId" value="#RatingId">
</div>
是否可以在我的 Web 控件中处理对图像的点击?我的意思是,当用户单击图像时,我想执行一些操作,但我想在我的 Web 控件中编写这些代码。
这是我的网络控件:
[DefaultProperty("ContentKey")]
[ToolboxData("<{0}:ContentRating runat=server></{0}:ContentRating>")]
public class ContentRating : WebControl
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string ContentKey
{
get
{
String s = (String)ViewState["ContentKey"];
return ((s == null) ? "[" + this.ID + "]" : s);
}
set
{
ViewState["ContentKey"] = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string PositiveRateIconPath
{
get
{
String s = (String)ViewState["PositiveRateIconPath"];
return ((s == null) ? "[" + this.ID + "]" : s);
}
set
{
ViewState["PositiveRateIconPath"] = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string NegativeRateIconPath
{
get
{
String s = (String)ViewState["NegativeRateIconPath"];
return ((s == null) ? "[" + this.ID + "]" : s);
}
set
{
ViewState["NegativeRateIconPath"] = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string NeutralRateIconPath
{
get
{
String s = (String)ViewState["NeutralRateIconPath"];
return ((s == null) ? "[" + this.ID + "]" : s);
}
set
{
ViewState["NeutralRateIconPath"] = value;
}
}
protected override void RenderContents(HtmlTextWriter output)
{
ContentRatingSettings contentRatingSettings = GetContentRatingSettings(this.ContentKey);
if (!contentRatingSettings.Visible)
{
output.Write(string.Empty);
return;
}
StringBuilder builder = new StringBuilder(@"
<div id=""divContentRating"">
<div id=""divAskForRating"">#Question
<br />
<a id=""likeIcon""><img src=""#PositiveRateIconPath""/></a>
<a id=""neutralIcon""><img src=""#NeutralRateIconPath""/></a>
<a id=""unlikeIcon""><img src=""#NegativeRateIconPath""/></a>
</div>
<div id=""divPositiveRating"">
<div>
<img src=""#PositiveRateIconPath""/> #PositiveAnswerMessage <br />
<a href=""javascript:void(0)"" class=""updateRate"">Güncelle</a>
</div>
</div>
<div id=""divNegativeRating"">
<div>
<img src=""#NegativeRateIconPath""/> #NegativeAnswerMessage <br />
<a href=""javascript:void(0)"" class=""updateRate"">Güncelle</a>
</div>
</div>
<div id=""divNeutralRating"">
<div>
<img src=""#NeutralRateIconPath""/> #NeutralAnswerMessage <br />
<a href=""javascript:void(0)"" class=""updateRate"">Güncelle</a>
</div>
</div>
<input type=""hidden"" id=""HasRated"" value=""#HasRated"">
<input type=""hidden"" id=""Rate"" value=""#Rate"">
<input type=""hidden"" id=""ContentKey"" value=""#ContentKey"">
<input type=""hidden"" id=""RatingId"" value=""#RatingId"">
<script type=""text/javascript"">
$(document).ready(function () {
var protocol = location.protocol;
var host = window.location.host;
if ($(""#HasRated"").val() == ""True"")
{
var rate = $(""#Rate"").val();
if (rate == 1) {
setPositiveRatedView();
}
else if (rate == 0) {
setNeutralRatedView();
}
else if (rate == -1) {
setNegativeRatedView();
}
else {
setNotRatedView();
}
}
else {
setNotRatedView();
}
$(""#likeIcon"").click(function () {
alert(""like"");
setPositiveRatedView();
ratePage(1, """");
});
$(""#neutralIcon"").click(function () {
alert(""neutral"");
setNeutralRatedView();
ratePage(0, """");
});
$(""#unlikeIcon"").click(function () {
alert(""unlike"");
setNegativeRatedView();
//mkMPopClc('NegativeRatingReason', 200, 300, 0, 0);
});
$("".updateRate"").click(function () {
setNotRatedView();
});
function setNotRatedView() {
$(""#divNeutralRating"").fadeOut();
$(""#divPositiveRating"").fadeOut();
$(""#divAskForRating"").fadeIn();
$(""#divNegativeRating"").fadeOut();
}
function setPositiveRatedView()
{
$(""#divNegativeRating"").fadeOut();
$(""#divNeutralRating"").fadeOut();
$(""#divAskForRating"").fadeOut();
$(""#divPositiveRating"").fadeIn();
}
function setNegativeRatedView() {
$(""#divNeutralRating"").fadeOut();
$(""#divPositiveRating"").fadeOut();
$(""#divAskForRating"").fadeOut();
$(""#divNegativeRating"").fadeIn();
}
function setNeutralRatedView() {
$(""#divNegativeRating"").fadeOut();
$(""#divPositiveRating"").fadeOut();
$(""#divAskForRating"").fadeOut();
$(""#divNeutralRating"").fadeIn();
}
function ratePage(rating, comment)
{
//alert(rating + """" """" + comment);
var contentKey = $(""#ContentKey"").val();
var hasRated = $(""#HasRated"").val();
var ratingId = $(""#RatingId"").val();
}
});
</script>
</div>");
SetTrackingCookie();
builder.Replace("#ContentKey", this.ContentKey);
builder.Replace("#PositiveRateIconPath", this.PositiveRateIconPath);
builder.Replace("#NeutralRateIconPath", this.NeutralRateIconPath);
builder.Replace("#NegativeRateIconPath", this.NegativeRateIconPath);
builder.Replace("#Question", contentRatingSettings.Question);
builder.Replace("#PositiveAnswerMessage", contentRatingSettings.PositiveAnswerMessage);
builder.Replace("#NeutralAnswerMessage", contentRatingSettings.NeutralAnswerMessage);
builder.Replace("#NegativeAnswerMessage", contentRatingSettings.NegativeAnswerMessage);
output.Write(builder);
}
}
提前致谢,