Get the Control ID of ImageButton that Raised PostBack in CodeBehind file of ASP.Net?

Posted by Divs99 under C# on 9/3/2012 | Points: 10 | Views : 5912 | Status : [Member] | Replies : 6
Get the ImageButton Control ID that Raised PostBack in CodeBehind file of ASP.Net?

Normally we can get the name of the control which causes the page post back by the following

this.Page.Request.Form["__EVENTTARGET"]

But this will not be applicable for controls like Asp Image Button...iam getting this.Page.Request.Form["__EVENTTARGET"] as blank for Image button?

onw way we can do it by using below:

this.Page.Request.Form is a string collection we can loop through the collection
and check whether image button control id exist in the list or not.

But i dont want in this way ..every time i have to loop all controls and check image button control id.....

is there any other way to get ImageButton Control ID that Raised PostBack in CodeBehind file of ASP.Net?




Responses

Posted by: Vasanthmvp on: 9/3/2012 [Member] Starter | Points: 25

Up
0
Down
Hi, i think this might help you.
You can find the client id of img button from the code behind


aspx page:

<asp:LinkButton ID="linkbuttonID" runat="server" Text="GetId" PostBackUrl="~/ControlIdoflinkbutton.aspx" />
<asp:ImageButton PostBackUrl="~/ControlIdoflinkbutton.aspx" OnClick="method" ID="ImageButtonId" runat="server" ImageUrl="~/CF_Logo.jpg" />

.aspx.cs


protected void Page_Load(object sender, EventArgs e)
{
Response.Write(this.Page.Request.Form["__EVENTTARGET"]); // Here we get the id of Link Button.
}

protected void method(object sender, EventArgs e)
{
string x = this.Page.FindControl("ImageButtonId").ClientID; // Here we are finding the control of image button and adding the event to it.
Response.Write(x);
}

If this is not appropriate answer to your question, let me know.

Regards,

Awesome Coding !! :)

Divs99, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Divs99 on: 9/3/2012 [Member] Starter | Points: 25

Up
0
Down
I want for Image button not for link button......

iam getting blank if i use liek this

this.Page.Request.Form["__EVENTTARGET"]);

iam not getting image button id.

Divs99, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vasanthmvp on: 9/3/2012 [Member] Starter | Points: 25

Up
0
Down
Hey, its working fine for me dude. Here ControlIdoflinkbutton is my .aspx Page name, ~/CF_Logo.jpg is my image soruce.

You copy the same code in to this page. If we click on the link button it displays the link button id from code behind.
If you click on the image button, image button id will be rendered on to the screen.

Regards,

Awesome Coding !! :)

Divs99, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Divs99 on: 9/3/2012 [Member] Starter | Points: 25

Up
0
Down
mine is image button in aspx page
<asp:ImageButton ID="btnAdd" runat="server" OnClick="btnAdd_Click" ImageUrl="~/_layouts/Images/rtArrow.gif" /><br />

in code behind page_load just i kept

string id=this.Page.Request.Form["__EVENTTARGET"]);

iam getting blank in string....




Divs99, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vasanthmvp on: 9/3/2012 [Member] Starter | Points: 25

Up
0
Down
The Request.Form has a http collection values. _EventTarget, _EventArgument, _target, _viewstate, etc.

_EventTarget is taking the id of LinkButton only. Its not taking the control id of image button as value for _EVENTTARGET. that is why we are getting a blank value.

Regards,

Awesome Coding !! :)

Divs99, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Divs99 on: 9/3/2012 [Member] Starter | Points: 25

Up
0
Down
how to get id for image button?pls help me out

Divs99, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response