Pass variable to event handler

hi all - i'm dynamically creating an Image Button, and defining an Event Handler for its click event....when the image button is clicked, i need to be able to pass a local variable to the event handler, but i'm not sure how to proceed
(C#)
//add image button to table cell
string rd = "SomeUniqueValue"; // <----   need to pass this variable (rd) to event handler
HtmlTableCell imgbtn_cell = new HtmlTableCell();
ImageButton MyImgButton = new ImageButton();
MyImgButton.ImageUrl      = "someimage.gif";
MyImgButton.ID = "imgbtn_" + rd;
MyImgButton.Click += new System.Web.UI.ImageClickEventHandler(this.MyImgButton_Click);
imgbtn_cell.Controls.Add(MyImgButton);

private void MyImgButton_Click(object sender,System.Web.UI.ImageClickEventArgs e)
{
//need to have variable rd passed here
string MyPassedString = rd;
}

what would also work is if once i'm in the event handler, i could obtain the MyImgButton.ID  property of the image button that was just clicked - since the rd string var is appended to the ID string, I could then parse the ID string to obtain the value for rd - is there a simple way to do this? i'm open to alternative suggestions
friskyweaselAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

apb2Commented:
I would try (excuse my c#)

private void MyImgButton_Click(object sender,System.Web.UI.ImageClickEventArgs e)
{
//need to have variable rd passed here
ImageButton myButton = (ImageButton)sender;
//The use the ID as such myButton.ID;

}


HTH

apb
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
friskyweaselAuthor Commented:
what can i say apb2 u are the man - that works perfectly for me

i'm assigning you the points as this solves my problem - but...if i were in a situation where i had to have an actual variable value passed to the event handler, and couldn't get what i needed by accessing the ID property of the sender control...what would be the easiest way to accomplish that? just curious

again thanks a million

0
apb2Commented:
I am just "a" man. not "the" man.

Firstly i would do try:

ImageButton MyImgButton = new ImageButton();
MyImgButton.ImageUrl     = "someimage.gif";
MyImgButton.ID = "imgbtn_" + rd;
MyImgButton.CommandArgument = "Check this text";
MyImgButton.Click += new System.Web.UI.ImageClickEventHandler(this.MyImgButton_Click);

private void MyImgButton_Click(object sender,System.Web.UI.ImageClickEventArgs e)
{
string myText = Convert.ToString(e.CommandArgument);
//myText wil now equal "Check this text"
}

Or personally would probably cheat and something in the view state. That would be easiest. but don't tell my boss

HTH aswell mate

Cheers for the points

apb2
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

friskyweaselAuthor Commented:
: ) - very good -thanks a lot!
 
0
apb2Commented:
So you could:

ImageButton MyImgButton = new ImageButton();
MyImgButton.ImageUrl     = "someimage.gif";
MyImgButton.CommandArgument = rd;
MyImgButton.Click += new System.Web.UI.ImageClickEventHandler(this.MyImgButton_Click);


private void MyImgButton_Click(object sender,System.Web.UI.ImageClickEventArgs e)
{
string rd = Convert.ToString(e.CommandArgument);
}

apb2
0
friskyweaselAuthor Commented:
hey apb2 - again - just out of curiosity - i actually just tried that latest example and in my event handler when i enter this:

string rd = Convert.ToString(e.CommandArgument);

im' getting a  'System.Web.UI.ImageClickEventArgs' does not contain a definition for 'CommandArgument' error

i'm defining the commandargument property and assigning it the value of the rd variable before passing to event handler as you suggested....now my curiosity is up : ) any idea why i'm getting that error?

0
apb2Commented:
Yes sorry, My fault. In a repeateing control a similar method could be used

You will have to do the same

ImageButton MyImgButton = new ImageButton();
MyImgButton.ImageUrl     = "someimage.gif";
MyImgButton.CommandArgument = rd;
MyImgButton.Click += new System.Web.UI.ImageClickEventHandler(this.MyImgButton_Click);


private void MyImgButton_Click(object sender,System.Web.UI.ImageClickEventArgs e)
{
ImageButton myButton (ImageButton)sender;
string rd = myButton.CommandArgument;
}

Should work

Sorry for the confusion

apb2
0
friskyweaselAuthor Commented:
yes apb just tested that and it works perfectly - i should have seen that too....i think i like that 2nd method better - feels cleaner than parsing thru the ID property....once again thanks!
0
apb2Commented:
I agree

Glad to help. I am having a very dull day a work.

apb2
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.