简体   繁体   English

我如何在aspx页面上显示变量值后面的代码

[英]How do I display the code behind variable value on my aspx page

Hi I have a Default aspx page and and its code behind page, I want to render the value of particular variable on my code behind page to the aspx page. 嗨,我有一个Default aspx页面及其背后的代码,我想将我代码背后的特定变量的值呈现给aspx页面。 For ex:- 例如:

protected void Page_Load(object sender, EventArgs e)
{
    int i=12;
}

Now i am using bootstrap control on my aspx page 现在我在aspx页面上使用引导程序控件

  <button type="button" class="btn btn-primary">Primary <span class="badge">7</span></button>

I want to display value of i(present in c# code behind file) on the place of 7 我想在7的位置显示i的值(在文件后面的c#代码中显示)

Please guide me... 请指导我...

Easy way would be to use use a server control like a label instead of the span and you can set the text in the codebehind that way. 一种简单的方法是使用诸如标签之类的服务器控件而不是跨度,并且您可以在该方法后面的代码中设置文本。

C# C#

protected void Page_Load(object sender, EventArgs e)
{
    int i = 12;
    Lbl_Badge.Text = i.ToString();
}

Markup: 标记:

<button type="button" class="btn btn-primary">Primary <asp:Label CssClass="badge" runat="server" ID="Lbl_Badge" Text="7" /></button>

If you don't want to use an ASP Label as in @abney317's example, you can do this: 如果您不想像@ abney317的示例中那样使用ASP标签,则可以执行以下操作:

CodeBehind 代码隐藏

protected void Page_Load(object sender, EventArgs e) {
    int i = 12;
    BadgeValue.InnerHtml = i.ToString();
}

Markup: 标记:

<button type="button" class="btn btn-primary">Primary <span class="badge" id="BadgeValue" runat="server">0</span></button>

Note: If you are going to be displaying text that may come from a user, you should be sure to AntiXss.HtmlEncode anything you put into a control's InnerHtml. 注意:如果要显示可能来自用户的文本,则应确保对AntiXss.HtmlEncode放入控件的InnerHtml中的任何内容进行编码。

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

相关问题 如何在后面的代码中访问另一个ASPX页面? - How can I reach an aspx page from another in code behind? 在按钮上,单击发送Ajax调用并从aspx.cs(后面的代码)获取值到aspx页面,并将其显示在同一页面的输入字段中 - On button click send Ajax call and get value from aspx.cs(code behind) to aspx page and display it in input field in the same page 从页面后面的代码访问变量到aspx页面 - access variable from code behind page into aspx page 关于不在服务器上运行的.aspx页上的输入变量以及背后的代码 - Regarding an input variable on .aspx page which is not running on server and code behind 从aspx获取变量值并在后面的代码中使用它 - Get variable value from aspx and use it in code behind 如何在页面上多次显示我的js变量 - How do i display my js variable multiple times on a page 如何在页面加载时将代码后面的字符串发送到 javascript 中的变量? - How do I send a string from code behind to a variable in javascript, on page load? 在哪里可以找到动态生成的.aspx文件背后的代码 - Where do I find the code behind a dynamically generated .aspx file 如何从后面的代码中调用.aspx页中的书面函数 - how to call a written function in .aspx page from code behind ASPX页面后面的代码未执行js代码 - code behind ASPX page is not executing js code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM