0

I am working on a ASP.NET site, in which i display results returned by internal search engine to the end user. I am parsing the result creating HTML string to display those results as following

        foreach (XmlNode result in results)
    {
        srHtml = "<li class=\"\" style=\"\">";
        srHtml += "<a href=\"" + result.SelectSingleNode("./web:Url", nsmgr).InnerText + "\"> <span class=\"title\">" + result.SelectSingleNode("./web:Title", nsmgr).InnerText + "</span></a> <button>Short This URL</button>\n";
        if (result.SelectSingleNode("./web:Description", nsmgr) != null)
            srHtml += "<br />" + result.SelectSingleNode("./web:Description", nsmgr).InnerText + "<br />";
        srHtml += "<span class=\"url\">" + result.SelectSingleNode("./web:Url", nsmgr).InnerText + "</span></li>\n";
        phResults.Controls.Add(new System.Web.UI.LiteralControl(srHtml));

    }

I can see that button on result page. But I dont know where can I put the code that run when user clicks on this button. When User clicks on that button, I want to change that button textfield with shorten url.

Can anyone help me out here ?

Regards, Sumit Lonkar

1 Answer 1

1

What about doing it this way?:

Button button = new Button();
button.ID = "Button1";
button.Click += new EvandHandler(Button1_Click);
phResults.Controls.Add(button);

And your event handdler:

protected void Button1_Click(object sender, EventArgs e)
{
     //Event handler code here
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot. Btw how can I pass argument to this one ? I want to replace this button by text field containing specific text, which I will pass to it as argument.
I just tested this code with simple console.writeline("test"); eventhandler is not getting called...
@Sumit Lonkar: If you're creating this button dynamically through code behind, make sure to recreate the the button with the above code after each postback. Use OnInit or OnPreInit to recreate the controls. Do that and then test the event handler again.

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.