如何在后面的代码中获取控件ID [英] How to get control id in Code behind
本文介绍了如何在后面的代码中获取控件ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的按钮代码
Hi,
This is my button code
<asp:ImageButton ID="Img1" ImageUrl='<%#Eval("ProductImage") %>' runat="server" Width="30px" Height="30px" OnClick="MakeImg1Main" />
像这样,我的代码中没有imagebuttons,
我的目的是要知道单击了哪个按钮.
如何获取后面代码中的单击控件ID.
请帮帮我
在此先感谢
Like this i have no.of imagebuttons in my code,
my intension is to know which button is clicked.
how to get the clicked control id in code behind.
Please help me
Thanks in advance
推荐答案
如果您的事件代码是这样的,
直接事件.
Hi,
if your event code is like this,
for direct event.
protected void MakeImg1Main(object sender, EventArgs e)
{
//Then try like this.
string ID = ((ImageButton)sender).ID;
string CLientID = ((ImageButton)sender).ClientID;
}
与数据列表一起,为数据列表添加项目命令事件.
添加命令名称
with datalist, add item command event for datalist.
add the command name
<asp:ImageButton ID="Img1" ImageUrl='<%#Eval("ProductImage") %>' runat="server" Width="30px" Height="30px" CommandName="Image" />
//In ItemCommand Event,
if(e.CommandName == "Image")
{
string ID = ((ImageButton)e.CommandSource).ID;
string CLientID = ((ImageButton)e.CommandSource).ClientID;
}
希望对您有所帮助.
Hope it helps.
您好,
将点击事件作为MakeImg1Main
授予所有图像按钮,并从点击事件类型中将您的发件人投射到图像按钮并获取其ID ...
就像
Hi,
Give click event asMakeImg1Main
to all imagebuttons and from the click event type cast your sender to imagebutton and get its id...
like
private void MakeImg1Main(object sender,EventArgs e)
{
ImageButton img=(ImageButton)sender;
// this will get you the id of the image button clicked
Response.Write(img.Id.ToString());
}
希望对您有帮助.
克鲁纳尔(Krunal)
Hope it helps..
Krunal
这篇关于如何在后面的代码中获取控件ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文