如何访问<表单>那不是母版页
表单>Winforms 开发人员转换为 Web 开发人员。我知道关于具有标签的母版页的部分,不清楚的是我是否可以在我的内容页面之一中拥有另一个标签。我一直在搜索中看到这两个答案。我认为是的,只要有 runat=server 就可以。问题是我有一个包含母版页的网页上的几个单选按钮。我有一个 .js 文件,它有一个函数,如果我将名称发送到其中,它将循环遍历表单中的控件以查看选择了哪个并返回所需的日期范围(MTD、QTD、YTD 等)。当我在非母版页网页上运行它时,它工作正常。但是,当我在具有母版页的网页上运行时,我似乎无法访问该元素。我尝试了 getElementByID,我尝试循环浏览页面元素等。也许我的处理方式不正确,我希望有人可以纠正我。
这是我的 .js 文件中的代码,可能有助于解释我想要做得更好一点。
var frmDateRanges = document.getElementById(formFieldName);
var chosen;
var len = frmDateRanges.DateRanges.length;
for(i=0;i<len;i++)
{
if(frmDateRanges.DateRanges[i].checked)
{
chosen = frmDateRanges.DateRanges[i].value;
}
}
其中 formFieldName 是传递到函数中的参数,DateRanges 是赋予单选按钮的名称值。
在我调用此函数的按钮中,我有: onclick ="FunctionCall('frmDateRanges')"
FunctionCall 仅用于描述目的,'frmDateRanges' 是为表单 action="" 提供的名称和 id,
感谢您的帮助,因为我在这一点上感到困惑。如果有更好的方法来做到这一点,也请告诉我。
减少在客户端重新渲染的 HTML
var theForm = document.forms['aspnetForm'];
if (!theForm) { theForm = document.aspnetForm; } 函数 __doPostBack(eventTarget, eventArgument) { if (!theForm.onsubmit || (theForm.onsubmit() != false)) { theForm.__EVENTTARGET.value = eventTarget; theForm.__EVENTARGUMENT.value = eventArgument; theForm.submit(); } }
<form id = "frmDateRanges" action = "">
<dl>
<dt> Begin Date: End Date:</dt><dd><input name="ctl00$ContentPlaceHolder1$wpSettings$beginDT" type="text" id="ctl00_ContentPlaceHolder1_wpSettings_beginDT" style="width:67px;" />
<input name="ctl00$ContentPlaceHolder1$wpSettings$endDT" type="text" id="ctl00_ContentPlaceHolder1_wpSettings_endDT" style="width:67px;" />
<input id="btnBackOneDateRange" name = "btnBackOneDateRange" style="width: 20px; height: 21px" type="button"
value="<" onclick="BackOneDateRange('ctl00_ContentPlaceHolder1_wpSettings_beginDT', 'ctl00_ContentPlaceHolder1_wpSettings_endDT', 'frmDateRanges');"/>
<input id="btnForwardOneDateRange" style="width: 20px; height: 21px" type="button"
value=">" /></dd><dd>
<input type="radio" id="btnTrl1Yr" name="DateRanges" style="width: 19px" value="1" onclick="GetTrailingYears('ctl00_ContentPlaceHolder1_wpSettings_beginDT', 'ctl00_ContentPlaceHolder1_wpSettings_endDT','1');" />
1 Year
<input type="radio" id="btnTrl3Yr" name="DateRanges" style="width: 19px" value="3" onclick="GetTrailingYears('ctl00_ContentPlaceHolder1_wpSettings_beginDT', 'ctl00_ContentPlaceHolder1_wpSettings_endDT','3');" />
3 Years
<input type="radio" id="btnTrl5Yr" name="DateRanges" style="width: 19px" value="5" onclick="GetTrailingYears('ctl00_ContentPlaceHolder1_wpSettings_beginDT', 'ctl00_ContentPlaceHolder1_wpSettings_endDT','5');" />
5 Years
<input type="radio" id="btnTrl10Yr" name="DateRanges" style="width: 19px" value="10" onclick="GetTrailingYears('ctl00_ContentPlaceHolder1_wpSettings_beginDT', 'ctl00_ContentPlaceHolder1_wpSettings_endDT','10');" />
10 Years</dd><dt><input type="radio" id="btnMthToDate" name="DateRanges" style="width: 19px" value="mth" onclick="GetMonthToDate('ctl00_ContentPlaceHolder1_wpSettings_beginDT', 'ctl00_ContentPlaceHolder1_wpSettings_endDT');" />
Month
<input type="radio" id="btnQtrToDate" name="DateRanges" style="width: 19px" value="mth" onclick="GetQuarterToDate('ctl00_ContentPlaceHolder1_wpSettings_beginDT', 'ctl00_ContentPlaceHolder1_wpSettings_endDT');" />
Quarter <input type="radio" id="btnYearToDate" name="DateRanges" style="width: 19px" value="mth" onclick="GetYearToDate('ctl00_ContentPlaceHolder1_wpSettings_beginDT', 'ctl00_ContentPlaceHolder1_wpSettings_endDT');" />
Calendar Year</dt></dl>
</div>
</form>
Winforms developer converting to web developer. I know the part about the Master Page having the tag, what is not clear is if I can or can not have another in one of my content pages. I have been seeing both answers doing searches. I see yes you can if only one has runat=server. The thing is I have a that contains several radio buttons on a web page that has a master page. I have a .js file that has a function if I send the name into it, it will loop thru the controls in the form to see which one is selected and return the desired date horizon(MTD, QTD, YTD, etc.). When I run this on a non master page web page it works fine. However, when I run on a web page that has a master page I can't seem to get to the element. I tried getElementByID, I tried looping through the page elements, etc. Maybe I am going about this incorrectly and I hope someone can straighten me out.
Here is the code from my .js file that may help explain what I am trying to do a little better.
var frmDateRanges = document.getElementById(formFieldName);
var chosen;
var len = frmDateRanges.DateRanges.length;
for(i=0;i<len;i++)
{
if(frmDateRanges.DateRanges[i].checked)
{
chosen = frmDateRanges.DateRanges[i].value;
}
}
where formFieldName is an arguement that is passed into the function and DateRanges is the name value given to the radio buttons.
In the button I call this function I have: onclick ="FunctionCall('frmDateRanges')"
FunctionCall is just for description purposes, 'frmDateRanges' is the name and id given to the form action=""
Thanks for the help as I am stumped at this point. If there is a better way to do this please let me know that as well.
Trimmed down HTML redered on the client
var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
<form id = "frmDateRanges" action = "">
<dl>
<dt> Begin Date: End Date:</dt><dd><input name="ctl00$ContentPlaceHolder1$wpSettings$beginDT" type="text" id="ctl00_ContentPlaceHolder1_wpSettings_beginDT" style="width:67px;" />
<input name="ctl00$ContentPlaceHolder1$wpSettings$endDT" type="text" id="ctl00_ContentPlaceHolder1_wpSettings_endDT" style="width:67px;" />
<input id="btnBackOneDateRange" name = "btnBackOneDateRange" style="width: 20px; height: 21px" type="button"
value="<" onclick="BackOneDateRange('ctl00_ContentPlaceHolder1_wpSettings_beginDT', 'ctl00_ContentPlaceHolder1_wpSettings_endDT', 'frmDateRanges');"/>
<input id="btnForwardOneDateRange" style="width: 20px; height: 21px" type="button"
value=">" /></dd><dd>
<input type="radio" id="btnTrl1Yr" name="DateRanges" style="width: 19px" value="1" onclick="GetTrailingYears('ctl00_ContentPlaceHolder1_wpSettings_beginDT', 'ctl00_ContentPlaceHolder1_wpSettings_endDT','1');" />
1 Year
<input type="radio" id="btnTrl3Yr" name="DateRanges" style="width: 19px" value="3" onclick="GetTrailingYears('ctl00_ContentPlaceHolder1_wpSettings_beginDT', 'ctl00_ContentPlaceHolder1_wpSettings_endDT','3');" />
3 Years
<input type="radio" id="btnTrl5Yr" name="DateRanges" style="width: 19px" value="5" onclick="GetTrailingYears('ctl00_ContentPlaceHolder1_wpSettings_beginDT', 'ctl00_ContentPlaceHolder1_wpSettings_endDT','5');" />
5 Years
<input type="radio" id="btnTrl10Yr" name="DateRanges" style="width: 19px" value="10" onclick="GetTrailingYears('ctl00_ContentPlaceHolder1_wpSettings_beginDT', 'ctl00_ContentPlaceHolder1_wpSettings_endDT','10');" />
10 Years</dd><dt><input type="radio" id="btnMthToDate" name="DateRanges" style="width: 19px" value="mth" onclick="GetMonthToDate('ctl00_ContentPlaceHolder1_wpSettings_beginDT', 'ctl00_ContentPlaceHolder1_wpSettings_endDT');" />
Month
<input type="radio" id="btnQtrToDate" name="DateRanges" style="width: 19px" value="mth" onclick="GetQuarterToDate('ctl00_ContentPlaceHolder1_wpSettings_beginDT', 'ctl00_ContentPlaceHolder1_wpSettings_endDT');" />
Quarter <input type="radio" id="btnYearToDate" name="DateRanges" style="width: 19px" value="mth" onclick="GetYearToDate('ctl00_ContentPlaceHolder1_wpSettings_beginDT', 'ctl00_ContentPlaceHolder1_wpSettings_endDT');" />
Calendar Year</dt></dl>
</div>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的内容页面中只能有 1 个 runat=server 表单。与页面中的其他表单混在一起可能会比其价值更令人困惑,即使它是一个小型 HTML 控件表单。
话虽这么说,我认为您遇到了这样的问题:当您尝试访问内容页面中的 DOM 元素时,包含这些元素的母版页已更改您的所有 ID。如果检查渲染的表单源,您将看到 id 元素现在已预先添加了内容容器 id,如下所示:
这可能会导致您出现 document.getElementById(formFieldName); 问题。
You can only have 1 runat=server form in your content page. Messing around with other forms in the page can be more confusing than its worth, even if its a small HTML control only form.
That being said I think you are running into the issue that when you try to access a DOM element in a content page, the Master page that contains the elements has changed all your ID's. If you check your rendered form source you will see the id elements now have the content container id pre-pended like this:
That may be causing your issue with document.getElementById(formFieldName);
有几点:
id
不完全匹配 - 它将根据id 上的内容占位符以及(可能)DOM 树中位于其上方的任何其他容器。如果可以的话,您应该尝试为您动态生成一些 JavaScript,这样您就可以输出服务器控件的
ClientID
属性,您可能必须传入服务器控件的名称DateRange 单选按钮 - 当您声明 RadioButton 控件时,您应该给它一个 GroupName - 这样所有单选按钮都具有相同的name
(而不是 id),因此您可以传入该单选按钮的名称第一个选项然后您可以对该对象调用
document.GetElementById
,并请求 .name 属性来获取单选按钮集合的名称,并使用该名称调用document.getElementsByName
价值:A couple of things:
<form>
elements isn't legal mark-up: some browsers are more lenient than others about this, but you will see odd behaviour if you do this.id
that you gave the control in the markup - it will be changed depending on the id the content placeholder and (potentially) any other containers above it in the DOM tree.If you can, you should try and have some of your JavaScript generated dynamically for you, that way you can output the
ClientID
property of your server controls, you'll probably have to pass in the name of the DateRange radio buttons - when you declare a RadioButton control you should give it a GroupName - that way all the radio buttons have the samename
(rather than id), so you could potentially pass in the name of the first optionYou can then call
document.GetElementById
on that object, and request the .name property to get the name of the radio button collection, and calldocument.getElementsByName
with that value: