How to assign C# variable to asp element using javascript -
i want assign csharp variable element of asp page using javascript. seems assignement not working in code.
here is:
document.getelementbyid('lbaccessories').innerhtml = '<%#selectlabel%>';
in asp code use:
<asp:linkbutton id="lbaccessories" runat="server" />'
i can't assign value directly linkbutton using text='<%#selectlabel%>'
because want make more intelligent.
does knows how that?
thanks
edit:
here code, i've tried use <%=lbaccessories.clientid%>
, generates error : lbaccessories not exist in context.
<script type="text/javascript"> function function(ref) { if ('<%=textboxclientid%>' == 'txtlink') { document.getelementbyid('lbaccessories').innerhtml = '<%#selectlabel%>'; } else if ('<%=textboxclientid%>' == 'btnsearch') { document.getelementbyid('lbaccessories').innerhtml = '<%#viewdetail%>'; } } </script>
edit:
<asp:gridview id="gv1" autogeneratecolumns="false" runat="server" allowpaging="true" onpageindexchanging="pic"> <columns> <asp:templatefield headertext=""> <itemtemplate> <asp:linkbutton id="lbaccessories" runat="server" onclientclick='<%#string.format("passaccessory(\"{0}\");", eval("ref"))%>'></asp:linkbutton> </itemtemplate> </asp:templatefield> </columns> </asp:gridview>
edit:
thanks had posted suggestions.
i have changed javascript code following:
<script type="text/javascript"> function passaccessory(accessoryref) { if ('<%=textboxclientid%>' == 'txtlink') { document.getelementbyid('<%= gvaccessories.findcontrol("lbaccessories").clientid %>').innerhtml = '<%#selectlabel%>'; } else if ('<%=textboxclientid%>' == 'btnsearch') { document.getelementbyid('<%= gvaccessories.findcontrol("lbaccessories").clientid %>').innerhtml = '<%#viewdetail%>'; } } </script>
in *.aspx.cs
file, have added: protected linkbutton lbaccessories { get; set; }
it throws exception:
object reference not set instance of object
anyone have ideas? much
edit:
finally, did csharp code. it's more flexable , manageable. colleague helped me. thank help!!!
check output html whether anchor's id looking for. in case should rather use <%= lbaccessories.clientid %>
refering server id client script.
if reason don't want (or cannot) use <%= lbaccessories.clientid %>
check following article jquery plugin allows dom element server id:
http://radek-stromsky.blogspot.com/2010/11/aspnet-how-to-get-client-id-with-jquery.html
edit:
i made several changes in code:
<script type="text/javascript"> function passaccessory(ref) { if ('<%= textboxclientid %>' == 'txtlink') { document.getelementbyid('<%= gridview1.findcontrol("lbaccessories").clientid %>').innerhtml = '<%# selectlabel %>'; } else if ('<%= textboxclientid %>' == 'btnsearch') { document.getelementbyid('<%= gridview1.findcontrol("lbaccessories").clientid %>').innerhtml = '<%#viewdetail%>'; } } </script> <asp:linkbutton id="lbaccessories" runat="server" onclientclick='<%#string.format("passaccessory(\"{0}\");", eval("ref"))%>'></asp:linkbutton>
edit: edit:
i fixed code above. there gridview1.findcontrol("lbaccessories")
Comments
Post a Comment