C # Dynamically created CommandButton Command event handler

So, I have a strange situation ... I have a System.Web.UI.WebControls.WebParts.EditorPart class. It displays the Search button, when you click this button, the clickHandler method searches the database and dynamically creates a LinkButton for each row returned, sets the CommandName and CommandArgument properties and adds the CommandEventHandler method, and then adds the LinkButton control to the page.

The problem is that when you click LinkButton, its CommandEventHandler method is never called, it looks like the page just returns messages to where it was before the ORIGINAL Search button was clicked.

I saw publications saying that you need to add event handlers to OnLoad () or some other early method, but my LinkButtons were not even created until the user told us what to look for and click on "Search", button ... Any ideas on how to handle this?

Thank!

+3
source share
4 answers

This is my favorite trick :)

Our scenario is to first render the control. Then, using some input from the user, visualize additional controls and ask them to respond to events.

- - , PostBack, ViewState. ; ViewState LoadViewState(), , .

, LoadViewState() SaveViewState(), .

( , , , , )

private string searchQuery = null;

private void SearchButton(object sender, EventArgs e)
{
    searchQuery = searchBox.Text;
    var results = DataLayer.PerformSearch(searchQuery);
    CreateLinkButtonControls(results);
}

// We save both the base state object, plus our query string.  Everything here must be serializable.
protected override object SaveViewState()
{
    object baseState = base.SaveViewState();
    return new object[] { baseState, searchQuery };
}

// The parameter to this method is the exact object we returned from SaveViewState().
protected override void LoadViewState(object savedState)
{
    object[] stateArray = (object[])savedState;

    searchQuery = stateArray[1] as string;

    // Re-run the query
    var results = DataLayer.PerformSearch(searchQuery);

    // Re-create the exact same control tree as at the point of SaveViewState above.  It must be the same otherwise things will break.
    CreateLinkButtonControls(results);

    // Very important - load the rest of the ViewState, including our controls above.
    base.LoadViewState(stateArray[0]);
}
+3

onload, .

+1
 LinkButton link= new LinkButton();
 link.Command +=new CommandEventHandler(LinkButton1_Command);

 protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
    try
    {
        System.Threading.Thread.Sleep(300);
        if (e.CommandName == "link")
        {
           //////////
        }
    }
    catch
    {

    }
}
0

The dirty hack I just came up with is to create dummy LinkButtons with the same identifiers as real buttons. So, let's say you are going to create LinkButton "foo" in Pre_Render (which is too late), and then create a dummy foo in Page_Load:

        var link = new LinkButton();
        link.ID = "foo";
        link.Click += fooEventHandler;
        dummyButtons.Controls.Add(link);

(Where "dummyButtons" is just a PlaceHolder on the page with visibility set to false). It is ugly, but it works.

-1
source

Source: https://habr.com/ru/post/1697584/


All Articles