添加按钮面板动态和获取它的父ID [英] Adding button to panel dynamically and getting it's parent ID

查看:102
本文介绍了添加按钮面板动态和获取它的父ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个网页,我给管理员的改变照片的信息(如标题,描述等),所有网页上的控件动态添加,因为我有照片多个画廊的一种方法。

I created a page where I give admin's a way to change photos info (e.g. Title, Description, etc) All the controls on the page are added dynamically because I have more than one gallery of photos.


  1. 面板 - >父

  1. panel --> parent.

按钮。

标题文本框。

说明文本框。

在每一个小组,我点击该按钮时,将更改的信息存放照片信息存储在服务器(Flickr的)。动态添加该按钮的Click事件,我想知道是否有可能获得我只是点击按钮的父。

In every panel, I have button that when clicked, sends the changed information to the server where the photo info is stored (Flickr). The click event for this button is added dynamically, and I want to know if is possible to get the parent of the Button I just clicked on.

下面是code其中添加我的所有控件:

Here is the code where I add all my controls:

 //global veriables (this is only part of the code)
 Panel panel;
 Button button;
 for (int i = 0; i < photo.Length; i++) {
     photo[i] = new FlickerImages(photoSet.MediumURLS[i], photoSet.ThumbnailURLS[i], photoSet.Titles[i], photoSet.Descreption[i]);

     panel = new Panel();
     panel.ID = "panel" + i;

     button = new Button();
     button.ID = "sendDataButton" + i;
     button.Text = "send data";
     button.Click += button_Click; //adding the event

     label = new Label();
     label.ID = "editLabel" + i;

     panel.Controls.Add(label);
     panel.Controls.Add(photo[i].CurrentImage(i)); //Image control
     panel.Controls.Add(photo[i].EditTitleTextBox(i)); //TextBox control
     panel.Controls.Add(photo[i].EditCommentTextBox(i)); //TextBox control
     panel.Controls.Add(button);

     Form.Controls.Add(panel);
}

下面是点击事件中,我添加到所有的按钮:

Here is the click event I add to all the buttons:

void button_Click(object obj, EventArgs e) {
    Response.Write(button.Parent.ID); // i get panel10 every time this get fired.
}

我知道这是可能使用jQuery,但有可能得到ASP.NET中的按钮ID?

I know this is possible with jQuery but is it possible to get the button ID in ASP.NET?

对不起,我的英语和感谢您的帮助。

Sorry for my English and thanks for the help.

推荐答案

不知道,但你要找的客户端ID 属性? ( button.Parent.ClientID

Not sure, but are you looking for the ClientID property? (button.Parent.ClientID)

编辑:

您应该引用发送按钮,在事件处理程序:

You should reference the sending button in the event handler:

void button_Click(object obj, EventArgs e)
{
    Response.Write(((Button)obj).Parent.ID); 
}

这篇关于添加按钮面板动态和获取它的父ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆