【问题标题】:Fire Gridview OnRowCommand on clicking label单击标签时触发 Gridview OnRowCommand
【发布时间】:2014-08-08 13:50:40
【问题描述】:

当我单击 GridView 的标签时,我无法执行 RowCommand。

这是我的网格视图代码。

<asp:GridView ID="JobsGridView" runat="server" OnRowCommand="JobsGridView_RowCommand" EmptyDataText="No builds found" HeaderStyle-CssClass="HeaderColors" AutoGenerateColumns="False" ShowHeader="false">
  <Columns>
     <asp:ButtonField Text='SingleClick' CommandName="SingleClick" Visible="False"/>
     <asp:TemplateField>
         <ItemTemplate>
             <asp:Label ID="JobIdLabel" runat="server" Text='<%# Bind("BrokenProject") %>' ></asp:Label>
         </ItemTemplate>
     </asp:TemplateField>
   </Columns>
 <RowStyle Width="100%" CssClass=""></RowStyle>
</asp:GridView>

如何在单击标签时调用JobsGridView_RowCommand 函数?如果我使按钮域可见,我可以看到按钮旁边显示 SingleClick,它确实调用了该函数,但我不想要这个额外的按钮。我只想单击列表中的名称并发生一些事情。

我知道期望单击标签执行任何操作似乎很愚蠢,但标签似乎没有事件,我一直在查看一些类似的代码,它们会执行类似的操作...

【问题讨论】:

  • Label 不会让你走运。也许尝试在客户端上不可见的一行中创建一个 LinkBut​​ton 并模拟单击它。或手动使用__doPostBack 提出请求。
  • 我同意@Andrei。 LinkBut​​ton 是解决方案。您可以使用 CSS 使其看起来像标签。

标签: c# asp.net gridview webforms


【解决方案1】:

您可以改用 LinkBut​​ton。

<asp:LinkButton ID="JobIdLink" runat="server"
    CommandName="SomeCommand"
    Text='<%# Bind("BrokenProject") %>' >
</asp:LinkButton>

您甚至可以设置它的样式,使其看起来不像链接,而更像标签。

a:link
{ 
    color: black; 
    text-decoration: none;
}

a:visited
{ 
    color: black; 
    text-decoration: none;
}

a:hover
{
    color: black; 
    text-decoration: none;
} 

【讨论】:

    【解决方案2】:

    除非您想编写 Javascript 来处理点击,否则您将需要一个能够回发到服务器的控件,就像 Andrei 提到的那样。输入(例如按钮)和超链接可以做到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多