如何将我的imagebutton值传递给c#函数

时间:2012-03-23 12:06:19

标签: c# asp.net onclick

我保留一个名为id的变量,它保留了来自db (id = row[3].ToString();)

的值

然后我将类yildiz的id分配给'id'然后我尝试传递c#函数但是我不能这样做。

当在click事件中,id的值是“id”(我尝试的代码是(string id = objImage.UniqueID; objImage.ID))但实际上它必须是诸如“26”之类的值 来自db,但它没有。

实际上我的问题是在c#侧处理数据。我也尝试了命令参数,但我不能这样做有任何办法,我怎么能解决这个问题?感谢..

        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>


       <% } %>

是c#side

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;

    }

1 个答案:

答案 0 :(得分:1)

你正在设置ID = id,这就是为什么我认为你正在把它作为一串“id”读回来...我觉得你真正想做的就是这样设置:

ID = LT;%= ID%&GT;

编辑:

是的,它在我的预期中不起作用......

我将其改编为以下内容:

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

和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);

        }
}

然而,这有错误:

  

找到具有相同ID“2”的多个控件。的FindControl   要求控件具有唯一ID。

然而,这将起作用:

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"];
}