【问题标题】:How to retrieve from javascript the id of itemplate textbox control aspx如何从 javascript 中检索 itemplate 文本框控件 aspx 的 id
【发布时间】:2015-01-14 10:27:44
【问题描述】:

我正在尝试将 asp:textbox 的 id 检索到 itemplate 中。 我试过这样的事情:

var pu= window.document.getElementById("prixID");

它返回空值。我也试过了

window.document.getElementById("<%=prixID.ClientID%>");

它返回 prixID 不在实际上下文中。

如何解决这个问题?

【问题讨论】:

  • 您的 prixID 是否在母版页中,脚本是否在 Web 表单中?
  • 你在ItemTemplate中有TextBox
  • 是的,我的文本框在 itemtemplate 中
  • 你试过我的解决方案了吗?

标签: javascript c# asp.net


【解决方案1】:

文本框的 id 由层次结构中的所有控件组合而成。 contentplaceholder - 如果有母版页 网格视图 文本框

以及网格/中继器中的行/项目的计数器

你想用那个 id 做什么?

【讨论】:

  • 我不使用母版页。这是一个简单的 web 表单,在 c# 中带有单独的代码
  • 所以你的 id 将是 gridviewID + textboxID + counter,看看你的源代码。但真正的原因是你为什么需要它?
  • 所以你的 id 将是 gridviewID + textboxID + counter,看看你的源代码。但真正的原因是你为什么需要它?
  • “计数器??”我如何评估你检查这个计数器??
【解决方案2】:

如果文本框控件位于脚本的同一页面中,则可能您缺少

runat="server" 在控件中

如果不行就这样试试

<asp:TemplateField>
    <ItemTemplate>
        <asp:TextBox ID="idTExtBox" runat="server">
        </asp:TextBox>                      
    </ItemTemplate>
</asp:TemplateField>

然后

var pu= '<%= ((GridViewRow)Container).FindControl("idTExtBox").ClientID %>';

更新

尝试以这种方式在代码后面的C#代码中直接将id传递给脚本函数

idtext.Attributes.Add("onchange", "javascript:changeprix('" & idtext.ClientId & "')");
idtext.Attributes.Add("class", "disableThis");

然后在javascript函数中

function changeprix(idOfElement){
   var textBoxes= document.getElementsByClassName("disableThis");
   foreach(var textBox in textBoxes){
        textBox.disabled=true;
   }
   var pu= document.getElementById(idOfElement);

}

【讨论】:

  • 谢谢。我可以在客户端使用这个代码而不是后面的代码吗?
  • 是的,var pu= '&lt;%= ((GridViewRow)Container).FindControl("idTExtBox").ClientID %&gt;'; 是客户端
  • 在后面的代码中我使用了这个:TextBox idtext = (TextBox)row.FindControl("prixID"); with :GridViewRow 行 = (GridViewRow)child.Rows[e.NewEditIndex];
  • 在后面的代码中我使用了这个:TextBox idtext = (TextBox)row.FindControl("prixID");与 :GridViewRow 行 = (GridViewRow)child.Rows[e.NewEditIndex];这项工作。但我像这样将 onchange 属性添加到 idtext:idtext.Attributes.Add("onchange", "javascript:changeprix()");我尝试从客户端(aspx 代码)使用 window.document.getElementById ,检索所有其他文本框并将它们操作到 onchange 方法中
  • 不,这不行。我遇到了这个错误:“容器”在实际上下文中不存在。”
猜你喜欢
  • 2015-04-13
  • 1970-01-01
  • 1970-01-01
  • 2016-03-28
  • 2013-08-23
  • 1970-01-01
  • 2012-10-23
  • 2014-03-19
  • 2012-06-13
相关资源
最近更新 更多