无法在GridView中获取Linkbutton的文本

时间:2015-09-11 20:43:42

标签: c# asp.net gridview

这就是我试图获取LinkBut​​ton文本的方法: -

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    BorderStyle="None" AlternatingRowStyle-BorderStyle="None" CellPadding="5" 
    EditRowStyle-BorderStyle="None" HeaderStyle-BorderStyle="None" 
    PagerStyle-BorderStyle="None" RowStyle-BorderStyle="None" ShowHeader="False" 
    GridLines="None" >
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <img id="image" src='<%# Eval("pic") %>' runat="server" style="height:120px; width:150px" />
            </ItemTemplate>                   
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="click_download" runat="server" OnClick="download"><%# Eval("title") %></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

但我收到的文字是&#34;&#34; (空白)。

这是我的aspx部分: -

<table border="1" cellpadding="3" cellspacing="0" class="table_content">
   <tr>
      <td width="38" rowspan="2">NO.</td>
      <td rowspan="2">PILOT ON BOARD</td>
      <td colspan="2">PILOT ON BOARD</td>
      <td rowspan="2">PILOT WAITING TIME</td>
      <td rowspan="2">TOTAL</td>
      <td rowspan="2">BERTHING</td>
   </tr>
   <tr>
      <td width="100">DATE </td>
      <td width="100">TIME</td>
   </tr>

   <?php
        $no=0;
        $dataFromTable=json_decode('[{"PilotWaitingTime":"0.2","PilotOnBoardDate":"2013-01-01","PilotOnBoardTime":"17:40:00","PilotOnBoard":"2013-01-01 17:40:00","Berthing":"2013-01-01 20:25:00","rata2PilotWaitingTime":"9.067"},{"PilotWaitingTime":"19.2","PilotOnBoardDate":"2013-01-03","PilotOnBoardTime":"12:15:00","PilotOnBoard":"2013-01-03 12:15:00","Berthing":"2013-01-03 15:10:00","rata2PilotWaitingTime":"9.067"},{"PilotWaitingTime":"7.8","PilotOnBoardDate":"2013-01-03","PilotOnBoardTime":"14:45:00","PilotOnBoard":"2013-01-03 14:45:00","Berthing":"2013-01-03 17:05:00","rata2PilotWaitingTime":"9.067"},{"PilotWaitingTime":"6.4","PilotOnBoardDate":"2013-02-04","PilotOnBoardTime":"18:25:00","PilotOnBoard":"2013-02-04 18:25:00","Berthing":"2013-02-04 20:25:00","rata2PilotWaitingTime":"6.000"},{"PilotWaitingTime":"5.6","PilotOnBoardDate":"2013-02-06","PilotOnBoardTime":"05:40:00","PilotOnBoard":"2013-02-06 05:40:00","Berthing":"2013-02-06 08:30:00","rata2PilotWaitingTime":"6.000"},{"PilotWaitingTime":"26","PilotOnBoardDate":"2013-03-04","PilotOnBoardTime":"13:00:00","PilotOnBoard":"2013-03-04 13:00:00","Berthing":"2013-02-04 20:25:00","rata2PilotWaitingTime":"26.000"}]',true);

        foreach( $dataFromTable as $results ){
   ?>

   <tr>
      <td width="38" style="text-align:right"><?php echo $no+=1; ?></td>
      <td width="150"><?php echo $results['PilotOnBoard']; ?></td>
      <td width="100"><?php echo $results['PilotOnBoardDate']; ?> </td>
      <td width="100"><?php echo $results['PilotOnBoardTime']; ?></td>
      <td style="text-align:right"><?php echo $results['PilotWaitingTime']; ?></td>
      <td style="text-align:right"><?php echo $results['rata2PilotWaitingTime']; ?></td>
      <td style="text-align:right"><?php echo $results['Berthing']; ?></td>
   </tr>

   <?php }; ?>

</table>

LinkBut​​ton的文本在gridView内部的页面中可见,并且可以单击但是我面对查询错误而不检索任何结果,因为查询的名称是空白

2 个答案:

答案 0 :(得分:2)

我认为,由于您正在按如下方式设置按钮的文本:

<asp:LinkButton ID="click_download" runat="server" OnClick="download"><%# Eval("title") %></asp:LinkButton>

Text属性未正确设置。将<%# Eval("title") %>移动到链接声明按钮并将其值分配给Text属性:

<asp:LinkButton ID="click_download" runat="server" OnClick="download" Text='<%# DataBinder.Eval (Container.DataItem, "title") %>'></asp:LinkButton>

答案 1 :(得分:1)

我不知道你在哪里设置LinkBut​​ton的text属性/属性。但是,我确实看到了你的位置&#34;&lt;%#Eval(&#34; title&#34;)%&gt;&#34;漂浮在你的标签中。应该说Text =&#34;&lt;%#Eval(&#34; title&#34;)%&gt;&#34;。

如果没有设置,我真的不明白它是如何被查看的。你在Page_Load中设置它吗?希望这些问题有助于追究问题。