I have a sharepoint website which has multiple instances of the same visual webpart. On button click I am trying to get a textbox present in the usercontrol of the webpart using document.getelementbyid('<%textboxID.ClientID%>'). I get a textbox but it is always from the last instance of the webpart.
I have seen few articles related to document.getelementbyid not returning the controls according to their ID but think that's not the case here as I get a relevent control but from a diffrent instance of webpart.
Below is the srtipped down version on the web part's user control,
<script language="javascript" type="text/javascript">
function Excel_Clicked() {
var reportname = document.getElementById('<%=txtReportName.ClientID%>').value;
} </script>
<asp:HiddenField ID = "ReportName" runat="server"/>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="mainPanel" runat="server" Width="98%">
<asp:TextBox ID="txtReportName" runat="server" Visible="true"></asp:TextBox>
<asp:Panel ID="Panel1" runat="server" Height="350px">
<div>
<img id='ExcelImg' onclick="return Excel_Clicked();" src="~/_layouts/SmallExcel.png"
style="height: 20px; width: 20px; cursor: pointer" alt="Excel" />
</div>
</asp:Panel>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
This webpart is dropped on a sharepoint webpage 3-4 time with slightly diffrent configurations. As I started earlier, the Excel_Clicked() function tries to get 'txtReportName' textbox on the current webpart but ends up with the textbox in the last instance of webpart . Needless to say that if there is only one instance of the webpart it work fine.
document.getElementById('<%= textboxID.ClientID %>').value
correctly