Min Hong Tan
Min Hong Tan

Reputation: 545

asp.net button/linkbutton webcontrol dynamically added in bootstrap modal body doesn't postback

I have added linkbutton inside html table dynamically and add into bootstrap modal's body. (linkbutton has coded linkbutton.click += new eventhandler(Eventclick1);)

enter image description here

but, when I click on select, it won't go to my function Eventclick1. It just refreshes the whole page. (it is already inside updatepanel). Anyways I can make the select button to postback? (I don't want to add client side click function like onclientclick = $('#otherbutton').click(); )

UPDATE

lnk_button.ID = this.ID + "AuditSelectedRow_" + Convert.ToString(l_loop); 
lnk_button.Click += new EventHandler(OnAuditRowSelected);
lnk_button.Text = "Select"; 
WebControl wc_tdSelect = new WebControl(HtmlTextWriterTag.Td); 
wc_tdSelect.Controls.Add(lnk_button);

Upvotes: 20

Views: 2204

Answers (5)

user7488658
user7488658

Reputation: 116

This the following inside pageload:

ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(this.OnAuditRowSelected);

Upvotes: 0

prashantchalise
prashantchalise

Reputation: 506

First make sure that your custom webcontrol inside Updatepanel still exist at the end of page life cycle. I assume you are calling a function where you are adding linkbutton to the webcontrol. something like this:

// Custom function Creating link buttons

private void CreateControls() {

// Create your link buttons here.

}

Now try calling the same function again inside page pre-init method which ensures that the control still exist at the time of button click event. something like this:

//Page Pre Init
protected void Page_PreInit(object sender, EventArgs e)
{
CreateControls();
}

Make sure you have your web control added to updatepanel inside the same function as listed above. Here is a sample code attaching webcontrol to the updatepanel.

yourUpdatePanel.ContentTemplateContainer.Controls.Add(wc_tdSelect);

I am sure you will have your desired result this time :)

Upvotes: 1

RezaGhahari
RezaGhahari

Reputation: 413

You can use the ASP Button like in your example

   <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <!-- Modal -->
                    <asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
                    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
                        aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                        <span aria-hidden="true">&times;</span></button>
                                    <h4 class="modal-title" id="myModalLabel">
                                        Modal title</h4>
                                </div>
                                <div class="modal-body">
                                    <asp:TextBox ID="TextBox1" runat="server" placeholder="First Name" class="form-control"></asp:TextBox><br />
                                    <asp:TextBox ID="TextBox2" runat="server" placeholder="Middle Name" class="form-control"></asp:TextBox><br />
                                    <asp:TextBox ID="TextBox3" runat="server" placeholder="Last Name" class="form-control"></asp:TextBox><br />
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">
                                        Close</button>
                                    <%--<button type="button"  class="btn btn-primary">
                                        Save changes</button>--%>
                                    <asp:Button Text="Save" OnClick="Submit" runat="server" />
                                </div>
                            </div>
                        </div>
                    </div>
                </ContentTemplate>
            </asp:UpdatePanel>

Upvotes: 0

Zeeshan Umar
Zeeshan Umar

Reputation: 502

Generally you should be avoiding the dynamic controls, you should add button in design time inside div and show/hide that div on client side for popup.

Have a look at this thread:-

stackoverflow

Upvotes: 0

DaniDev
DaniDev

Reputation: 2631

I have run into similar issue with modal pop-ups and the problem is basically (as stated similarly above), is the sequence of when the asp.net control is rendered verses when events and/or JS functions get registered.

One way to solve is to render an HTML control manually so that you can control it's name and when it gets rendered.

Upvotes: 0

Related Questions