如何使用 Javascript 更改 Gridview 中隐藏字段的值?
好吧,我需要更改 gridview 中隐藏字段的值,这是我到目前为止所得到的:
for(var i = 0; i < gv_Proofs.rows.length; i++)
{
var tbl_Cell = gv_Proofs.rows[i].cells[0];
var sdiFound = false;
for(var x = 0; x < tbl_Cell.childNodes.length; x++)
{
if(tbl_Cell.childNodes[x].id == "_ctl0_MasterContentPlaceHolder_gv_Proofs__ctl2_lbl_SDI")
{
if(tbl_Cell.childNodes[x].innerHTML == sdi)
sdiFound = true;
}
if(tbl_Cell.childNodes[x].id == "_ctl0_MasterContentPlaceHolder_gv_Proofs__ctl2_lbl_Updated" && sdiFound)
tbl_Cell.childNodes[x].value = "true";
}
}
任何人都可以告诉我我做错了什么吗? 谢谢你!
Ok I need to change the value of a hidden field in a gridview and here is what I have so far:
for(var i = 0; i < gv_Proofs.rows.length; i++)
{
var tbl_Cell = gv_Proofs.rows[i].cells[0];
var sdiFound = false;
for(var x = 0; x < tbl_Cell.childNodes.length; x++)
{
if(tbl_Cell.childNodes[x].id == "_ctl0_MasterContentPlaceHolder_gv_Proofs__ctl2_lbl_SDI")
{
if(tbl_Cell.childNodes[x].innerHTML == sdi)
sdiFound = true;
}
if(tbl_Cell.childNodes[x].id == "_ctl0_MasterContentPlaceHolder_gv_Proofs__ctl2_lbl_Updated" && sdiFound)
tbl_Cell.childNodes[x].value = "true";
}
}
can anyone tell me what I am doing wrong? Thank You!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我成功了。 上面的循环工作得很好,但显然我的 sdi 值并不总是设置正确,因此我检查的值总是设置为 false。 因此,如果有人再次遇到此问题,上述内容在我的情况下非常有效。
I got it working. The above loop was working just right but apparently my value of sdi was not always getting set right, and therefore the value I was checking was always set to false. So the above worked perfectly in my case if anyone ever has this issue again.
仅当 childNode id 为(截断的)lbl_SDI 时,您的第一个语句才为 true。
仅当第一个语句为真(通过 sdiFound)并且 ID 为(截断)lbl_Updated 时,您的第二个语句才为真。
所以第二个陈述不可能是真的。 仅当节点 ID 不是 lbl_Updated 时,变量 sdiFound 才会为 true。
我认为很多人可能不同意您也专门引用 .NET ID。 您是否考虑过
在 JavaScript 中使用:?
Your first statement is only true if the childNode id is (truncated) lbl_SDI.
Your second statement is only true if the first statement is true (through sdiFound) AND the ID is (truncated) lbl_Updated.
So there's no way for the second statement to ever be true. The variable sdiFound will only be true when the node id isn't lbl_Updated.
I think a lot of people probably disagree with you referencing the .NET ID specifically as well. Have you considered using:
in your javascript?
好吧,你能告诉我发生了什么事吗? 似乎您忘记将 sdiFound 变量重置为 false,或者在找到它时中断循环。
至于你的问题:
如果你将隐藏的 html 输入作为服务器控件运行,那么它将由 GridView 显示,并且它将有一个唯一的 id。 然后您可以使用 getElementById 找到它。 您可以将其放入一个为您构建正确 ID 的循环中,您应该能够找到您的控件。
well, can you tell what is happening? it seems like you are forgetting to reset the sdiFound variable to false, or breaking off the loop when you do find it.
as far as your question:
if you run the hidden html input as a server control, then it will be displayed by the GridView and it will have a unique id. you could then find it by using getElementById. You can put this inside a loop that builds the right id for you and you should be able to find your control.
编辑:发布前检查的经典案例。 显然设置hidden=true实际上会阻止数据绑定这就是 JS 出现问题的原因。 我会说这是 ASP.NET 的典型问题之一,这让我咒骂它的名字,但你可能只是选择使用众多控件之一来调整控件googleable 解决方案。
关于在 ASP.NET 中使用元素 ID 的评论保持原样,因为它是邪恶的。
如果没有更多代码,很难知道(这到底是什么时候执行的?是否有其他东西覆盖了它?什么)标记看起来像什么?)但我现在可以说,通过 ASP.NET 变形 ID 引用元素是一个坏主意(TM)。
如果可以的话,我建议你尝试在为你处理引用的代码隐藏中修改它,或者至少写出可以使用 clientID 的 JS。 如果做不到这一点,我会尝试通过扩展的元素搜索器(您自己的或像 jquery 等人的“$”函数这样的库实用程序)找到该元素,这将允许您使用强大的 CSS 或 Xpath 参数。
Edit: classic case of check before you post. Apparently setting hidden=true actually prevents databinding which will be why the JS is having issues. I would say this is one of those typical problems with ASP.NET which make me curse it's very name, but you might just choose to adapt the control with one of the many googleable solutions.
Comments about using element ID in ASP.NET left intact because it's evil.
Hard to know without more code (when is this executing exactly? is something else overwriting it? what does the markup look like?) but I can say right now that referencing an element by it's ASP.NET deformed ID is a Bad Idea(TM).
If you can I suggest you try and modify this in the codebehind where the reference is handled for you, or at least write out the JS there where you can use clientID. Failing that I would try and find the element through an expanded element hunter - your own or a library util like jquery et al's "$" function - which will allow you to use a robust CSS or Xpath argument instead.