c# - How to get Dynamic Span id in jquery? -
iam using jquery in asp.net
i have 1 user control in have div , in div table , in table tr , in tr td , in td have lables.
ascx :
<div id="container" runat="server" class="dvcontainer"> <table id="table" class = "tablecs"> <thead><tr> <td colspan="2"> <asp:label id="lbl1" text="swiss" runat="server" /></td> </tr> </thead> <tr> <td>abc</td> <td>def</td> </tr> <tr> <td><asp:label id="lblpa" text="sun 12/21 05:04" runat="server" /></td> <td ><asp:label id="lblpd" text="sun 12/21 19:00" runat="server" /></td> </tr> <tr> <td><asp:label id="lblaa" text="sun 12/21 05:04" runat="server" /></td> <td ><asp:label id="lblad" text="sun 12/21 19:00" runat="server" /></td> </tr> </table>
i want bind data dynamically these user control. i.e., binding data lables in user contol.
my jquery
$div.find(".table").text(oports[icount].name); // result array of oports , have filed name
but not working fine. when checked code dynamically generating span each , every lable how find span id dynamiccaly in loop , bind data lables??
any appreciated. in advance.
suppose have usercontrol markup given below
<%@ control language="c#" autoeventwireup="true" codebehind="webusercontrol1.ascx.cs" inherits="dotnet_forms.webusercontrol1" %> <div id="container" runat="server" class="dvcontainer"> <table id="table" class="tablecs"> <tr> <td> <asp:label id="lblpa" text="sun 12/21 05:04" runat="server" /> </td> <td> <asp:label id="lblpd" text="sun 12/21 19:00" runat="server" /> </td> </tr> </table> </div>
and have registered usercontrol on .aspx
page this:
<%@ register tagprefix="uc" tagname="test" src="~/webusercontrol1.ascx" %>
and used this:
<uc:test id="uc1" runat="server" />
then, when run page, elements rendered like
<div id="uc1_container" class="dvcontainer"> <table id="table" class="tablecs"> <tr> <td> <span id="uc1_lblpa">sun 12/21 05:04</span> </td> <td> <span id="uc1_lblpd">sun 12/21 19:00</span> </td> </tr> <tr> <td> <span id="uc1_lblaa">sun 12/21 05:04</span> </td> <td> <span id="uc1_lblad">sun 12/21 19:00</span> </td> </tr> </table> </div>
see how ids of labels , other elements (elements runat="server"
attribute ) got changed i.e.
lblpa > uc1_lblpa lblpd > uc1_lblpd container > uc1_container
so, have out these changes, because can grab these elements using jquery, cause jquery client side language, executes, after server side code (runat="server") has executed.
however, if not want out modified id, can following
- remove
runat="server"
attribute, , make sure ids unique, of them - let
runat="server"
attribute their, place attributeclientidmode="static"
on of server side controls. , ids wont change. - use clientid i.e. in jquery selector, grab element this:
$('#"+'<%= lblpa.clientid %>');
now, since ids unique, don't need find, directly grab elements this:
$('#lblpa').text();
or if want loop through tds of table class tablecs
, this:
$('.tablecs tr td').each(function(index, item){ alert($(item).text()); });
Comments
Post a Comment