Buttons on click event, inside an update panel, re

2019-09-03 10:41发布

问题:

I am developing a custom control which renders (a) textbox(es), in its textchange event I want to asynchronously send the value to the control where it would search and make the data available in an exposed property to be fetched later on page load. But my problem is, HOW TO ASYNCHRONOUSLY CALL A METHOD CODED INSIDE MY CONTROL to do the search functionality. For now, the textbox textchange and the buttons click events are not firing, the button click loads the page and writing in the textbox does nothing. Here is the code.

    public int Count { get; set; }
    public List<object> Selection { get; set; }
    public object SelectedObject { get; set; }
    public Label label;
    public TextBox textBox;
    protected override void RenderContents(HtmlTextWriter output)
    {
        textBox = new TextBox();
        textBox.TextChanged += new EventHandler(Method);
        UpdatePanel up = new UpdatePanel();
        up.ID = "UpdatePanel1";
        up.ChildrenAsTriggers = true;
        up.UpdateMode = UpdatePanelUpdateMode.Conditional;
        up.ContentTemplateContainer.Controls.Add(a);
        this.label = new Label();
        this.label.Text = "Initialized Text.";
        up.ContentTemplateContainer.Controls.Add(this.label);
        Button button = new Button();
        button.Text = "Say Hello";
        button.Click += new EventHandler(HandleButtonClick);
        up.ContentTemplateContainer.Controls.Add(button);
        this.Controls.Add(up);
        foreach (Control c in this.Controls)
        {
            c.RenderControl(output);
        }
    }


    private List<object> Get(string searchValue)
    {
        throw new NotImplementedException();
    }
    public void Method(object sender, EventArgs e) {
        (sender as TextBox).Text += " ";
        //Selection = Get((sender as TextBox).Text);
    }
    private void HandleButtonClick(object sender, EventArgs e)
    {
        this.label.Text = "Hello " + this.textBox.Text;
        //Selection = Get(this.textBox.Text);
    }

回答1:

found out the you have to inherit your control from IPostBackDataHandler, also, the asynchronous problem is there. I have used a workaround, I have put the control in updatepanel (put updatepanel on aspx), registered a button through control onclick of which is bound, on jquery/javascript I am firing the click event of the button to send the calls asynchronously. Things work fine, but after firing that event javascript code doesnt run again, tried calling the function by registering client script in prerender again, didnt work.