设置 runat=server 时输入名称和 ID 会发生变化 [英] input name and id changes when set runat=server

查看:22
本文介绍了设置 runat=server 时输入名称和 ID 会发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的表单中,我需要插入文本"类型的不同输入.输入必须是带有名称和 ID 的 html 控件.因为我将此表单发送到外部网址.

In my form I need to insert different inputs of type "text". The inputs must be html controls with name and id's. Because I send this form to a external url.

对于验证,我在所有输入中执行 runat=server,然后我可以使用 requiredfieldvalidator.

For the validation I do runat=server in all inputs and then I can use requiredfieldvalidator.

但问题是当我在访问页面后查看源代码时,名称和 ID 都已更改.例如

But the problem is when I look in the source after visiting the page the name and id's are all changed. for example

<input id="first_name" class="formright" type="text" name="first_name" runat="server" />

更改为

<input name="ctl00$cphContent$first_name" type="text" id="ctl00_cphContent_first_name" class="formright">

我必须使用 html 控件,因为外部 postbackurl 查看 name 和 id 值以找到控件.所以我不能使用asp控件.这是因为我使用了带有 runat=server

I have to use html controls because the external postbackurl looks at the name and id values to find the control. So I can't use asp controls. It is because of that I used html controls with runat=server

感谢您的帮助

推荐答案

这是因为您使用的是 MasterPages.

This is because you're using MasterPages.

内容页面中包含的控件采用基于母版页层次结构的动态 ID,以避免 ID 重复.

Controls contained within a content page take on a dynamic ID based on the Master Page hierarchy in order to avoid duplication of IDs.

如果需要在客户端脚本中引用控件ID和名称,可以使用<%= first_name.ClientID %>.

If you need to reference the control IDs and names in client-side script, you can use <%= first_name.ClientID %>.

如果您使用 .NET4,您可以使用 ClientIDMode="Static" 使生成的 ID 保持一致,尽管这会带来自己的 ID 冲突警告,例如,在同一用户控件上使用多个实例时页.Rick Strahl 在此处概述了这些内容.

If you're using .NET4 you can use ClientIDMode="Static" to make the generated IDs consistent, although this comes with its own ID-conflict caveats when, for example, using multiple instances of the same user control on a page. Rick Strahl outlines those here.

但是,如果您使用的是 ASP.NET 验证器,那么一切都应该没问题.您应该使用 inputrel="noreferrer">ASP.NET 文本框控件:

However, if you're using ASP.NET validators then everything should be fine. Instead of using an HTML input you should use an ASP.NET TextBox control:

<asp:TextBox id="first_name" runat="server" CssClass="formright" />

这篇关于设置 runat=server 时输入名称和 ID 会发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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