在CodeBehind,WebForm C#中插入HTML?

时间:2016-01-18 05:34:07

标签: c# html asp.net web webforms

我有一个基于插入的数值生成工具的方法,我有另一个名为" DateTimePicker"它完成了提供文本框和日期+时间选择器功能的工作。

然而,现在的问题是,当我调用dynamic_tools方法并插入值" 4"和" addInfoPanel"方法。

html代码不会在面板中生成任何内容。

但是,当我尝试这个HTML时,它的工作原理。

lit.Text = @"<div class='errorMessage'>The user ID or password you entered does not match our records. Please try again. <br />
                        You may also securely recover your <a href='#'>User ID</a> or reset your <a href='#'>Password</a> online.
                    </div>";

我不太清楚这背后的原因是什么。我怀疑是否是因为我创建的自定义控件?任何帮助深表感谢。感谢

以下是代码:

DateTimePicker.ascx.cs - &gt;取自https://github.com/jiestrada/DateTimePicker

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class DateTimePicker : System.Web.UI.UserControl
{
    public string DateTime
    {
        get { return txtDateTime.Text; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        DateTimePicker picker = this;
        ScriptManager.RegisterClientScriptBlock(picker, picker.GetType(), "message", "<script type=\"text/javascript\" language=\"javascript\">getDateTimePicker();</script>", false);
    }
}

DateTimePicker.ascx - &gt;取自https://github.com/jiestrada/DateTimePicker

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="DateTimePicker.ascx.cs"
    Inherits="DateTimePicker" %>
<style type="text/css">
    .textBox
    {
        width: 150px;
        border-radius: 4px 4px 4px 4px;
        color: #555555;
        display: inline-block;
        font-size: 14px;
        height: 20px;
        line-height: 20px;
        margin-bottom: 10px;
        padding: 4px 6px;
        vertical-align: middle;
        background-color: #FFFFFF;
        border: 1px solid #CCCCCC;
        box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset;
        transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s;
        font-family: Helvetica Neue,Helvetica,Arial,sans-serif;
    }
</style>

<script type="text/javascript" language="javascript">
    function getDateTimePicker()
    {
       $('#datetimepicker').DateTimePickerNew({
            format: 'dd/MM/yyyy hh:mm:ss',
            language: 'en'
      });
    }
</script>

<div id="datetimepicker" class="input-append date">
    <asp:TextBox ID="txtDateTime" CssClass="textBox" runat="server"></asp:TextBox>
    <span class="add-on"><i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
    </span>
</div>

<script type="text/javascript" language="javascript">
       $('#datetimepicker').DateTimePickerNew({
            format: 'dd/MM/yyyy hh:mm:ss',
            language: 'en'
      });
</script>

动态工具方法

 protected string dynamic_tools(int key, PlaceHolder panel)
    {

    if (key == 4)
    {
    string value = "dtp"+i;

    Literal lit = new Literal();

    lit.Text = @"<td><DateTime:DateTimePicker ID='DateTimePicker' runat='server' /></td>";
    panel.Controls.Add(lit);


    }

    }

HTML CODE

<%@ Register Src="~/Control/DateTimePicker.ascx" TagName="DateTimePicker" TagPrefix="DateTime" %>

<div id="wrapper">


     <asp:ScriptManager ID="ScriptManager1" runat="server">
     </asp:ScriptManager>

     <asp:UpdatePanel ID="singtelUpdatePanel" runat="server" UpdateMode="Conditional">


                <ContentTemplate>

                     <asp:PlaceHolder ID="addInfoPanel" runat="server" Visible="false"></asp:PlaceHolder>

                </ContentTemplate>


     </asp:UpdatePanel>
</div>

1 个答案:

答案 0 :(得分:0)

DateTimePicker dtp = new DateTimePicker();
dtp.ID = "someID";
//Add some attributes else, if you need. Such as a width, height, so on.
panel.Controls.Add(new LiteralControl("<td>"));
panel.Controls.Add(dtp);
panel.Controls.Add(new LiteralControl("</td>"));