Search code examples
javascriptjqueryasp.netclientid

passing clientID to javascript and extracting a value, clarification


Forgive the noob question, but I'm trying to get my head wrapped around this. I have some controls that are inside of an in a listview that are used to submit some information. I'm running a test pattern here to do this, but I'm getting object undefined errors. All of the articles I've seen about this are kind of vague. In this example, I'm trying to pass the id of a textbox, then pull the value from that in javascript. Can you tell me what I'm doing wrong?

<form id="form1" runat="server">
        <asp:ScriptManager runat="server" />
        <script type="text/javascript">
            function ClientIDS(id) {
                var info = document.getElementById(id).value;
                alert(info);
                return false;
            }
        </script>
        <asp:ListView runat="server" ID="lsvTest">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="txtTextBox" />
                <asp:Button runat="server" ID="btnSubmit" Text='<%#Eval("Name") %>' OnClientClick="ClientIDS('<%=txtTextBox.ClientID %>')" />
            </ItemTemplate>
        </asp:ListView>

    </form>

Thanks


Solution

  • if you are want to doing using jquery than try this

    just replace in button onclientclick with this one

       OnClientClick="ClientIDS(this);"
    

    and than find your previous input text in to asp:listview

    function ClientIDS(obj) {
    
       var txt = $(obj).prevAll('input[id*="txtTextBox"]:first');
       alert($(txt).val());
    
       return false;
    }