2014-10-29 86 views
0

我創建了一個簡單的網格視圖與多列,並添加一個按鈕,所以當點擊傳遞行的值不同的網絡表單,但點擊不按鈕點擊。我使用的示例代碼從鏈接 :http://msdn.microsoft.com/en-us/library/bb907626(v=vs.100).aspxGridView按鈕onclick沒有從微軟開發者網絡射擊範例

這是我的代碼:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AllowSorting="True" DataSourceID="SqlDataSource1" 
    PageSize="100" AutoGenerateColumns="False"> 
    <Columns> 
     <%--<asp:HyperLinkField HeaderText="Edit" NavigateUrl="FormReport2.aspx" 
      Text="Edit" />--%> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:Button ID="AddButton" runat="server" 
       CommandName="AddToCart" 
       CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>" 
       Text="Add to Cart" /> 
      </ItemTemplate> 
     </asp:TemplateField> 

     <asp:BoundField DataField="fldEmployeeID" HeaderText="EmployeeID" 
      SortExpression="fldEmployeeID" /> 
     <asp:BoundField DataField="fldAbsentDate" HeaderText="AbsentDate" 
      SortExpression="fldAbsentDate" /> 
     <asp:BoundField DataField="fldAbsentCode" HeaderText="AbsentCode" 
      SortExpression="fldAbsentCode" /> 
     <asp:BoundField DataField="fldRuleViolationWarningType" 
      HeaderText="Rule Violation Warning Type" 
      SortExpression="fldRuleViolationWarningType" /> 
     <asp:BoundField DataField="fldRuleViolationIssueDate" 
      HeaderText="Rule Violation Issue Date" 
      SortExpression="fldRuleViolationIssueDate" /> 
     <asp:BoundField DataField="fldLOAEndDate" HeaderText="LOA End Date" 
      SortExpression="fldLOAEndDate" />          

    </Columns> 
</asp:GridView> 

代碼背後

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) 
    If (e.CommandName = "AddToCart") Then 
     ' Retrieve the row index stored in the CommandArgument property. 
     Dim index As Integer = Convert.ToInt32(e.CommandArgument) 

     ' Retrieve the row that contains the button 
     ' from the Rows collection. 
     Dim row As GridViewRow = GridView1.Rows(index) 

     ' Add code here to add the item to the shopping cart. 

    End If 
End Sub 

任何幫助將是非常讚賞。還有什麼是最好的方式來將行中的值從後面的代碼傳遞到不同的Web表單?

回答

1

你忘了唱歌的事件。看到最後一行在這裏:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AllowSorting="True" DataSourceID="SqlDataSource1" 
    PageSize="100" AutoGenerateColumns="False" 
    OnRowCommand="GridView1_RowCommand"> 
+0

它仍然沒有解僱它。任何其他想法? – CodeEngine 2014-10-29 16:10:48

+0

@ omachu23,不,沒有更多的想法,應該是這樣。你怎麼知道它沒有被解僱? – Andrei 2014-10-29 16:18:38

+0

我創建了一個文本框,並且在後面的代碼的if語句之前寫道:Textbox1.Text =「它的工作」但沒有。我也設置了一個突破點,但它永遠不會引發火災。你認爲是一種不同的方式,或達到相同的結果。感謝您的時間Andrei – CodeEngine 2014-10-29 16:28:42