2013-06-19 50 views
2

我讀過幾十對這個問題的帖子,並嘗試所有這些,但沒有運氣,我不知道我缺少什麼,這是GridView控件代碼:LinkBut​​ton的點播,的onClick,RowCommand在網格視圖不點火

<asp:GridView ID="recentJobsGridView" runat="server" CellPadding="4" ForeColor="#333333" 
          GridLines="None" Height="151px" Width="541px" Visible="False" AutoGenerateColumns="False" 
          PageSize="5" AllowPaging="True" OnPageIndexChanging="recentJobsGridView_PageIndexChanging" 
          DataKeyNames="orderItemId"> 
          <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
          <Columns> 
           <asp:TemplateField HeaderText="Order Date" Visible="true"> 
            <ItemTemplate> 
             <asp:Label ID="Label1" runat="server" Text='<%# Bind("orderItemId") %>'></asp:Label> 
            </ItemTemplate> 
           </asp:TemplateField> 
           <asp:TemplateField HeaderText="Order Date"> 
            <ItemTemplate> 
             <asp:Label ID="Label1" runat="server" Text='<%# Bind("orderDate") %>'></asp:Label> 
            </ItemTemplate> 
           </asp:TemplateField> 
           <asp:TemplateField HeaderText="QTY"> 
            <ItemTemplate> 
             <asp:Label ID="Label2" runat="server" Text='<%# Bind("QTY") %>'></asp:Label> 
            </ItemTemplate> 
           </asp:TemplateField> 
           <asp:TemplateField HeaderText="ID"> 
            <ItemTemplate> 
             <asp:Label ID="Label3" runat="server" Text='<%# Bind("ID") %>'></asp:Label> 
            </ItemTemplate> 
           </asp:TemplateField> 
           <asp:TemplateField HeaderText="Length"> 
            <ItemTemplate> 
             <asp:Label ID="Label3" runat="server" Text='<%# Bind("length") %>'></asp:Label> 
            </ItemTemplate> 
           </asp:TemplateField> 
           <asp:TemplateField HeaderText="wall"> 
            <ItemTemplate> 
             <asp:Label ID="Label3" runat="server" Text='<%# Bind("wall") %>'></asp:Label> 
            </ItemTemplate> 
           </asp:TemplateField> 
           <asp:TemplateField HeaderText="Paper Composition"> 
            <ItemTemplate> 
             <asp:Label ID="Label3" runat="server" Text='<%# Bind("paperComposition") %>'></asp:Label> 
            </ItemTemplate> 
           </asp:TemplateField> 
           <asp:TemplateField> 
            <ItemTemplate> 
             <asp:LinkButton ID="btAdd" runat="server" OnCommand="btAdd_Command" Text="Add" 
             CommandArgument='<%# Container.DataItem %>' CommandName="Add"></asp:LinkButton> 
            </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" /> 
          <SortedAscendingCellStyle BackColor="#E9E7E2" /> 
          <SortedAscendingHeaderStyle BackColor="#506C8C" /> 
          <SortedDescendingCellStyle BackColor="#FFFDF8" /> 
          <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> 
         </asp:GridView> 

這裏是我結合網格視圖:

protected void custGridView_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (custGridView.SelectedDataKey != null) 
     { 
      selectCustomer = (int)custGridView.SelectedDataKey.Value; 
      recentJobsGridView.Visible = true; 
      recentJobsGridView.DataSource = ViewDataSource(selectCustomer); 
      recentJobsGridView.DataBind(); 
     } 

    } 

,這是LinkBut​​ton的的代碼:

protected void btAdd_Command(object sender, CommandEventArgs e) 
    { 
     if (e.CommandName == "Add") 
     { 
      StatusLbl.Text = "Hellooooooo"; 
     } 
    } 

我刪除了所有我只想顯示此消息但沒有響應 請幫助...

+1

如果在頁面加載時沒有'!Ispostback',gridview中的事件不會觸發。 –

+0

謝謝,但我已經在page_load – salRad

+0

我有這個問題。我在Page_PreRender中綁定了網格的數據,需要在page_load中這樣做。 – Jono

回答

4

我發現你在gridview中缺少onrowcommand。試試這個

asp:GridView ID="recentJobsGridView" runat="server" CellPadding="4" ForeColor="#333333" 
          GridLines="None" Height="151px" Width="541px" Visible="False" AutoGenerateColumns="False" 
          PageSize="5" AllowPaging="True" OnPageIndexChanging="recentJobsGridView_PageIndexChanging" 
          DataKeyNames="orderItemId" onrowcommand="recentJobsGridView_RowCommand"> 

然後添加到您的CodeFile

protected void recentJobsGridView_RowCommand(object sender, GridViewCommandEventArgs e) 
     { 
      if (e.CommandName.Equals("Add")) 
      { 
       StatusLbl.Text = "Hellooooooo"; 
      } 
     } 
+0

正確的答案 – UrsulRosu

1

嘗試使用GridView的RowCommand事件,該事件在點擊鏈接按鈕時觸發,命令名爲/ arg。

+0

已經做到了,但它也沒有工作:( – salRad

+1

我認爲問題是從另一個gridview選擇記錄時綁定網格視圖...在這種情況下,解決方案是什麼?? – salRad

0

刪除OnCommand="btAdd_Command",所以LinkButton將...

<asp:LinkButton ID="btAdd" runat="server" Text="Add" 
       CommandArgument='<%# Container.DataItem %>' 
       CommandName="Add"> 
</asp:LinkButton> 

並添加OnRowCommand="btAdd_Command"GridView

<asp:GridView ID="recentJobsGridView" runat="server" CellPadding="4" 
       ForeColor="#333333" GridLines="None" Height="151px" Width="541px" 
       Visible="False" AutoGenerateColumns="False" 
       PageSize="5" AllowPaging="True" 
       OnPageIndexChanging="recentJobsGridView_PageIndexChanging" 
       DataKeyNames="orderItemId" 
       OnRowCommand="btAdd_Command"> 

現在上的代碼背後,修改該按鈕的定義如下面...

protected void btAdd_Command(Object sender, GridViewCommandEventArgs e) 
{ 
    // If multiple buttons are used in a GridView control, use the 
    // CommandName property to determine which button was clicked. 
    if (e.CommandName == "Add") 
    { 
     StatusLbl.Text = "Hellooooooo"; 
    } 
} 

馬克,你必須使用GridViewCommandEventArgs,不CommandEventArgs

相關問題