如何使用动态创建的按钮调用ajax模式弹出窗口?

时间:2014-02-13 00:37:23

标签: c# asp.net ajax modalpopupextender

我用数据库中的值动态创建按钮,我有一个使用ajax的modalpopup。我知道在使用弹出窗口时我必须设置一个目标,当它是一个预定义的按钮时,这很好。但是如何动态创建modalpopup来识别目标,而不必使用stringbuilder并在事件处理程序上重建modalpopup,如果我还没有使用stringbuilder,那么如何在创建时调用它? 这是按钮的代码

button.ID = Convert.ToString(myID);
button.Text = myStr;
button.PostBackUrl = "~/WebForm2.aspx?SubjectID=" + myID;
button.CssClass = "DynamicButtonOverlay";

这是modalpopup

<asp:Panel ID="MyPanel" runat="server" CssClass="myModalPopup">
        <table style="width:100%;">
            <tr>
                <td>Select News</td>
            </tr>
            <tr>
                <td>
                    <asp:DropDownList ID="ddNewsToDelete" runat="server" CssClass="form-control"></asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnDeleteNews" runat="server" Text="Radera" CssClass="btn btn-danger pull-left" OnClick="btnDeleteNews_Click" CausesValidation="False" />
                    <asp:Button ID="btnClose" runat="server" Text="Close" class="btn btn-success pull-right" />
                </td>
            </tr>
        </table>
</asp:Panel>
    <asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" PopupControlID="MyPanel" BackgroundCssClass="myModalBackground" TargetControlID="Submit1" OkControlID="btnClose"></asp:ModalPopupExtender>

这是生成按钮的代码..

test.GetSubjects();
        int subjectid = 0;

        // Current row count.
        int rowCtr;// = 0;
        // Total number of cells per row (columns).
        int cellCtr;
        // Current cell counter.
        int cellCnt;

        //count number of rows in dataset
        int rN = test.dsSubjects.Tables[0].Rows.Count;

        cellCnt = 4;


        for (rowCtr = 1; rowCtr <= rN; rowCtr++)
        {
            // Create a new row and add it to the table.
            TableRow tRow = new TableRow();
            Table1.Rows.Add(tRow);

            for (cellCtr = 1; cellCtr <= 4; cellCtr++)
            {
                //
                Button button = new Button();
                //
                HyperLink link = new HyperLink();
                // Create a new cell and add it to the row.
                TableCell tCell = new TableCell();


                /* If the rowcounter is equal to the record numbers
                 * then it has to break because if not it will throw an error
                 * saying that there is no row at ending position */

                if (rowCtr == rN)
                    break;

                string myStr = test.dsSubjects.Tables[0].Rows[rowCtr - 1]["SubjectName"].ToString();
                int myID = Convert.ToInt32(test.dsSubjects.Tables[0].Rows[rowCtr - 1]["SubjectID"].ToString());

                button.ID = Convert.ToString(myID);
                button.Text = myStr;
                button.PostBackUrl = "~/WebForm2.aspx?SubjectID=" + myID;
                button.CssClass = "DynamicButtonOverlay";
                tCell.Controls.Add(button);

                tCell.CssClass = "DynamicButtonOverlay";
                tRow.Cells.Add(tCell);
                rowCtr++;
                /* If the cellcount is 3 then it needs to break, if not then 
                 * you'll miss every 4rth record, don't know why. But this works */

                if (cellCtr == 4)
                {
                    rowCtr = rowCtr - 1;
                    break;
                }
            }

        } 

0 个答案:

没有答案