在InnerHTML中的代码后面添加一个ASP控制页面 [英] Adding a ASP Control to page from Code Behind within InnerHTML

查看:245
本文介绍了在InnerHTML中的代码后面添加一个ASP控制页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从后面的代码中向网页添加一个按钮。我的主页上有一个空的div,可以在需要时显示。然而,我希望动态创建的内容可以根据条件而改变。



我已经意识到,在我的ASP Control中,我使用了一个/(反斜杠),它取消我的HTML。我现在遇到的问题是了解如何使用代码解决这个问题,有没有办法将ASP控件添加到网页中?我开放给InnerHtml之外的建议。



我创建我的按钮就像这样(在我的代码背后):

  string buttonout = string.Format(< asp:Button ID = \helpButton_0\CommandArgument = \0 \CssClass = \ HelpButton \runat = \server \Text = \?\/>); 
innercontent [0] = string.Format(< table>< tr>< td>带领乘客信息< / td>< / tr>< tr>< td>在这里deafaul we < / td>< / tr>< tr>< td> { 0}< / td>< / tr>< / table>+ buttonout);

如上所述,这是行不通的原因是因为InnerHtml讨厌反斜杠,我想。



我确实有解决方案;
$ b

 < div id =HelpBoxDivrunat =serverVisible =假> 
< div id =HelpBoxDiv_Toprunat =serverVisible =true>
< / div>
< div id =HelpBoxDiv_Bottomrunat =serverVisible =true>
< asp:Button ID =button_HelpBox_falserunat =server/>
< / div>
< / div>

然后,我会将Innerhtml添加到_Top Div,而不是我现在正在执行的HelpBoxDiv 。然而,这个解决方案并没有教会我什么。



我在这里提出问题犹豫不决,因为我知道很多问题都被问到了,我确信这个问题有之前,但我没有找到解决方案。任何帮助我们都非常感谢。



谢谢。

解决方案


我已经意识到,在我的ASP控件中,我使用了一个/(反斜杠),
取消了我的HTML。我现在遇到的问题是理解
如何通过代码解决这个问题,有没有办法将ASP控件添加到
网页中?我接受InnerHtml以外的建议。


您不能像字面字符串那样添加ASP.Net服务器控件。

相反,您希望将动态服务器控件添加到以下方法 -



ASPX



 < asp:PlaceHolder runat =serverID =PlaceHolder1>< / asp:PlaceHolder> 



代码隐藏



  protected void Page_Init(object sender,EventArgs e)
{
var button = new Button
{
ID =helpButton_0,
CommandArgument = 0,
CssClass =HelpButton,
Text =?
};
button.Command + = button_Command;
PlaceHolder1.Controls.Add(button);
}

私人无效按钮命令(对象发件人,CommandEventArgs E)
{
//处理动态按钮的命令事件。
}


I'm trying to add a button to the webpage, from the code behind. I have a single empty div on my main page that visible on and off, when needed. However the content I wish to create dynamically as the div content can change dependent on conditions.

I have realised that within my ASP Control I use a / (backslash) which cancels out my HTML. The problem I now have is understanding how I can get around this with code, is there a way to add ASP Controls to the web page? I am open to suggestions outside of the InnerHtml.

I'm creating my Button like so (in my Code Behind):

      string buttonout = string.Format("<asp:Button ID=\"helpButton_0\" CommandArgument=\"0\" CssClass=\"HelpButton\" runat=\"server\" Text=\"?\"/>");
      innercontent[0] = string.Format("<table><tr><td>Lead Passenger Information</td></tr><tr><td>Here by deafaul we used your detaisl from your profile, if you're not the lead passenger (As in you're booking for someone else) then please change details.</td></tr><tr><td>{0}</td></tr></table>"+ buttonout);

As Said above, The reason this doesn't work is because of InnerHtml hating backslashes, I think.

I do have a solution to this; and that's by adding more divs to the page.

        <div id="HelpBoxDiv" runat="server" Visible="false">
            <div id="HelpBoxDiv_Top" runat="server" Visible="true">
            </div>
            <div id="HelpBoxDiv_Bottom" runat="server" Visible="true">
                <asp:Button ID="button_HelpBox_false" runat="server" />
            </div>
        </div>

I would then add my Innerhtml to the _Top Div, instead of the HelpBoxDiv which I am currently doing now. However this solution doesn't teach me anything.

I am hesitant to ask questions here, as I know a lot of question have been asked and I am sure this one has before, but I didn't find a solution. Any help is much appreciated.

Thank you.

解决方案

I have realised that within my ASP Control I use a / (backslash) which cancels out my HTML. The problem I now have is understanding how I can get around this with code, is there a way to add ASP Controls to the web page? I am open to suggestions outside of the InnerHtml.

You cannot add ASP.Net server control like a literal string.

Instead, you want to add the dynamic server control to the following approach -

ASPX

<asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>

Code Behind

protected void Page_Init(object sender, EventArgs e)
{
    var button = new Button
    {
        ID = "helpButton_0",
        CommandArgument = "0",
        CssClass = "HelpButton",
        Text = "?"
    };
    button.Command += button_Command;
    PlaceHolder1.Controls.Add(button);
}

private void button_Command(object sender, CommandEventArgs e)
{
    // Handle dynamic button's command event.
}

这篇关于在InnerHTML中的代码后面添加一个ASP控制页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆