2015-11-28 73 views
0

我已經聲明瞭一個gridview,其中一些字段是從數據庫中綁定的。我已經添加了一個項目模板,我有一個文本框和按鈕。現在,當我點擊按鈕時,我想要捕獲相應行的列和該文本框的值。我怎樣才能完成它?如何從gridview中相應的按鈕點擊獲取特定的行值

我的aspx gridview的標記:

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="FOODITEM_ID" DataSourceID="SqlDataSource2" EnableModelValidation="True" OnSelectedIndexChanged="GridView2_SelectedIndexChanged" 
    OnRowCommand="GridView2_OnRowCommand"> 
    <Columns> 
     <asp:BoundField DataField="FOOD_ITEM_NAME" HeaderText="FOOD_ITEM_NAME" SortExpression="FOOD_ITEM_NAME" /> 
     <asp:BoundField DataField="FOODITEM_ID" HeaderText="FOODITEM_ID" ReadOnly="True" SortExpression="FOODITEM_ID" /> 
     <asp:BoundField DataField="PRICE" HeaderText="PRICE" SortExpression="PRICE" /> 
     <asp:BoundField DataField="DAY_AVAILABLE" HeaderText="DAY_AVAILABLE" SortExpression="DAY_AVAILABLE" /> 
     <asp:BoundField DataField="TIME_AVAILABLE" HeaderText="TIME_AVAILABLE" SortExpression="TIME_AVAILABLE" /> 
     <asp:BoundField DataField="DISCOUNT_PERCENTAGE" HeaderText="DISCOUNT_PERCENTAGE" SortExpression="DISCOUNT_PERCENTAGE" /> 
     <asp:BoundField DataField="START_DATE" HeaderText="START_DATE" SortExpression="START_DATE" /> 
     <asp:BoundField DataField="DEADLINE" HeaderText="DEADLINE" SortExpression="DEADLINE" /> 
     <asp:BoundField DataField="Rating" HeaderText="Rating" SortExpression="Rating" /> 
     <asp:TemplateField ShowHeader="False"> 
      <ItemTemplate> 
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
       <asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="Add" 
        Text="Add" CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

現在,當我按下按鈕,我怎麼能得到文本框等領域對應行的值。

回答

0

有事情你缺少轎跑車,所以我決定給你我完整的示例。

第一個aspx代碼。注意我使用命令名(編輯,刪除,取消,更新)來獲取適當的命令。其次,我綁定了「ItemsGridView_RowUpdating」來捕獲新的值(這是你在找什麼)。

   <asp:GridView ID="ItemsGridView" runat="server" DataKeyNames="ItemId,FieldName" PageSize="1000" 
        AutoGenerateColumns="False" AllowPaging="False" AllowSorting="False" SkinID="DefaultGridView" 
        OnRowEditing="ItemsGridView_RowEditing" 
        OnRowCancelingEdit="ItemsGridView_RowCancelingEdit" 
        OnRowDeleting="ItemsGridView_RowDeleting" 
        OnRowUpdating="ItemsGridView_RowUpdating" 
        > 
        <Columns> 
         <asp:TemplateField ItemStyle-CssClass="TemplateFieldFourColumns"> 
          <ItemTemplate> 
           <asp:ImageButton ID="ibEdit" runat="server" ToolTip="<% $resources:AppResource,Edit %>" SkinID="EditPage" CommandName="Edit" /> 
           <asp:ImageButton ID="ibDelete" runat="server" ToolTip="<% $resources:AppResource,Delete %>" SkinID="DeletePage" CommandName="Delete" OnClientClick='<%#this.GetDeleteConfirmation() %>' /> 
           <asp:ImageButton ID="ibUp" runat="server" ToolTip="<% $resources:AppResource,Up %>" SkinID="UpPage" OnClick="ibUp_Click" CommandArgument='<%#Eval("ItemId")%>' /> 
           <asp:ImageButton ID="ibDown" runat="server" ToolTip="<% $resources:AppResource,Down %>" SkinID="DownPage" OnClick="ibDown_Click" CommandArgument='<%#Eval("ItemId")%>' /> 
          </ItemTemplate> 
          <EditItemTemplate> 
           <asp:ImageButton ID="ibCancel" runat="server" ToolTip="<% $resources:AppResource,Cancel %>" SkinID="Cancel" CommandName="Cancel" /> 
           <asp:ImageButton ID="ibUpdate" runat="server" ToolTip="<% $resources:AppResource,Save %>" SkinID="Save" CommandName="Update" /> 
          </EditItemTemplate> 
         </asp:TemplateField> 

         <asp:TemplateField HeaderText="<% $resources:AppResource,Format %>"> 
          <ItemTemplate> 
           <asp:Label ID="lblFormat" runat="server" Text='<%#Eval("Format")%>'></asp:Label> 
          </ItemTemplate> 
          <EditItemTemplate> 
           <asp:TextBox ID="txtFormat" runat="server" Text='<%#Bind("Format")%>' Width="50"></asp:TextBox> 
          </EditItemTemplate> 
         </asp:TemplateField> 

        </Columns> 
       </asp:GridView> 

十一月後面的代碼:

protected void ItemsGridView_RowEditing(object sender, GridViewEditEventArgs e) 
    { 
     ItemsGridView.EditIndex = e.NewEditIndex; 
     ItemsGridView.DataSource = this.genericForm.FormItems; //TODO: get your data source 
     ItemsGridView.DataBind(); 
    } 

    protected void ItemsGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 
    { 
     ItemsGridView.EditIndex = -1; 
     ItemsGridView.DataSource = this.genericForm.FormItems; //TODO: get your data source 
     ItemsGridView.DataBind(); 
    } 

    protected void ItemsGridView_RowDeleting(object sender, GridViewDeleteEventArgs e) 
    { 
     //delete... 
     try 
     { 
      Guid itemId = (Guid)e.Keys[0]; //key 
      //TODO: delete your item and bind new data 
     } 
     catch (Exception ex) 
     { 
      this.MessageBoxError(ex); 
     } 
    } 


    protected void ItemsGridView_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 

     //update... 
     try 
     { 
     //get your key and read new values for update.... 
      Guid itemId = (Guid)e.Keys[0]; 
      string fieldName = (string)e.Keys[1]; 
      string Format = (string)e.NewValues["Format"]; 

      //TODO: make an update and rebind your data 
     } 
     catch (Exception ex) 
     { 
      this.MessageBoxError(ex); 
     } 
    } 

編碼愉快!

0

Flowwing代碼應工作:

void GridView2_OnRowCommand(Object sender, GridViewCommandEventArgs e) 
{ 
    if(e.CommandName=="Add") 
    { 
     int index = Convert.ToInt32(e.CommandArgument); 
     GridViewRow selectedRow = CustomersGridView.Rows[index]; 
     string otherFieldText = row.Cells[0].Text; // put othe fields' index 
     TextBox txt = (TextBox)row.FindControl("TextBox1"); // Textbox 
     string txtVal = txt.text; // Textbox value 
    } 
} 
相關問題