3

I have a string in my main body that takes from an array. I want to pass that to the click event on a linkbutton. Is there any way to do this? Any help much appreciated. Example code below:

(main body)

string myLabelsName = column.ToString();

LinkButton myButton = new LinkButton();
myButton.Text = ("THIS IS MY BUTTON");
myButton.Click += new System.EventHandler(myButton_Click);

(event)

protected void myButton_Click(object sender, EventArgs e)
    {
        I WANT THE STRING 'myLabelsName' Here <<
        Response.Redirect(myLabelsName + ".aspx");
    }
0

1 Answer 1

6

You can use fields/properties or more appropriate here: the LinkButton's CommandName property.

string myLabelsName = column.ToString();
LinkButton myButton = new LinkButton();
myButton.Text = "THIS IS MY BUTTON";
myButton.CommandName = myLabelsName;
myButton.Click += new System.EventHandler(myButton_Click);

// ...

protected void myButton_Click(object sender, EventArgs e)
{
    LinkButton btn = (LinkButton) sender;
    string myLabelsName = btn.CommandName;
    Response.Redirect(myLabelsName + ".aspx");
}

By the way, you're using a LinkButton, why don't you use it's PostBackUrl property directly?

Sign up to request clarification or add additional context in comments.

1 Comment

@user3809554: no, why do you want that?

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.