在代码隐藏中添加ASP.Net按钮

时间:2016-08-04 19:14:13

标签: c# asp.net ajax

我有一个非常紧凑的表格,带有AJAX Accordion窗格。我使用的是Master页面,但ASPX子页面如下所示:

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
  <link rel="stylesheet" href="Content/themes/base/jquery-ui.css">
  <script src="Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>  
  <script src="Scripts/jquery-ui-1.8.24.min.js" type="text/javascript"></script>  

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="FeaturedContent" runat="server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="MainContent" runat="server">

    <asp:UpdatePanel ID="MyUpdatePanel" runat="server">
        <ContentTemplate>
            <asp:panel ID="MyContent" runat="server">

    <!--  ************************************  -->
                    <ajaxtoolkit:modalpopupextender id="MdlCommentsExtender" runat="server"     
                        targetcontrolid="MyContent" popupcontrolid="pnlComments" 
                        popupdraghandlecontrolid="PopupHeader" drag="True" 
                        backgroundcssclass="ModalPopupBG" Enabled="False"  >
                    </ajaxtoolkit:modalpopupextender>

                    <asp:panel id="pnlComments" style="display: none" runat="server" BackColor="White" CssClass="modalPopup">
                        <div class="HellowWorldPopup">
                            <div class="PopupHeader" id="Div3" style="border: thin solid #000000; vertical-align: middle; text-align: center; background-color: #C0C0C0; color: #000000; font-weight: bold; height: 40px;" ><br />Pend Comment</div>
                                <div class="PopupBody" style="background-color: #FFFFFF">
                                    <table style="width: 300px">
                                        <tr style="text-align:left">
                                            <td style="padding:4px"><asp:Label ID="lblCommentBox" runat="server" Text="Comment:"></asp:Label></td>
                                        </tr>
                                        <tr>
                                            <td style="padding:4px">
                                           <!--     <asp:TextBox ID="txtCommentBox_Old" runat="server" CssClass="textbox" TextMode="multiline" Wrap="True" Height="70px" Width="270px" Font-Size="Small"></asp:TextBox>  -->
                                                <asp:TextBox ID="txtCommentBox" TextMode="MultiLine" CssClass="textbox" Wrap="True" Height="70px" Width="270px" Font-Size="Small" Rows="3" runat="server"  onkeyup="textCounter(this, this.form.remLen, 50);" onkeydown="textCounter(this, this.form.remLen, 50);" onpaste="textCounter(this, this.form.remLen, 50);" />
                                            </td>
                                        </tr>  
                                        <tr>
                                            <td>
                                                <input readonly="readonly" type="text" id="remLen" name="remLen" size="2" maxlength="3" value="50" /> characters left
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                                <div class="Controls">
                                    <table style="width: 300px">
                                        <tr>
                                           <td style="vertical-align: middle; text-align: center"> <asp:Button ID="mdlCmntsOk_Click" runat="server" Text="OK" CssClass="textbox" Height="28px" Width="75px" OnClick="mdlCommentsOk_Click" /></td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        </div>  
                    </asp:panel>
            </asp:panel>
        </ContentTemplate>
    </asp:UpdatePanel>

    <!--  ************************************  -->

</asp:Content>

这个表单包含一个modalpopupextender,我想从&#34;编辑&#34;按钮。手风琴是通过代码隐藏动态添加的。我的整个代码隐藏看起来像这样:

public partial class frmBenefitSummaryList : System.Web.UI.Page
{
    string ConnStr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnCST"].ConnectionString;


    protected void Page_Load(object sender, EventArgs e)
    {

        BindAccordions();
        if (Page.IsPostBack)
        {
            MdlCommentsExtender.Show();
        }
    }

    private void BindAccordions()
    {
        #region Get the value from the database

         DataTable dt = new DataTable();   
         try   
         {   

             using (SqlConnection connection = new SqlConnection())   
             {
                 connection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnCST"].ToString();   
                 connection.Open();   
                 SqlCommand cmd = new SqlCommand();   
                 cmd.Connection = connection;
                 string CmdTxt = "Select CBL.ID, CBL.[Category], CBL.[Provision], CTL.MarkForReview, CTL.IssueType, CTL.Resolution, CTL.Feedback, CTL.TemplateID";
                 CmdTxt = CmdTxt + " from [tblCSTBenefitList] CBL";
                 CmdTxt = CmdTxt + " LEFT JOIN tblCSTTemplateList CTL";
                 CmdTxt = CmdTxt + " ON CBL.ID = CTL.BenefitID";
                 CmdTxt = CmdTxt + " where CBL.ID > '0'";
                 CmdTxt = CmdTxt + " ORDER BY CBL.[Category], CBL.[Provision] ASC";

                 cmd.CommandText = CmdTxt; 
                 cmd.CommandType = CommandType.Text;   
                 SqlDataAdapter da = new SqlDataAdapter(cmd);   

                 da.Fill(dt);   
                 cmd.Dispose();   
                 connection.Close();   
             }   
         }
         catch (Exception ex) { Response.Write(ex.Message); }

        #endregion

        #region Create accordion with properties

        Accordion acrDynamic = new Accordion();
        acrDynamic.ID = "MyAccordion";
        acrDynamic.SelectedIndex = -1;//No default selection   
        acrDynamic.RequireOpenedPane = false;//no open pane   
        acrDynamic.HeaderCssClass = "accordionHeader";//Header class   
        acrDynamic.HeaderSelectedCssClass = "accordionHeaderSelected";//Selected herder class   
        acrDynamic.ContentCssClass = "accordionContent";//Content class  

        #endregion

        #region Create the panes

        Label lbTitle;   
        Label lbContent;   
        AccordionPane pane;   
        string Content = "";


        for (int i = 0; i < dt.Rows.Count; i++)   
        {
            if (Content == "")
                Content += "<table class='hoverTable'><tr><th bgcolor='#5D7B9D' width='440px'><font color='#fff'>Provision</th><th bgcolor='#5D7B9D' width='120px'><font color='#fff'>Mark For Review</th><th bgcolor='#5D7B9D' width='120px'><font color='#fff'>Issue Type</th><th bgcolor='#5D7B9D' width='120px'><font color='#fff'>Resolution</th><th bgcolor='#5D7B9D' width='120px'><font color='#fff'>Feedback</th><th bgcolor='#5D7B9D' width='60px'><font color='#fff'>Edit</th></tr>";

            string BranchName = dt.Rows[i]["Category"].ToString();   
            string Next_Branch = "";   

            if (i != dt.Rows.Count - 1)   
                Next_Branch = dt.Rows[i + 1]["Category"].ToString();

            Content += "<tr>";
            Content += "<td>" + dt.Rows[i]["Provision"].ToString() + "</td>";
            Content += "<td>" + dt.Rows[i]["MarkForReview"].ToString() + "</td>";
            Content += "<td>" + dt.Rows[i]["IssueType"].ToString() + "</td>";
            Content += "<td>" + dt.Rows[i]["Resolution"].ToString() + "</td>";
            Content += "<td>" + dt.Rows[i]["Feedback"].ToString() + "</td>";
            Content += "<td><input type=\"submit\" ID=\"btnEdit\" name=\"ctl00$MainContent$btnEdit_OnClick\" onserverclick=\"btnEdit_OnClick()\" UseSubmitBehavior=\"False\" value=\"Edit\" /></td>";
            Content += "</tr>"; 

            if (BranchName != Next_Branch) //if last row of branch   
            {
                Content += "</table>";
                pane = new AccordionPane();   
                lbTitle = new Label();   
                lbContent = new Label();   
                pane.ID = "Pane_" + BranchName.ToString();   
                lbTitle.Text = BranchName;   
                pane.HeaderContainer.Controls.Add(lbTitle);   
                lbContent.Text = Content;   
                pane.ContentContainer.Controls.Add(lbContent);   
                acrDynamic.Panes.Add(pane);   
                Content = "";   
            }
        }

        #endregion

        #region Add the accordion to the page

        MyContent.Controls.Add(acrDynamic);

        #endregion

    }

    protected void btnEdit_OnClick(object sender, EventArgs e)
    {
        MdlCommentsExtender.Enabled = true;
        MdlCommentsExtender.Show();
        ScriptManager.GetCurrent(this).SetFocus(this.txtCommentBox);
    }

    protected void mdlCommentsOk_Click(object sender, EventArgs e)
    {
        MdlCommentsExtender.Hide();
    }
}

那就是它。这就是我的所有代码。它像一个冠军一样运行。除了一件事:当前的编辑按钮是由#region Create the panes部分中的这一行创建的:

Content += "<td><input type=\"submit\" ID=\"btnEdit\" name=\"ctl00$MainContent$btnEdit_OnClick\" onserverclick=\"btnEdit_OnClick()\" UseSubmitBehavior=\"False\" value=\"Edit\" /></td>";

这会导致回发,并且会否定我的模态形式。所以,我需要做的是将其改为asp:Button按钮,这样我就可以调用我的模态窗口。

谁能告诉我怎么做?假设我是一个白痴(因为我很喜欢),请在你的解释中明确表示。

1 个答案:

答案 0 :(得分:1)

我总是试图阻止在代码隐藏中创建html标记,特别是如果你仍然计划利用ASP.NET管道。如果您可以确保在页面加载时重新生成controltree,则更改后的代码将如下所示:

Table table = null;

string BranchName = null;

for (int i = 0; i < dt.Rows.Count; i++)
{
    TableRow tr;
    if (BranchName != dt.Rows[i]["Category"].ToString())
    {
        BranchName = dt.Rows[i]["Category"].ToString();
        // setup pane
        pane = new AccordionPane();
        // add pane to accordion
        acrDynamic.Panes.Add(pane);

        // pane content
        lbTitle = new Label();
        pane.ID = "Pane_" + BranchName;
        lbTitle.Text = BranchName;
        pane.HeaderContainer.Controls.Add(lbTitle);

        // pane container will have the table
        table = new Table();
        // set properties like class, height .. etc
        table.CssClass = "hoverTable";
        // add it to a container
        pane.ContentContainer.Controls.Add(table);

        // top row
        tr = new TableRow();
        // cells
        TableHeaderCell th = new TableHeaderCell();
        th.Width = 440;
        th.Text = "Provision";
        // set other properties
        tr.Controls.Add(th);
        // create next control
        th = new TableHeaderCell();
        th.Width = 120;
        th.Text = "Mark for Review";
        // set other properties
        tr.Controls.Add(th);
        // etc for other columns
        table.Controls.Add(tr);

    }

    tr = new TableRow();
    TableCell tc = new TableCell();
    tc.Text = dt.Rows[i]["Provision"].ToString();
    tr.Controls.Add(tc);
    tc = new TableCell();
    tc.Text = dt.Rows[i]["MarkForReview"].ToString();
    tr.Controls.Add(tc);
    // etc for other columns
    // add the button
    tc = new TableCell();
    Button button = new Button();
    button.Text = "Edit";
    button.UseSubmitBehavior = false;
    button.Click += new EventHandler(btnEdit_OnClick);
    tc.Controls.Add(button);
    tr.Controls.Add(tc);

    table.Controls.Add(tr);
}

我不确定姓名和身份证明。我希望控件将在Button控件的Render方法中获得正确的名称和id。