简体   繁体   English

我如何将我的imageButton值传递给C#函数

[英]How can i pass my imagebutton values to c# function

I keep a variable named id and it keeps the value which comes from db (id = row[3].ToString();) 我保留了一个名为id的变量,它保留了来自db的值(id = row[3].ToString();)

Then I assign the id of class yildiz to 'id' then I try to pass the c# function but I cannot do it. 然后,我将类yildiz的id分配给“ id”,然后尝试传递c#函数,但我做不到。

When in the click event value of id is "id" (the code I try is ( string id = objImage.UniqueID; objImage.ID )) but actually it must be such as "26" the value which comes from db but it does not. 在点击事件中,id的值为“ id”(我尝试的代码为( string id = objImage.UniqueID; objImage.ID )),但实际上它必须为诸如“ 26”,该值来自db,但它确实不。

Actually my problem handling the data in c# side. 实际上我在c#端处理数据时遇到问题。 I tried also commandarguments but I cannot do it there is any way to do it how can i solve the problem? 我也尝试过指挥官论点,但我做不到,有什么办法可以解决问题? thanks.. 谢谢..

        id = row[3].ToString();
   %>

<div class="sag-re">-
            <div class="sag-re-baslik" runat="server"><asp:HyperLink ID=baslik runat="server">HyperLink</asp:HyperLink></div>
                <!--<div class="sag-re-resim"><img src="images/rss-image.jpg" width="120" height="120"  /></div>-->
            <div class="sag-re-icerik" id = icerik runat="server">Lorem ipsum dolor sit amet, , arcu nec viverra mollis, turpis neque feugiat massa, non dapibus neque nunc ac orci. </div>
            <div class="oy-verme">
                 <!-- <body onload="getDivs()"> -->

           <!--  <input type="image"
            src="images/yildiz.png" 
            ID="ImgButton"                                
                runat="server" OnServerClick="ImgButton_ServerClick" />-->
                <% for (j = i * 5 + 1; j < (i *5)+6; j++)
        {

            //Imageid = "Img" + j;

                       %>
                        <div class="yildiz"><asp:ImageButton ID= id  runat="server" Height="19px" 
                        ImageUrl="~/images/yildiz.png"  OnClick="ImageButton2_Click" Width="20px" 
                        style="position: relative; top: 13px; left:6px; float:left; "   /></div>


       <% } %>

It is c# side 是C#面

protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton objImage = (ImageButton)sender;
        string id = objImage.UniqueID;

     /*   if (e.CommandName == "Comment")
        {
            string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });
            string scrapid = commandArgs[0];
            string uid = commandArgs[1];
        } */


        //  string s= Imageid.UniqueID.ToString();
        //this.baslik2.Text = s;

    }

you are setting the ID=id, which is why I think you are reading it back as a string of "id"...I think what you actually want to do is set it like this: 您正在设置ID = id,这就是为什么我认为您将其读回为字符串“ id”的原因...我认为您实际想要做的是这样设置它:

ID=<%=id%> ID = <%= id%>

EDIT: 编辑:

yes, it doesn't work as I expected it to in this case... 是的,在这种情况下,它无法正常运行...

I reworked it to the following: 我将其重新整理为以下内容:

<asp:Panel id="test" class="yildiz" runat="server"></asp:Panel>

and page_load: 和page_load:

protected void Page_Load(object sender, EventArgs e)
{
    var i = 2;

    var id = 2;

    for (var j = i * 5 + 1; j < (i *5)+6; j++)
        {

                var imagebutton = new ImageButton();
                imagebutton.ID = id.ToString( );
                imagebutton.ImageUrl = "https://www.google.com/logos/2012/juan_gris-2012-hp.jpg";
                imagebutton.Style.Add("position","relative");
                imagebutton.Style.Add("top","13px");
                imagebutton.Style.Add("left","6px");
                imagebutton.Style.Add("float","left");
                test.Controls.Add(imagebutton);

        }
}

however, that has this error: 但是,有此错误:

Multiple controls with the same ID '2' were found. 找到多个具有相同ID“ 2”的控件。 FindControl requires that controls have unique IDs. FindControl要求控件具有唯一的ID。

HOWEVER, THIS WILL WORK: 但是,它将起作用:

protected void Page_Load(object sender, EventArgs e)
{
    var i = 2;

    var id = 2;

    for (var j = i * 5 + 1; j < (i *5)+6; j++)
        {

                var imagebutton = new ImageButton();
                imagebutton.Attributes.Add("custom-id", id.ToString( ));
                imagebutton.ImageUrl = "https://www.google.com/logos/2012/juan_gris-2012-hp.jpg";
                imagebutton.Style.Add("position","relative");
                imagebutton.Style.Add("top","13px");
                imagebutton.Style.Add("left","6px");
                imagebutton.Style.Add("float","left");
                imagebutton.Click += new ImageClickEventHandler(imagebutton_Click);
                test.Controls.Add(imagebutton);

        }
}

void imagebutton_Click(object sender, ImageClickEventArgs e)
{
    ImageButton objImage = (ImageButton)sender;
    objImage.Attributes["custom-id"];
}

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

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