简体   繁体   English

无法使用JQuery在Gridview中找到动态asp:Control

[英]Cannot Find dynamic asp:Control inside a Gridview using JQuery

In an ASP.Net Application I have an aspx file inside which I have an Update Panel with a Gridview Inside it. 在ASP.Net应用程序中,我有一个aspx文件,其中有一个带有Gridview的更新面板。 Inside the Gridview there is a table which also contains some panels. 在Gridview内部有一个表格,其中也包含一些面板。 There are some controls inside those panels. 这些面板中有一些控件。

ASPX ASPX

<asp:Gridview ID="GridView1" runat = "server"...>
 <Columns>
  <asp:TemplateField ShowHeader="False">
   <ItemTemplate>
     <table>
         <asp:Panel ID="pnlVisualAst" runat="server" Visible="false">
              <tr>
                <td style="text-indent: 50px; width: 25%">
                  <asp:Label ID="lblVisAst" runat="server" Text="Visual Assitance"></asp:Label>
                </td>
                <td colspan="3">
                  <asp:Button ID="btnVisAst" class="button"  runat="server" Text="View"/>
                  <asp:Image ID="popImg" ImageUrl='<%# GetImage(Eval("VA_Img"))%>' runat="server" Visible="false" />
                </td>
              </tr>
        </asp:Panel>
     </table>
   </ItemTemplate>
  </asp:TemplateField >
 </Columns>
</asp:Gridview>

Now, I am writing a JQuery function to capture the click event for the button inside the Gridview and show the image field in a pop up window. 现在,我正在编写一个JQuery函数,以捕获Gridview中按钮的click事件,并在弹出窗口中显示image字段。

JQuery jQuery查询

<script type="text/javascript">

    $(document).on('ready', function ()
    {
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_endRequest(function ()
        {
            $('#<%=GridView1.ClientID%>').find($('#<%= btnVisAst.ClientID %>')).click(function ()
            {
                $('#<%=popImg.ClientID%>').dialog
                  ({
                      title: "my jquery popup",
                      width: 430,
                      height: 200,
                      modal: true,
                      button:
                          {
                              Close:
                              function ()
                              {
                                  $(this).dialog('Close');
                              }
                          }
                  });
                $(this).prop('checked', true);

            });
        });

    });

The Problem with the above code is it cannot find the the dynamic controls inside that Gridview. 上面的代码的问题是它找不到该Gridview内部的动态控件。 It shows the 'btnVisAst' and 'popImg' does not exist in the current context. 它显示在当前上下文中不存在“ btnVisAst”和“ popImg”。

Is there any way to solve this problem? 有什么办法解决这个问题? Thanks. 谢谢。

I agree with ConnorsFan. 我同意ConnorsFan。 Use classes instead. 改用类。 If you apply a class to your visit button, say "classVisAst", then you could do something like this: 如果您将类别应用于访问按钮,例如说“ classVisAst”,则可以执行以下操作:

$('.classVisAst').click(function ()
        {
            var newDialog = $(this).siblings("img").dialog
              ({
                  title: "my jquery popup",
                  width: 430,
                  height: 200,
                  modal: true,
                  button:
                      {
                          Close:
                          function ()
                          {
                              $(this).dialog('Close');
                          }
                      }
              });
            $(newDialog).dialog('open');

            $(this).prop('checked', true);

        });

Then you're not worrying about IDs at all because everything is relative. 然后,您根本不必担心ID,因为一切都是相对的。

Instead of searching button assign a function to it. 代替搜索按钮,而是为其分配功能。

<asp:Gridview ID="GridView1" runat = "server"...>
 <Columns>
  <asp:TemplateField ShowHeader="False">
   <ItemTemplate>
         <asp:Panel ID="pnlVisualAst" runat="server" Visible="false">
     <table><%--put table inside panel else you will have empty table element --%>
              <tr>
                <td style="text-indent: 50px; width: 25%">
                  <asp:Label ID="lblVisAst" runat="server" Text="Visual Assitance"></asp:Label>
                </td>
                <td colspan="3">
                  <asp:Button ID="btnVisAst" OnClientClick="return doSomething(this)" class="button"  runat="server" Text="View"/>
                  <asp:Image ID="popImg" ImageUrl='<%# GetImage(Eval("VA_Img"))%>' runat="server" Visible="false" />
                </td>
              </tr>
     </table>
        </asp:Panel>
   </ItemTemplate>
  </asp:TemplateField >
 </Columns>
</asp:Gridview>

javascript: javascript:

function doSomething(elem){//elem == input[type=submit]
    $('#'+ elem.id.replace('btnVisAst','popImg')).dialog
      ({
          title: "my jquery popup",
          width: 430,
          height: 200,
          modal: true,
          button:
              {
                  Close:
                  function ()
                  {
                      $(this).dialog('Close');
                  }
              }
      });
    $(elem).prop('checked', true);
    return false;
}

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

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