C# 中的 ASP.Net 标签更改

发布于 2024-11-27 22:52:05 字数 937 浏览 0 评论 0 原文

我通常处理 C# 代码,但最近开发了一个 ASP.NET 页面,我有一个出现的日历,用户可以选择他/她想要的日期。该页面上有一个 asp lbl,我想显示当前选定的日期,这通常对我来说很容易,但我在引用/查找控件时遇到了麻烦。我也不确定实现这一目标的最佳方法,并且我肯定将来会遇到这个问题。

这是我想设置 lbl 文本并尝试使用 FindControl 方法的地方,但它对我不起作用,认为它可能嵌套,因为我有一些 div?

 public void Calendar1_SelectionChanged(object sender, System.EventArgs e)
    {

        Control Lbl = FindControl("inputField");
        if (Lbl != null)
        {
            //Control mycontrol2 = Lbl.Parent;
            Lbl.Text = Calendar1.SelectedDate.ToShortDateString();
        }   

这是在 asp 中。

  <div id="date">


         <input type="text" size="12" id="inputField"   />
        <script>
            $("#inputField").click(function () {
                $("#box").show("slow");
            });
</script>
    </div>

如何完成将输入字段文本设置为 Calendar.SelectedDate ? (以及您自己遇到的任何提示(如果有的话),以供良好实践)

感谢您的任何帮助。

I am usually dealing with c# code , but recently developing a asp.net page , I have a Calendar that Appears and the user selects the date he/she wants. On that page is a asp lbl that i would like to display the currently selected date, which is normally easy for me but i am having trouble referencing/finding the control . Also I am unsure of the best way to do achieve this and i'm sure to come across this problem in the future.

This is where I would like to set the lbl text and have tried using the FindControl method but it's not working for me , thinking its possibly nested as i have some divs?.

 public void Calendar1_SelectionChanged(object sender, System.EventArgs e)
    {

        Control Lbl = FindControl("inputField");
        if (Lbl != null)
        {
            //Control mycontrol2 = Lbl.Parent;
            Lbl.Text = Calendar1.SelectedDate.ToShortDateString();
        }   

and this is in asp.

  <div id="date">


         <input type="text" size="12" id="inputField"   />
        <script>
            $("#inputField").click(function () {
                $("#box").show("slow");
            });
</script>
    </div>

How do I accomplish setting the inputfield text to the Calendar.SelectedDate ?.
(and any tips you have come across yourself if any, for good practice)

Thanks For any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花之痕靓丽 2024-12-04 22:52:05

您不使用 asp:Label 的原因是什么?您找不到“标签”,因为它是一个 html 控件,也称为客户端控件。使用 并且您在后面的代码中更改其文本应该没有问题:

lblCal.Text = Calendar1.SelectedDate.ToShortDateString();

Any reason why you're not using an asp:Label? You can't find the "label" because it is an html control, aka a client side control. Use an <asp:Label id="lblCal" runat="server"... and you should have no problem changing its text in code behind:

lblCal.Text = Calendar1.SelectedDate.ToShortDateString();
伴梦长久 2024-12-04 22:52:05

Asp.Net 仅将服务器标记视为 C# 级别可用的控件。

所有这些 DIV 和其他标签对于 Asp.Net 来说只是纯文本,它们不会被考虑在控制层次结构中。

要使 Asp.Net 在服务器中考虑它,请将 runat="server" 属性放在您感兴趣的标签上。它们将可以在代码隐藏中通过 id 属性中放置的名称进行访问。

Asp.Net 将永远无法在示例代码中找到“inputField”。

解决方案:

为了解决您的问题,我建议您使用名为 TextBox 的 Asp.Net WebForm 控件...如果您想要的是输入

<asp:TextBox runat="server" id="inputField" />

通过这样做,您将能够通过名称访问代码隐藏中的inputField

inputField.Text = Calendar1.SelectedDate.ToShortDateString();

Asp.Net considers only server tags as being controls available at C# level.

All those DIVs and other tags are just plain text for the Asp.Net, they will no be considered in the control hierachy.

To make Asp.Net consider it in the server, place the runat="server" attribute on the tags of your interest. They will become accessible in the code-behind by the name placed in the id attribute.

Asp.Net will never be abled to find the 'inputField' in your sample code.

Solution:

To solve your problem I recommend you use an Asp.Net WebForm control called TextBox... if what you want is an input:

<asp:TextBox runat="server" id="inputField" />

By doing this, you will be abled to access the inputField, in the code-behind by name:

inputField.Text = Calendar1.SelectedDate.ToShortDateString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文