简体   繁体   English

在CodeBehind,WebForm C#上插入HTML?

[英]Insert HTML at CodeBehind, WebForm C#?

i have a method that generate tools based on the number value inserted, and I have another control called "DateTimePicker" that does the job of providing a textbox and the date+time picker function. 我有一个基于插入的数字值生成工具的方法,还有另一个名为“ DateTimePicker”的控件,该控件负责提供文本框和日期+时间选择器功能。

However, now the problem is that when i call dynamic_tools method and inserted the value "4" and "addInfoPanel" to the method. 但是,现在的问题是,当我调用dynamic_tools方法并将值“ 4”和“ addInfoPanel”插入该方法时。

The html code does not generating anything in the panel. html代码不会在面板中生成任何内容。

But, when i tried this html, it works. 但是,当我尝试使用此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>";

I'm not too sure what is the reason behind this. 我不太清楚这背后的原因是什么。 I'm suspecting whether is it because of the custom control that i have created? 我怀疑是否是因为我创建了自定义控件? any help is much appreciated. 任何帮助深表感谢。 Thanks 谢谢

Here's the code: 这是代码:

DateTimePicker.ascx.cs -> taken from https://github.com/jiestrada/DateTimePicker DateTimePicker.ascx.cs->取自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 -> taken from https://github.com/jiestrada/DateTimePicker DateTimePicker.ascx->取自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>

Dynamic Tools Method 动态工具方法

 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 HTML代码

<%@ 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>
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>"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM