在页面加载访问标签值在C#中,当值通过jQuery的设置 [英] Access the Label value on Page load in c# when value set through jQuery

查看:94
本文介绍了在页面加载访问标签值在C#中,当值通过jQuery的设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我再次发布了这个问题,也许这一次更准确的描述。

I am posting this question again, maybe this time more accurate description.

问题是,我使用jQuery来设置标签的文本价值,它工作正常的浏览器,但是当我想将它保存到字符串,它不保存它。这里是
前端的Html code。

The problem is , I am using jQuery to set the Label's text value and it works fine on browser, but when I want to save it to string, it does not save it. Here is the front End Html Code.

  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
        $(window).load(function () {
            var myNewName = "Ronaldo";
           $('#<%= Label1.ClientID %>').text(myNewName);

        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

    </form>
</body>
</html>

这里是后端C#code在页面加载

And here is the Back End C# Code On Page Load

using System;
using System.Web.UI;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string mynameCheck = Label1.Text;
        if (mynameCheck=="Ronaldo")
        {
            Response.Write("Yes Name is Fine");
        }
        else
        {
            Response.Write("Name's not Fine");

        }
    }
}

显示的结果是

Name's not Fine
Ronaldo

好像串仍然是空。有没有渲染任何问题?

Seems like the string is still Null. Is there any problem of rendering??

推荐答案

这应该这样做:

<script type="text/javascript">
    $(window).load(function () {
       if($('#<%= Txt1.ClientID %>').val() != "Ronaldo"){
         var myNewName = "Ronaldo";
         $('#<%= Txt1.ClientID %>').val(myNewName);
         $('#<%= Label1.ClientID %>').text(myNewName);
         $('#<%= Btn1.ClientID %>').click();
       }
    });
</script>

<form id="form1" runat="server">
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:TextBox ID="Txt1" runat="server" style="display:none"></asp:Label>
    <asp:Button ID="Btn1" runat="server" style="display:none" Click="Btn1_Click"></asp:Label>
</form>


protected void Page_Load(object sender, EventArgs e)
{
    if(IsPostBack)
    {
      Label1.Text=Txt1.Text;

      string mynameCheck = Label1.Text;
      if (mynameCheck=="Ronaldo")
      {
        Response.Write("Yes Name is Fine");
      }
      else
      {
        Response.Write("Name's not Fine");
      }
    }
}

protected void Btn1_Click(object sender, EventArgs e)
{ }

希望它能帮助:)

Hope it helps :)

这篇关于在页面加载访问标签值在C#中,当值通过jQuery的设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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