NoviceToDotNet
NoviceToDotNet

Reputation: 10815

How to set the target control id of ajax modelpop up extender to a run time button created id?

I am creating a button at run time and on it's click i want to open a ajax model pop up. But i am unable to set model pop up's target control id to this run time created button id. Could some body suggest me how to achieve this? or any alternate way exist?

My code is as following. This is how i am creaing a run time button.

 protected void grdSurveyMaster_ItemCreated(object sender, GridItemEventArgs e)
    {

        if (e.Item is GridFooterItem)
        {
            GridFooterItem footerItem = (GridFooterItem)e.Item;
            // RadScriptManager1.RegisterAsyncPostBackControl(btn);
            Button btn = new Button();
            btn.Text = "Take a new survey";
            btn.CommandName = "newAssess";
            btn.Click += new System.EventHandler(grdMasterbtnClick);
            footerItem.Cells[2].Controls.Add(btn);
            //ModalPopupExtender1.TargetControlID = "btn";// Convert.ToString(Page.FindControl(Convert.ToString(btn.ClientID)));

        }
    }

And following is my HTMl

<asp:UpdatePanel ID="updatepanel1" runat="server">
        <ContentTemplate>
            <cc1:ModalPopupExtender CancelControlID="btnCancel" PopupControlID="modelPopUp" ID="ModalPopupExtender1"
                runat="server" TargetControlID="btnDefault">
            </cc1:ModalPopupExtender>
            <asp:Button ID="btnDefault" runat="server" Visible="false" />
            <asp:Panel ID="modelPopUp" runat="server" Visible="false" BackColor="AliceBlue">
                <p>
                    These items will be permanently deleted and cannot be recovered. Are you sure?
                </p>
                <asp:Button ID="btnOk" Text="OK" runat="server" />
                <asp:Button ID="btnCancel" Text="Cancel" runat="server" />
            </asp:Panel>
        </ContentTemplate>
    </asp:UpdatePanel>

Upvotes: 0

Views: 1822

Answers (1)

Novitchi S
Novitchi S

Reputation: 3741

Well you're doing right setting the Popup's TargetControl an invisible unused button. Now the best way to show/hide your popup is from Javascript. For this you have to set the behaviorid="someString" of your ModalPopupExtender and create a javascript function like this:

function ShowModalPopup(behaviourId) {
$find(behaviourId).show();
}

Then you can assign the javascript function to a button:

btn.OnClientClick = String.Format("ShowModalPopup('{0}')", 
    ModalPopupExtender1.behaviorid);

Upvotes: 1

Related Questions