2013-05-18 57 views
4

如何在代碼隱藏中從gridView中獲取AppId,如果我單擊第二行的編輯圖像按鈕。如何在代碼隱藏中獲取gridview列值

Application List

.aspx的代碼:

<asp:BoundField HeaderText="AppId" DataField="AppID" /> 


<asp:TemplateField HeaderText="Actions" ControlStyle-Width="20px" ItemStyle-Width="130px"> 
        <ItemTemplate> 
         <asp:ImageButton ID="imgMailCamp" runat="server" ImageUrl="~/Images/AppSetup/Mail.png" 
          Height="18px" ToolTip="Send Mail Campaign" CssClass="grdImageAlign" CommandName="SendMail" OnClick="btnMailCamp_Click" /> 
         <asp:ImageButton ID="imgViewApp" runat="server" ImageUrl="~/Images/AppSetup/application-view-list-icon.png" 
          Height="18px" ToolTip="View Appplication" CssClass="grdImageAlign" CommandName="View" OnClick="btnView_Click" /> 
         <asp:ImageButton ID="imgEditApp" runat="server" ImageUrl="~/Images/AppSetup/Action-edit-icon.png" 
          Height="18px" ToolTip="Edit Application" CssClass="grdImageAlign" CommandName="Edit" OnClick="btnEdit_Click"/> 
         <asp:ImageButton ID="imgDeleteApp" runat="server" ImageUrl="~/Images/AppSetup/Trash-can-icon.png" 
          Height="18px" ToolTip="Delete Application" CssClass="grdImageAlign" CommandName="Delete" OnClick="btnDelete_Click" /> 
        </ItemTemplate> 
       </asp:TemplateField> 

C#代碼:

protected void btnEdit_Click(object sender, EventArgs e) 
{ 
    // I need to get the current row appId, I use this appId in next page for sql query 
    Response.Redirect("/Secured/EditApplication.aspx?AppID="+AppID); 
} 
+0

你可以使用數據鍵或隱藏字段的方法,只要看看這裏http://weblogs.asp.net/aghausman/archive/2009/01/08/get-primary-key-on-row-command -gridview.aspx – user2031802

回答

2

嘗試像這樣....不要定義點擊按鈕的事件.. ..定義按鈕像這樣...

 <asp:ImageButton ID="imgEditApp" runat="server" 
ImageUrl="~/Images/AppSetup/Action-edit-icon.png" 
    Height="18px" ToolTip="Edit Application" CssClass="grdImageAlign" 
CommandName="Edit"/> 

而且 定義你的GridView RowEditing事件像這樣....

protected void gv_RowEditing(object sender, GridViewEditEventArgs e) 
     { 
      Response.Redirect("/Secured/EditApplication.aspx?AppID="+YourGridViewId.Rows[e.NewEditIndex].Cells[1].Text); 
     } 

編輯: 我想你在definig RowEditingEvent有問題..... OK,你可以做到這一點.. .nothing改變只寫這段代碼在你點擊事件...

protected void btnEdit_Click(object sender, EventArgs e) 
{ 
     ImageButton ib = sender as ImageButton; 
     GridViewRow row = ib.NamingContainer as GridViewRow; 
    Response.Redirect("/Secured/EditApplication.aspx?AppID="+YourGridViewId.Rows[row.RowIndex].Cells[1].Text); 
} 

編輯2

<asp:ImageButton ID="imgEditApp" runat="server" 
ImageUrl="~/Images/AppSetup/Action-edit-icon.png" 
    Height="18px" ToolTip="Edit Application" CssClass="grdImageAlign" 
CommandName="Edit" CommandArgument='<%#Eval("AppID") %>'/> 

    protected void btnEdit_Click(object sender, EventArgs e) 
    { 
string appid= (sender as ImageButton).CommandArgument; 
     Response.Redirect("/Secured/EditApplication.aspx?AppID="+appid 
    } 
+0

Iam得到這個錯誤如果我使用上面的代碼,Error'System.EventArgs'不包含'NewEditIndex'的定義,並且沒有接受'System.EventArgs'類型的第一個參數的擴展方法'NewEditIndex'可以找到(是否缺少using指令或程序集引用?) – Vignesh

+0

@Vignesh現在嘗試更新我的答案 –

+0

如果我改變了對EventArgs的GridViewEditEventArgs蔭越來越像「CS0123錯誤:‘btnEdit_Click’匹配委託「系統,沒有超載。 Web.UI.ImageClickEventHandler'「 – Vignesh

1

應該與commandargument
ASPX

<asp:ImageButton ID="imgEditApp" CommandArgument='<%# Eval("AppID") %>' runat="server" ... OnClick="btnEdit_Click"/> 

代碼

protected void gv_RowEditing(object sender, GridViewEditEventArgs e) 
     { 
      int categoryId = Convert.ToInt32(e.CommandArgument); 
      Response.Redirect("/Secured/EditApplication.aspx?AppID="+categoryId); 
     } 


或U可以使用的ImageButton的PostBackUrl財產它會是這樣的:

<asp:ImageButton ID="imgEditApp" PostBackUrl='<%# string.Format("/Secured/EditApplication.aspx?AppID={0}", Eval("AppID")) %>' runat="server" /> 
0

檢查此代碼段。

這是aspx文件中的代碼,包含兩列DataBound「AppId」和包含圖像按鈕的TemplateColumn「Action」。觀察Image Button的CommandName和CommandArgument屬性。還爲gridview定義OnRowCommand事件偵聽器。

<asp:GridView ID="grdDisplayData" runat="server" AutoGenerateColumns="false" 
      EnableViewState="false" onrowcommand="grdDisplayData_RowCommand"> 
      <Columns> 
       <asp:BoundField HeaderText="AppId" DataField="AppId" /> 
       <asp:TemplateField HeaderText="Action" > 
        <ItemTemplate> 
         <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="MyEdit" 
         CommandArgument="<%# ((GridViewRow) Container).RowIndex%>"/> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="ImageAction"> 
        <ItemTemplate> 
         <asp:ImageButton ID="ImageButton1" runat="server" Width="15px" Height="15px" 
          CommandName="ImgEdit" CommandArgument="<%# ((GridViewRow) Container).RowIndex%>"/> 
        </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
     </asp:GridView> 

這裏是代碼背後的代碼。 e.CommandArument返回單擊圖像按鈕的行的索引。

protected void grdDisplayData_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e) 
     { 
      if (e.CommandName == "ImgEdit") 
      { 
       int RowIndex = Convert.ToInt32(e.CommandArgument); 
       Response.Redirect("/Secured/EditApplication.aspx?AppID=" + grdDisplayData.Rows[RowIndex].Cells[1].Text.Trim()); 
      } 
     } 

讓我知道這是否解決了您的問題。

乾杯! Piyush Deshpande

+0

在[rowindex]中得到錯誤 – Vignesh

+0

你得到的錯誤是什麼?如果網格中的行比Int16支持的要多,將「Convert.ToInt16」更改爲「Convert.ToInt32」或「Convert.ToInt64」,並檢查錯誤是否消失,如果沒有發佈錯誤。乾杯! –