Do not receive ClientID in ASP.Net

I have an ASP.Net page where I call a JavaScript function as follows:

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!" /> 

But when I press the GO button, I get the following error:

JavaScript runtime error: cannot get property value undefined or null reference

When viewing the displayed code for a button:

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

It does not put the corresponding ClientID. I tried to set CLientIDMode to static for the test window, but it didn't help.

This question is similar to this unanswered question .

+4
source share
6 answers

There are several solutions to this problem.

One simple way would be to move the name into a JavaScript variable, because the problem only occurs in the control when trying to evaluate txt_name.ClientID inside the OnClientClick attribute of the ASP.NET control.

 <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!" /> 
+7
source

you can use the ClientIDMode="Static" property inside the asp: textbox tag

like this

 <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!" /> 

so the client id in the text box will be the same as the id, and you can use document.getElementById('txt_name').value

and it works for me

+3
source

- edited answer - ASPX

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

script

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

 // your code

 return false;
 }
0
source

You use <% =%> in the asp.net web control property - I would not expect this to work, since the entire control will be replaced with html.

I would move your javascript to a script block and bind an event handler in the code, rather than inserting it into a string.

 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> 
0
source

I prefer to use String.Concat. Instead of the apostrophe, you can use \ u0027.

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

Another solution: you can go to the browser so that the insect element reads the dynamic identifier of the text field and sets it to 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> 
0
source

All Articles