简体   繁体   English

ASP.NET C#如何访问标签内或字符串中的div文本?

[英]How can i access div text inside the label or in string?ASP.NET C#

I have a div in which user insert SQL query and I want to access this query in codebehind and pass to sqlcmd and execute query and show result to user but I'm unable to do this. 我有一个div ,用户在其中插入SQL查询,我想在codebehind访问此查询并传递给sqlcmd并执行查询并将结果显示给用户,但我无法执行此操作。 How can I do this? 我怎样才能做到这一点? Here is my aspx code: 这是我的aspx代码:

<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server">
    <form id="form1" runat="server">
        <div id="editor" contenteditable="true"></div>
        <br />
        <asp:Button runat="server" OnClick="load" Text="Submit" />
        <asp:label ID="TextBox1" ForeColor="White" runat="server"></asp:label>
    </form>
</asp:Content>

my aspx.cs (codebehind) is : 我的aspx.cs(代码隐藏)为:

public void load(object sender, EventArgs e)
{
    HtmlGenericControl footer = (HtmlGenericControl)FindControl("editor");
    String cmd = footer.InnerHtml.ToString();
    TextBox1.Text = cmd;
}

I'm getting this error: 我收到此错误: 链接

将runat =“ server”添加到您的DIV标签中,它将解决您的问题。

  <div id="editor" contenteditable="true" runat="server"></div>

Add runat="server" in div like this:- 像这样在div中添加runat="server" :-

<div id="editor" contenteditable="true" runat="server">Lorem Ipsum</div>

and use this code 并使用此代码

protected void Page_Load(object sender, EventArgs e)
    {       
        ContentPlaceHolder mainContent = (ContentPlaceHolder)this.Master.FindControl("body");
        HtmlGenericControl footer = (HtmlGenericControl)mainContent.FindControl("editor");
        String cmd = footer.InnerHtml.ToString();
        TextBox1.Text = cmd;
    }

where body is ContentPlaceHolderID you can find this on your content page bodyContentPlaceHolderID您可以在内容页面上找到它

<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server">

Hope this will help you 希望这个能对您有所帮助

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

相关问题 如何单击div中的按钮以不刷新整个页面asp.net C# - how can I click on button inside div to not to refresh whole page asp.net C# Asp.Net / C#-如何获取在重复器内嵌套的Label控件的文本? - Asp.Net / C# - How to get the Text of a Label control nested inside a Repeater? 如何使用jQuery和/或C#截断DataList中asp.net标签中的文本 - How to truncate text in asp.net label inside DataList using jQuery and/or C# 如何从 C# 更改 ASP.NET 标签文本? - How to Change ASP.NET Label Text from C#? 如何使用ASP.Net中的颜色对话框,然后使用c#将颜色应用于标签文本? - How can I use color dialog box in ASP.Net and apply that color to label text using c#? 如何在C#的FOR循环中使用ASP.NET的标签ID? - How can I use ASP.NET's Label IDs in a FOR loop in C#? 在ASP.NET/C#环境中,如何在按钮的文本字段上的字母字符串之间进行迭代? - How can you iterate between a letter string on the text field of a button in ASP.NET/C# enviroment? 在GridView(ASP.NET/C#)中的DataBinding中设置标签文本 - Setting label text in DataBinding in a GridView (ASP.NET/C#) Asp.Net(C#)标签文本内联 - Asp.Net(C#) Label Text inline 如何在C#ASP.NET中使用for循环更改多个asp:标签的文本 - How to change the text of the multiple asp:label using for loop in C# ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM