没有在ASP.Net中获得ClientID

时间:2013-04-22 12:36:29

标签: javascript asp.net clientid

我有ASP.Net页面,我正在调用这样的JavaScript函数:

 Enter server name: <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
 <asp:Button ID="btn_view" runat="server" OnClick="View_btn_click" OnClientClick="return  AlertOnGo('View Configuration',document.getElementById('<%= txt_name.ClientID %>').value)" Text ="GO!" />

但是点击GO按钮,我收到以下错误:

JavaScript运行时错误:无法获取未定义或空引用的属性“值”

在查看按钮的渲染代码时:

<input type="submit" name="btn_view" value="GO!" onclick="return AlertOnGo('View Configuration',document.getElementById('<%= txt_name.ClientID %>').value);" id="btn_view" />

它没有放置适当的ClientID。我尝试将CLientIDMode设置为测试框的静态,但没有帮助。

此问题与此unanswered question类似。

7 个答案:

答案 0 :(得分:9)

这个问题有一些解决方案。

其中一个更简单的方法是将名称移动到JavaScript变量,因为在尝试评估ASP.NET控件的OnClientClick属性中的txt_name.ClientID时,问题只发生在控件中。

<script>
var txtName = '<%= txt_name.ClientID %>';
</script>

Enter server name: <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
 <asp:Button ID="btn_view" runat="server" OnClick="View_btn_click" 
OnClientClick="return  AlertOnGo('View Configuration', 
document.getElementById(txtName).value)" Text ="GO!" />

答案 1 :(得分:3)

您可以在asp:textbox标记

中使用属性ClientIDMode="Static" 像这样

<asp:TextBox ClientIDMode="Static" ID="txt_name" runat="server"></asp:TextBox>
<asp:Button ID="btn_view" runat="server" OnClick="View_btn_click" 
 OnClientClick="return  AlertOnGo('View Configuration', 
 document.getElementById(txtName).value)" Text ="GO!" />

这样,文本框的客户端ID将与“id”相同,您可以使用      document.getElementById('txt_name').value

它对我有用

答案 2 :(得分:0)

- 编辑答案 - ASPX

<asp:Button ID="btn_view" runat="server" OnClick="View_btn_click" OnClientClick="javascript:return  AlertOnGo('View Configuration');" Text ="GO!" />

脚本

function AlertOnGo(mode)
{
  var btnId = '<%= txt_name.ClientID %>';
  var value=document.getElementById(btnId).value;

//your code

return false;
}

答案 3 :(得分:0)

你正在使用&lt;%=%&gt;在asp.net Web控件的属性中 - 我不希望它工作,因为整个控件将被html取代。

我会将你的javascript移到一个脚本块中,并将事件处理程序附加到代码中,而不是将它全部内联。

Enter server name: <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
 <asp:Button ID="btn_view" runat="server" OnClick="View_btn_click" Text ="GO!" />
<script>
document.getElementById('<%= btn_view.ClientID %>').attachEvent('click', function() {
     return AlertOnGo('View Configuration',
                        document.getElementById('<%= txt_name.ClientID %>').value);
});
</script>

答案 4 :(得分:0)

我更喜欢使用String.Concat。您可以使用\ u0027而不是撇号。

OnClientClick='<%#String.Concat("document.getElementById(\u0027", AddToCartBtn.ClientID.ToString(), "\u0027).value = \u0027Adding...\u0027;")%>'

答案 5 :(得分:0)

另一种解决方案是你可以去浏览器做昆虫元素读取文本框的动态ID并在javascript中设置如:

var ddlFunction = document.getElementById('ctl00_SPWebPartManager1_g_ecede3f6_2b87_405a_af1b_70401bb8d4c7_ddlFunction'); 

HTML:

<asp:DropDownList ID="ddlFunction" runat="server" CssClass="dropDownClass">
    <asp:ListItem Selected="True">Select One</asp:ListItem>
    <asp:ListItem>Sample 1</asp:ListItem>
    <asp:ListItem>Sample 2</asp:ListItem>
</asp:DropDownList>

答案 6 :(得分:0)

您可以创建一个css类并使用它而不是客户端ID(使用jQuery $('。txt_name')。text()):

输入服务器名称:

 <asp:TextBox ID="txt_name" CssClass="txt_name" runat="server"></asp:TextBox>
相关问题