Hi I am using jquery to get the values from my server tag in ASP.NET. All that javascript are on the same page. I have tried this
<asp:TextBox ID="LastNameTextBox" runat="server" Text='<%# Bind("LastName") %>' />
and the jquery
$("#<\%=LastNameTextBox.ClientID%>").val()
notice I have to escape the % so that the whole quote will be a string. I don't know why this is like that; the examples I saw did not need to escape the %. I also found in another post that I should use the class so I did this
<asp:Label ID="LastNameLabel" runat="server" Class = LastNameClass" Text='<%# Bind("LastName") %>' />
and the jquery is this
$(".LastNameClass").val()
This got me further but it returned me the function not the value. I change val() to text() nothing. I am thinking that it because I am binding the data and that the tags are in a template that I am using in a form? Any insight or advice on what I should try next?
Edit: Here is the Code
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="SchoolDataContext" EnableUpdate="True" EntityTypeName=""
TableName="People">
</asp:LinqDataSource>
<asp:FormView ID="FormView1" runat="server" AllowPaging="True"
DataKeyNames="PersonID" DataSourceID="LinqDataSource1">
<EditItemTemplate>
PersonID:
<asp:Label ID="PersonIDLabel1" runat="server" class="PersonalIDLablel1" Text='<%# Eval("PersonID") %>' />
<br />
LastName:
<asp:TextBox ID="LastNameTextBox" runat="server" class="LastNameClass1" onBlur="textBlur();"
Text='<%# Bind("LastName") %>' />
<br />
FirstName:
<asp:TextBox ID="FirstNameTextBox" runat="server"
Text='<%# Bind("FirstName") %>' />
<br />
HireDate:
<asp:TextBox ID="HireDateTextBox" runat="server"
Text='<%# Bind("HireDate") %>' />
<br />
EnrollmentDate:
<asp:TextBox ID="EnrollmentDateTextBox" runat="server"
Text='<%# Bind("EnrollmentDate") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
PersonID:
<asp:Label ID="PersonIDLabel" runat="server" class="PersonalIDLablel" Text='<%# Eval("PersonID") %>' />
<br />
LastName:
<asp:Label ID="LastNameLabel" runat="server" class="LastNameClass" Text='<%# Bind("LastName") %>' />
<br />
FirstName:
<asp:Label ID="FirstNameLabel" runat="server" Text='<%# Bind("FirstName") %>' />
<br />
HireDate:
<asp:Label ID="HireDateLabel" runat="server" Text='<%# Bind("HireDate") %>' />
<br />
EnrollmentDate:
<asp:Label ID="EnrollmentDateLabel" runat="server"
Text='<%# Bind("EnrollmentDate") %>' />
<hr/>
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" />
</ItemTemplate>
</asp:FormView>
</form>
You can use either text()
or html()
.
<asp:Label runat="server" ID="LastNameLabel" Text="This is a text"
ClientIDMode="Static"></asp:Label>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var text = $("#LastNameLabel").text();
alert(text);
});
</script>
Make sure to use ClientIDMode="Static"
since LastNameLabel
's Id is not available outside of FormView
.
Then you can just use #LastNameLabel
in jQuery selector.