Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.net服务器控件中处理事件_C#_Asp.net_Custom Controls - Fatal编程技术网

C# 在asp.net服务器控件中处理事件

C# 在asp.net服务器控件中处理事件,c#,asp.net,custom-controls,C#,Asp.net,Custom Controls,我有一个自定义asp.net服务器组件,呈现如下: <div id="divContentRating"> <div id="divAskForRating"> #Question <br /> <a id="likeIcon"><img src="#PositiveRateIconPath"/></a> <a id="neutralIcon"><img src="#Neut

我有一个自定义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控件中编写这些操作

这是我的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);
    }
}
[DefaultProperty(“ContentKey”)]
[ToolboxData(“”)
公共类内容分级:网络控制
{
[可装订(真实)]
[类别(“外观”)]
[默认值(“”)
[可本地化(正确)]
公共字符串ContentKey
{
得到
{
字符串s=(字符串)视图状态[“ContentKey”];
返回((s==null)“[”+this.ID+“]”:s);
}
设置
{
ViewState[“ContentKey”]=值;
}
}
[可装订(真实)]
[类别(“外观”)]
[默认值(“”)
[可本地化(正确)]
公共字符串正整数
{
得到
{
字符串s=(字符串)视图状态[“PositiveRateIconPath”];
返回((s==null)“[”+this.ID+“]”:s);
}
设置
{
ViewState[“PositiveRateIconPath”]=值;
}
}
[可装订(真实)]
[类别(“外观”)]
[默认值(“”)
[可本地化(正确)]
公共字符串NegativeRateIconPath
{
得到
{
字符串s=(字符串)视图状态[“NegativeRateIconPath”];
返回((s==null)“[”+this.ID+“]”:s);
}
设置
{
ViewState[“NegativeRateIconPath”]=值;
}
}
[可装订(真实)]
[类别(“外观”)]
[默认值(“”)
[可本地化(正确)]
公共字符串NeutralRateIconPath
{
得到
{
字符串s=(字符串)视图状态[“NeutralRateIconPath”];
返回((s==null)“[”+this.ID+“]”:s);
}
设置
{
ViewState[“NeutralRateIconPath”]=值;
}
}
受保护的覆盖无效渲染内容(HtmlTextWriter输出)
{
ContentRatingSettings ContentRatingSettings=GetContentRatingSettings(this.ContentKey);
如果(!contentRatingSettings.Visible)
{
output.Write(string.Empty);
返回;
}
StringBuilder=新的StringBuilder(@“
#问题:

#否定的回答信息
#中立消息
$(文档).ready(函数(){ var协议=location.protocol; var host=window.location.host; if($(“”#HasRated“”).val()=“True”) { 风险值比率=$(“比率”).val(); 如果(比率==1){ setPositiveRatedView(); } 否则,如果(比率==0){ setNeutralRatedView(); } 否则如果(比率==-1){ setNegativeRatedView(); } 否则{ setNotRatedView(); } } 否则{ setNotRatedView(); } $(“#likeIcon”)。单击(函数(){ 警觉(“像”); setPositiveRatedView(); 第(1)页,以““””号填列; }); $(“#neutralIcon”)。单击(函数(){ 警惕(“中立”); setNeutralRatedView(); 费率页(0,“”); }); $(“unlikeIcon”)。单击(函数(){ 警觉(“不像”); setNegativeRatedView(); //mkMPopClc('NegativeRatingReason',200,300,0,0); }); $(“”.updateRate“”)。单击(函数(){ setNotRatedView(); }); 函数setNotRatedView(){ $(“divNeutralRating”).fadeOut(); $(“#div”).fadeOut(); $(“divAskForRating”).fadeIn(); $(“#div”).fadeOut(); } 函数setPositiveRatedView() { $(“#div”).fadeOut(); $(“divNeutralRating”).fadeOut(); $(“divAskForRating”).fadeOut(); $(“#div”).fadeIn(); } 函数setNegativeRatedView(){ $(“divNeutralRating”).fadeOut(); $(“#div”).fadeOut(); $(“divAskForRating”).fadeOut(); $(“#div”).fadeIn(); } 函数setNeutralRatedView(){ $(“#div”).fadeOut(); $(“#div”).fadeOut(); $(“divAskForRating”).fadeOut(); $(“divNeutralRating”).fadeIn(); } 功能评级页面(评级、评论) { //警报(评级+评论); var contentKey=$(“”#contentKey“”).val(); var hasRated=$(“hasRated”).val(); var ratingId=$(“ratingId”).val(); } }); "); SetTrackingCookie(); builder.Replace(“#ContentKey”,this.ContentKey); builder.Replace(“#PositiveRateIconPath”,此.PositiveRateIconPath); builder.Replace(“#NeutralRateIconPath”),this.NeutralRateIconPath); builder.Replace(“#NegativeRateIconPath”),this.NegativeRateIconPath); builder.Replace(“#问题”,contentRatingSettings.Question); builder.Replace(“#PositiveAnswerMessage”,contentRatingSettings.PositiveAnswerMessage); builder.Replace(“#NeutralAnswerMessage”,contentRatingSettings.NeutralAnswerMessage); builder.Replace(“#NegativeAnswerMessage”,contentRatingSettings.NegativeAnswerMessage); 输出.写入(生成器); } }

提前感谢,

根据您想要执行的操作,最快的选项将是向图像控件添加
runat=“server”
标记以及指向函数的单击事件处理程序

这将在每次单击图像时执行回发到asp页面

如果不需要回发,则需要连接javascript以执行基于ajax的回发和适当的客户机/服务器处理程序