简体   繁体   English

C#如何调用imagebutton htmlgeneric控件中的.cs函数

[英]c# how can i call a function which is .cs function in imagebutton htmlgeneric control

I want to call a method when i click the imagebutton but I cannot add or call in htmlgenericcontrol. 我想在单击imagebutton时调用一个方法,但是无法添加或调用htmlgenericcontrol。 My code is below: 我的代码如下:

I tried this code: 我尝试了这段代码:

var duck = objRssItem;
objImage.Click += (s, e) => { WebForm4.amethod(objRssItem); }; 

But it does not work. 但这行不通。 I need to send objRssItem. 我需要发送objRssItem。

public HtmlGenericControl CreateDIV_OyVerme_Sub_Yildiz(string id, int subId, Rss.Items objRssItem)
{
        HtmlGenericControl objDiv = new HtmlGenericControl("div");
        objDiv.ID = strControlName_DivYildiz + id + "_" + subId;

        objDiv.Attributes.Add("class", strClassName_DivYildiz);
        //objDiv.Attributes.Add("runat", "server");

        ImageButton objImage = new ImageButton();
        objImage.Attributes.Add("runat", "server");

        //objImage.Src = strImgSrc_yildiz;
       //objImage.Click += new ImageClickEventHandler(WebForm4.ImageButtons_Click);


        objImage.ID = strControlName_ImageYildiz + id +"_" + subId;
        objImage.ImageUrl = strImgSrc_yildiz;
        objImage.OnClientClick = strOnClientClickFunc_yildiz;
       // objImage.Attributes.Add("OnClick","WebForm4.amethod (objRssItem);"); 
        objImage.Style.Add(HtmlTextWriterStyle.Height, "19px");
        objImage.Style.Add(HtmlTextWriterStyle.Width, "20px");
        objImage.Style.Add(HtmlTextWriterStyle.BorderWidth, "0px");
        objImage.Style.Add(HtmlTextWriterStyle.Position, "relative");
        objImage.Style.Add(HtmlTextWriterStyle.Top, "13px");
        objImage.Style.Add(HtmlTextWriterStyle.Left, "6px");
        objImage.Style.Add("float", "left");
        objImage.ToolTip = subId + "/" + 5;
        // calling the method 
       // objImage.Attributes.Add("OnClientClick", "return(GetRssID(objRssItem));");

        var duck = objRssItem;

        objImage.Click += (s, e) => { WebForm4.amethod(objRssItem); };
       //objImage.Click += WebForm4.amethod (objRssItem); 

        objDiv.Controls.Add(objImage);

        return objDiv;
   }

You cannot pass arguments on adding event-handlers. 您不能在添加事件处理程序时传递参数。

I would suggest to use the ImageButtons 's CommandArgument property for this purpose. 我建议为此使用ImageButtonsCommandArgument属性。 Note that RssItem cannot be used as CommandArgument as long as it is not a string since it must be rendered as HTML. 请注意, RssItem不能用作CommandArgument ,只要它不是字符串即可,因为它必须呈现为HTML。

Normally you would use the ID (or anything that identifies it). 通常,您将使用ID (或用于标识它的任何东西)。 Then you can use it in the event-handler to create an RssItem instance if you need it. 然后,如果需要,可以在事件处理程序中使用它来创建RssItem实例。

objImage.CommandArgument = objRssItem.ID;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM