最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to pass csharp binding variable to Javascript function - Stack Overflow

programmeradmin2浏览0评论

I want to pass c# binding variable to javascript function. Here is my code:

<asp:LinkButton ID="lbID" runat="server" Text="<%#Text%>" OnClientClick="passAccessory('<%#Bind(variable)%>'); "></asp:LinkButton>

But it always generates an error : "htmlfile: Not implemented".

And the code is not intepreted onclick="passAccessory('&lt;%#Bind(variable)%>');"

Does anyone knows how to solve it ?

Thanks.

Edit: I have changed the code to

<asp:LinkButton ID="lbID" runat="server" Text="<%#Text%>" OnClientClick="passAccessory('<%# Eval("lblTest.ClientID") %>');"></asp:LinkButton>

And this is the code it's related to, it uses the lblTest.ClientID

<asp:Label ID="lblTest" runat="server" Text='<%#Bind("reference")%>' />

But i received an error: HttpException. Databinding ... does not contain a property with the name lblTest. Is there any wrong with my code.

Edit: These posts gave me a clue.

.aspx

I am trying to learn how to bind an IEnumerable LINQ collection to a repeater

Now i have want to use

<asp:LinkButton ID="lbTest" runat="server" Text='<%#Text%>' OnClientClick='<%#string.Format("passAccessory(\"{0}\");", Eval("Ref"))%>'></asp:LinkButton>

I want to pass c# binding variable to javascript function. Here is my code:

<asp:LinkButton ID="lbID" runat="server" Text="<%#Text%>" OnClientClick="passAccessory('<%#Bind(variable)%>'); "></asp:LinkButton>

But it always generates an error : "htmlfile: Not implemented".

And the code is not intepreted onclick="passAccessory('&lt;%#Bind(variable)%>');"

Does anyone knows how to solve it ?

Thanks.

Edit: I have changed the code to

<asp:LinkButton ID="lbID" runat="server" Text="<%#Text%>" OnClientClick="passAccessory('<%# Eval("lblTest.ClientID") %>');"></asp:LinkButton>

And this is the code it's related to, it uses the lblTest.ClientID

<asp:Label ID="lblTest" runat="server" Text='<%#Bind("reference")%>' />

But i received an error: HttpException. Databinding ... does not contain a property with the name lblTest. Is there any wrong with my code.

Edit: These posts gave me a clue.

http://www.west-wind./Weblog/posts/5364.aspx

I am trying to learn how to bind an IEnumerable LINQ collection to a repeater

Now i have want to use

<asp:LinkButton ID="lbTest" runat="server" Text='<%#Text%>' OnClientClick='<%#string.Format("passAccessory(\"{0}\");", Eval("Ref"))%>'></asp:LinkButton>
Share edited May 23, 2017 at 11:44 CommunityBot 11 silver badge asked Nov 30, 2010 at 8:39 SUN JiangongSUN Jiangong 5,31216 gold badges60 silver badges77 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

You don't want a two way bind, but rather an Eval.

<asp:LinkButton ID="lbID" runat="server" Text="<%#Text%>" OnClientClick='passAccessory(\"<%# Eval("variable") %>\");'></asp:LinkButton>

I didn't do ASP.NET webforms for along time, but I believe it should be like this:

OnClientClick="<%# string.Format("passAccessory('{0}');", Container.DataItem("variable")) %>"

You can do this easily by changing the OnClientClick event from Code-Behind.

lbID.OnClientClick = "passAccessory(' " + variable + "')";
发布评论

评论列表(0)

  1. 暂无评论