Juan Garcia
Juan Garcia

Reputation: 305

GridView Template Issue with Javascript

Im working in ASP.net with C#, i need help to correct or have an approach to get what i need. Im working with gridview, data select a set of data that will be used depending of its primarykey into another table.

On the runtime on client side, i need to collect on the onclick event, the sid column of each row and put it into a hidden field. However, the code below, is not working for me as the <%#eval("sid"); %>! is being read as a string, than the current row value.

What i need is the checkbox once clicked alert(5) instead of alert('<%#eval("sid"); %>!'); that is what is currently doing.

<Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox runat="server" ID="cbSelect" onclick="javascript:alert('<%#eval("sid"); %>!');"/>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:HyperLinkField DataTextField="nombre" NavigateUrl="http://www.google.com" HeaderText="direccion"/>
            <asp:BoundField DataField="sid" HeaderText="sid" InsertVisible="False" 
                ReadOnly="True" SortExpression="sid" />
            <asp:BoundField DataField="nombre_archivo" HeaderText="nombre_archivo" 
                SortExpression="nombre_archivo" />
</Columns>

If further information is needed, please ask what you need me to add to the question.

Upvotes: 1

Views: 117

Answers (3)

user240141
user240141

Reputation:

Try changing your code to this. And your Eval() was actually eval() which is invalid.

<Columns>
 <asp:TemplateField>
  <ItemTemplate>
   <asp:CheckBox runat="server" 
        ID="cbSelect" 
    onclientclick='<%#string.Format("javascript:alert('{0}');",Eval("sid"))%>'/>
   </ItemTemplate>
  </asp:TemplateField>
 <asp:HyperLinkField DataTextField="nombre" 
     NavigateUrl="http://www.google.com" HeaderText="direccion"/>
  <asp:BoundField DataField="sid" HeaderText="sid" InsertVisible="False" 
                ReadOnly="True" SortExpression="sid" />
      <asp:BoundField DataField="nombre_archivo" HeaderText="nombre_archivo" 
                SortExpression="nombre_archivo" />
</Columns>

Upvotes: 0

afzalulh
afzalulh

Reputation: 7943

Try this, worked for me:

<asp:CheckBox runat="server" ID="cbSelect" onclick='<%# "javascript:alert(" + Eval("sid") + " );" %>'/>

Upvotes: 1

Mister Epic
Mister Epic

Reputation: 16713

I believe Eval is case-sensitive, and you don't need that semi-colon:

 <%# Eval("sid") %>

Upvotes: 0

Related Questions