Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

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
0
friskyweasel
Asked:
friskyweasel
  • 5
  • 4
1 Solution
 
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
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now