2015-11-24 84 views
0

獲取選定行的隱形標識我有一個gridview,我試圖讓選定行的ID(UrunId)。這裏是我的aspx:在GridView控件

<asp:GridView ID="GridViewHdcvi" runat="server" DataSourceID="EntityDataSourceHdcvi" AutoGenerateColumns="False" DataKeyNames="UrunId"> 
             <Columns> 
              <asp:TemplateField HeaderText="Sil"> 
               <ItemTemplate> 
                <asp:LinkButton ID="deleteProduct" runat="server" Text="Satırı Sil" CommandName="deleteProduct"> 
                </asp:LinkButton> 
               </ItemTemplate> 
              </asp:TemplateField> 

              <asp:TemplateField HeaderText="Sil" Visible="false"> 
               <ItemTemplate> 
                <%#Eval("UrunId") %> 
               </ItemTemplate> 
              </asp:TemplateField> 

              <asp:TemplateField HeaderText="ÜRÜN VE DETAYLARI"> 
               <ItemStyle Width="400px" /> 
               <ItemTemplate> 
                <div style="color: red" class="text-center"><%#Eval("UrunAdi") %></div> 
                <%#Eval("UrunDetay") %> 
               </ItemTemplate> 
              </asp:TemplateField> 
              <asp:ImageField HeaderText="ÜRÜN GÖRSELİ" DataImageUrlField="UrunResim"></asp:ImageField> 
              <asp:TemplateField HeaderText="BİRİM FİYAT"> 
               <ItemTemplate> 
                <%#Eval("UrunFiyati") %> 
               </ItemTemplate> 
              </asp:TemplateField> 
              <asp:TemplateField HeaderText="ADET"> 
               <ItemTemplate> 
                <asp:TextBox ID="txtAdet" runat="server" Width="40px"></asp:TextBox> 
               </ItemTemplate> 
              </asp:TemplateField> 
             </Columns> 
            </asp:GridView> 

正如你可以看到我不希望用戶請參閱我的「UrunId」列,但當用戶單擊「deleteProduct」時,我需要在代碼隱藏中獲取該行的UrunId。感謝您的時間。

+0

什麼是我現在ssue?你無法刪除該行嗎?你能顯示你的刪除方法嗎? –

+0

只需在您的鏈接按鈕中添加'CommandArgument'。看看我的答案。 – Prabhat

回答

0

.ASPX標記

<asp:TemplateField HeaderText="Sil"> 
     <ItemTemplate> 
      <asp:LinkButton ID="deleteProduct" runat="server" Text="Satırı Sil" CommandArgument='<%#Eval("UrunId")%>' CommandName="deleteProduct" OnClick="LinkButton_Click"> 
       </asp:LinkButton> 
      </ItemTemplate> 
</asp:TemplateField> 

代碼背後

protected void LinkButton_Click(Object sender, EventArgs e) 
    { 
     LinkButton btn=(LinkButton)sender; 
     int Id= Convert.toInt32(btn.CommandArgument); 
     //Your delete code here 
    } 

OR

protected void gridRowCommand(objectsender, GridViewCommandEventArgs e) 
{ 
    int Id= Convert.toInt32(e.CommandArgument); 
    //Your delete code here 
}