1

I have a grid with a link and what I want to achieve is when the link is clicked, to open a modal window. My modal is working properly. I'm just having issue with getting the link to do something. Below is my example code.

ASPX:

<asp:GridView AutoGenerateColumns="False" Width="100%" ID="grvUsers" runat="server">
    <Columns>
        <asp:TemplateField HeaderText="Delete">
            <ItemTemplate>
                <asp:LinkButton ID="Label1" runat="server" Text='<%# eval("ID") %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

HTML render:

<table cellspacing="0" rules="all" border="1" id="grvUsers" style="width: 100%; border-collapse: collapse;">
    <tr>
        <th scope="col">Delete</th>
    </tr>
    <tr>
        <td>
            <span id="grvUsers_Label1_0">23</span>
        </td>
    </tr>
</table>

jQuery:

$(document).ready(function(){
    $('td:nth-col(0)').click(function(){
        alert("OMG");
    });
});
2
  • Where in that HTML do you see a <tbody/> element? Commented May 26, 2011 at 15:57
  • does it work with built-in jquery selector :first-child. what is the result of just $('td:nth-col(0)'), do you have a result ? Commented Nov 21, 2011 at 19:08

2 Answers 2

1

Instead of trying to add the event with js can you just use :

<asp:LinkButton ID="Label1" onClick="myAlertFunction();" runat="server" Text='<%# eval("ID") %>'></asp:LinkButton>

I would recommend you check out this jquery documentation: Jquery Dialog w/Modal

Sign up to request clarification or add additional context in comments.

3 Comments

ok, so when I call that function, how do I display the modal? Or should I say how do I call jquery from javascript function? sorry, I'm a noob.
@jack Is this a question about capturing a click event, or a question about how to design a modal popup?
capturing an event. what i want to happen is when the link is click, it opens the modal window.
0
  1. You can just use ajax for the modal popup, works a whole lot better than using jquery library(there are exceptions..)

  2. You can use the a select command field as a delete and handle the event in your SelectIndexchanging or Delete either one.

Add this to your gridview

<asp:CommandField HeaderText="Delete" ButtonType="Link" ShowSelectButton="true"
SelectText='<%# eval("ID") %>' />

Codebehind:

protected void mygridview_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
        //here you can do anything with the id
        string id = mygridview.Rows[e.NewSelectedIndex].Cells[1].Text;

        mdlPopup.Show();
}

Ajax popup:

<asp:Button id="btnShowPopup" runat="server" style="display:none" />
<ajaxToolkit:ModalPopupExtender
ID="mdlPopup" 
runat="server" 
TargetControlID="btnShowPopup" 
PopupControlID="pnlPopup" 
CancelControlID="btnClose" 
BackgroundCssClass="Inactive" />
<asp:Panel ID="pnlPopup" runat="server" Width="500px" style="display:none;">
  <!--here you put any content that will go inside your popup-->
</asp:Panel>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.