从客户端获取telerik网格中选定行的值

时间:2017-03-08 06:58:49

标签: javascript jquery asp.net telerik

我试图在客户端使用javascript获取GridFoundColumn的值DataField =“id”。每当用户点击该行的图像按钮时,我应该检索该行的id并调用传递此id的web方法...我在同一个aspx页面中尝试过这个脚本块:

<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
    <script type="text/javascript">
      function test(object) {
         try {
           var master = $find('<%= RadGrid1.ClientID %>').get_masterTableView(); 
           var selectedItems= master.get_selectedItems()[0].getDataKeyValue("id"); 
           alert(selectedItems);
         } catch (err) {alert(err);}
      }
    </script>
</telerik:RadCodeBlock>

但是抛出了一个错误:无法读取未定义的属性getDataKeyValue ... 我发现所选项目的长度为0:

var selectedItems= master.get_selectedItems().length;  // selectedItems = 0

这里错过了什么?我怎样才能获得id值?下面是我的telerik网格代码:

    <telerik:RadGrid ID="RadGrid1" runat="server" AllowSorting="True" AllowPaging="True" AutoGenerateColumns="False" Width="100%" ShowFooter="true" ItemStyle-Wrap="false" FooterStyle-Wrap="false" FooterStyle-Width="100px" PagerStyle-Wrap="false" EnableEmbeddedSkins="false"                        GridLines="None" EnableViewState="false">
     <HeaderContextMenu EnableEmbeddedSkins="False"></HeaderContextMenu>
     <FooterStyle Wrap="False"></FooterStyle>
     <ItemStyle Wrap="False"></ItemStyle>
     <PagerStyle Visible="true" PagerTextFormat="Change page: {4}    <b>{5}</b> items in <b>{1}</b> pages" />

     <MasterTableView DataKeyNames="id" ClientDataKeyNames="id"  PageSize="15" TableLayout="Fixed">

       <NoRecordsTemplate>
         <div> No records to display.</div>
       </NoRecordsTemplate>

       <Columns>
         <telerik:GridBoundColumn DataField="id" ReadOnly="True" UniqueName="id"
                                        Visible="false">
         </telerik:GridBoundColumn>
         <telerik:GridTemplateColumn UniqueName="TemplateColumn" ItemStyle-Width="35px" HeaderStyle-Width="35px">
           <ItemTemplate>
             <asp:ImageButton ID="imgUpload" runat="server" CausesValidation="False" ClientIDMode="Static" data-id='<%# Eval("id") %>' ImageUrl="~/Images/Misc/File-Upload-icon.png"  OnClientClick="test(this);" TabIndex="-1" ToolTip="Upload" />
           </ItemTemplate>                                    
           <HeaderStyle Width="35px"></HeaderStyle>
           <ItemStyle Width="35px"></ItemStyle>
         </telerik:GridTemplateColumn>
      </Columns>

   </MasterTableView>
   <ClientSettings>
   </ClientSettings>
   <FilterMenu EnableEmbeddedSkins="False" EnableEmbeddedBaseStylesheet="False">
   </FilterMenu>
   <SortingSettings EnableSkinSortStyles="False" />
 </telerik:RadGrid>

修改

我也试过这个解决方案:

var imgUpload = document.getElementById("imgUpload");
var imgUpload_ID = imgUpload.getAttribute('data-id');
alert(imgUpload_ID);

var Clicked_rowIndex = object.parentNode.parentNode.rowIndex;
alert(Clicked_rowIndex);     

关于Clicked_rowIndex:每次点击都会返回正确的rowindex(1,2,3 ....) 但是对于所有行,它显示相同的id(第一行的id); 如何在Clicked_rowIndex和imgUpload_ID之间组合以获取所选row_index的id?

2 个答案:

答案 0 :(得分:1)

好吧我终于找到了它:

from bs4 import BeautifulSoup
import urllib2
import shutil
import requests
from urlparse import urljoin
import sys
import time

def make_soup(url):
    req = urllib2.Request(url, headers={'User-Agent' : "Magic Browser"}) 
    html = urllib2.urlopen(req)
    return BeautifulSoup(html, 'html.parser')

def get_images(url):
    soup = make_soup(url)
    images = [img for img in soup.findAll('img')]
    print (str(len(images)) + " images found.")
    print 'Downloading images to current working directory.'
    image_links = [each.get('src') for each in images]
    for each in image_links:
        try:
            filename = each.strip().split('/')[-1].strip()
            src = urljoin(url, each)
            print 'Getting: ' + filename
            response = requests.get(src, stream=True)
            # delay to avoid corrupted previews
            time.sleep(1)
            with open(filename, 'wb') as out_file:
                shutil.copyfileobj(response.raw, out_file)
        except:
            print '  An error occured. Continuing.'
    print 'Done.'

if __name__ == '__main__':
    #url = sys.argv[1]
    get_images('https://i1.adis.ws/i/jpl/sz_093868_a?qlt=80&w=600&h=672&v=1')

答案 1 :(得分:0)

var id = $find("<%= RadGrid1.ClientID %>").get_masterTableView().get_selectedItems()[0].getDataKeyValue("Id");