admin管理员组

文章数量:1305107

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

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

Share Improve this question asked Mar 21, 2012 at 16:26 SinaestheticSinaesthetic 12.2k30 gold badges114 silver badges185 bronze badges 1
  • Have you looked at the rendered View Source to see the input type="submit" markup to make sure it's what you'd expect? – Alex Commented Mar 21, 2012 at 16:32
Add a ment  | 

5 Answers 5

Reset to default 6

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;
}

You can't have inline code in a property for a server control.

Generally I would do this in the codefile.

    for (int j = 0; j < this.lsvTest.Items.Count; j++)
    {
        var btnSubmit = (Button)this.lsvTest.Items[j].FindControl("btnSubmit");
        var txtTextBox = (TextBox)this.lsvTest.Items[j].FindControl("txtTextBox");
        btnSubmit.Attributes["onclick"] = string.Format("ClientIDS('{0}')", txtTextBox.ClientID);
    }

Java Script Code

<script type="text/javascript">
    function ClientIDS(Btnid) {
        var textBoxID = Btnid.id.replace('btnSubmit', 'txtTextBox');
        var info = document.getElementById(textBoxID).value;
        alert(info);
        return false;
    }
</script>

HTML Code

<asp:ListView runat="server" ID="lsvTest">
    <ItemTemplate>
        <asp:TextBox runat="server" ID="txtTextBox" />
        <asp:Button runat="server" ID="btnSubmit" Text='<%#Eval("Name") %>' OnClientClick="ClientIDS(this)" />
    </ItemTemplate>
</asp:ListView>

first of all, you're using javascript to do what you're jQuery library should be doing.

For example:

var info = document.getElementById(id).value;
//  is easier as
var info = $("#"+id).val();

Secondly, it's been awhile, but i dont think you can put your asp script text in the form like that

You can use ItemDataBound event for the repeater to make this. See the following code

protected void lsvTest_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    string txtbox = e.Item.FindControl("txtTextBox").ClientID;
    Button btn = e.Item.FindControl("btnSubmit") as Button;
    btn.OnClientClick = string.Format("ClientIDS('{0}')", txtbox);
}

本文标签: jquerypassing clientID to javascript and extracting a valueclarificationStack Overflow