1

I create a button dynamically and I want when my button click and onclick event fired, a variable pass with this event.

Button btn = new Button();
btn.Click += new EventHandler(Button_Click);
//btn.Click += new EventHandler(Button_Click(32)); <-- I want something like this
divUserUploadedList.Controls.Add(btn);

And this is Onclick event:

protected void Button_Click(object sender, EventArgs e)
{
    //I want to access that value here
}
6
  • where does the value come from? Does it come from user input, or is it constant value - does the value ever change? Commented May 20, 2015 at 11:25
  • You can subscribe to an event, you cannot pass arguments. Why 32? Commented May 20, 2015 at 11:26
  • @user1666620 This value is id of a row from a table and yes its change Commented May 20, 2015 at 11:29
  • @TimSchmelter 32 is just example, Its id of some rows from a table. These buttons already have an event and they can successfully fire that event Commented May 20, 2015 at 11:31
  • 1
    you can do this through button's CommanArgument property. Commented May 20, 2015 at 11:31

2 Answers 2

3

You can do this by using CommandArgument property

protected void Page_Load(object sender, EventArgs e)
        {
            Button btn = new Button();
            btn.Click += btn_Click;
            btn.CommandArgument = "12"; //<-- you can pass argument like this
            divUserUploadedList.Controls.Add(btn);

        }

        void btn_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            string value = btn.CommandArgument;
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Yes tnx, That's what I need
2

What you are looking for is probably the CommandArgument property.

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument%28v=vs.110%29.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.