简体   繁体   English

在gridview内调用jquery函数

[英]call jquery function inside gridview

call jquery function inside gridview .what i want to happen when i click on the link button open the div . 在gridview内调用jquery函数。当我单击链接按钮打开div时,我想发生什么。 i have problem here with this case didn't work . 我在这里有这种情况的问题没有工作。

JavaScript JavaScript的

function toggleDiv(divId) {
    $("#shoow").toggle('slow');
}

HTML Markup HTML标记

<asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns="False" 
    CellPadding="4"
    ForeColor="#333333" 
    GridLines="None" 
    Width="507px" 
    Height="294px">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("CategoryName")%>'></asp:Label>
                <br />
                <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="toggleDiv('Shoow'); return false;">
                    LinkButton</asp:LinkButton>
                <div id="Shoow" style="background-color: blue; width: 150px; height: 150px; display: none;">
                </div>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("Description")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

Make these changes 进行这些更改

Markup 标记

  1. Pass the clicked element as itself like toggleDiv(this) toggleDiv(this)一样将单击的元素作为自身toggleDiv(this)
  2. Remove the id of the div 删除div的ID

Sample Code 样例代码

<ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Eval("CategoryName")%>'>
    </asp:Label>
    <br />
    <asp:LinkButton ID="LinkButton1" runat="server" 
            OnClientClick="javascript:toggleDiv(this);return false;">
        LinkButton</asp:LinkButton>
    <div style="background-color: blue; width: 150px; height: 150px; display: none;">
    </div>
</ItemTemplate>

JavaScript (Toggle the visibility of the element next to the element that's clicked) JavaScript(在单击的元素旁边切换元素的可见性)

function toggleDiv(elm) {
    $(elm).next().toggle('slow');
}

Whenever I need to execute JS from a Gridview I try to avoid using asp controls like LinkButton as they tend to cause a postback which you then need to prevent, which can be done, but I find it far easier to just embed a basic html button or img tag with an onclick set for your JS. 每当我需要从Gridview执行JS时,我都尽量避免使用诸如LinkBut​​ton之类的asp控件,因为它们往往会导致回发,然后您需要阻止回发,这可以做到,但是我发现嵌入一个基本的html按钮要容易得多或带有JS的onclick设置的img标签。 No postback and no extra code to prevent a postback. 没有回发,也没有额外的代码来防止回发。

<asp:TemplateField>
  <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Eval("CategoryName")%>'></asp:Label>
    <br />
    <button ID="LinkButton1" runat="server">Button Text</button>
    <div id="Shoow" runat=server style="background-color: blue; width: 150px; height: 150px; display: none;">
    </div>
  </ItemTemplate>
</asp:TemplateField>

Then in the OnRowDatabound event you do the following because runat="server" has been set: 然后在OnRowDatabound事件中执行以下操作,因为已设置了runat="server"

dim btn as HtmlButton = e.Row.FindControl("LinkButton1")
dim div as HtmlContainer = e.Row.FindControl("Shoow")

dim js as String = String.Format("toggleDiv('#{0}')", div.UniqueID)
btn.Attributes.Add("onclick", js)

and the JS: 和JS:

function toggleDiv(divId) {
  $(divId).toggle('slow');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM