Note that there are some explanatory texts on larger screens.

plurals
  1. POPrevent a LinkButton inside ItemTemplate from postback and execute a javascript function using Evaluated val as parameter
    text
    copied!<p>I dont know why this is giving me such a hard time, may be coding all day long has finally made my head smoke. I am trying to call a js function on client click of a link button inside an item template. I am using the Eval method to pass one of the parameters to my js function and prevent postback. However, it postbacks and never calls my function. If i take the evaluated parameter it works. </p> <pre><code> &lt;Columns&gt; &lt;asp:BoundField DataField="media_id" Visible="false" HeaderText="" /&gt; &lt;asp:BoundField DataField="dir_path" Visible="false" HeaderText="" /&gt; &lt;asp:TemplateField HeaderText="Date Taken"&gt; &lt;ItemTemplate&gt; &lt;asp:LinkButton ID="dateLinkCsBtn" OnClientClick='&lt;%#Eval("dir_path","javascript:return test({0},event);")%&gt;' CommandArgument='&lt;%#Eval("dir_path") %&gt;' runat="server" Text='&lt;%# Bind("date") %&gt;'&gt;&lt;/asp:LinkButton&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:BoundField DataField="description" Visible="true" HeaderText="Media Type" /&gt; &lt;/Columns&gt; </code></pre> <p>and this is the js function: </p> <pre><code>&lt;script&gt; function test(dir_path,event) { alert(dir_path); event.preventDefault(); }; &lt;/script&gt; </code></pre> <p>UPDATE : Replacing OnClientClick from </p> <pre><code>OnClientClick='&lt;%#Eval("dir_path","javascript:return test({0},event);")%&gt;' </code></pre> <p>to : </p> <pre><code>OnClientClick='&lt;%#Eval("dir_path","Javascript:return test(\"{0}\",event);")%&gt;' </code></pre> <p>Works but the output produced ignores all '\' and returns :<br> <code>C:KBlog_MediaColorSnapshota4dde1-76fc-4e2e-9100-f12f48427e24.png</code> instead of </p> <pre><code>C:\KBlog_Media\1\ColorSnapshot\00a4dde1-76fc-4e2e-9100-f12f48427e24.png </code></pre> <p>SOLUTION: Update the datasource datatable of the gridview to display double backslash for dir_path "\" instead of single backslash. It worked like a charm </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload