簡體   English   中英

如何在代碼隱藏中獲取gridview列值

[英]How to get gridview column value in codebehind

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

申請清單

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);
}

嘗試這樣....不要定義按鈕的Click事件....定義按鈕這樣...

     <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);
        }

編輯:我認為你在定義RowEditingEvent時遇到問題.....好的你可以做到這一點......沒有什么可以改變只需在你的代碼中編寫這個代碼點擊事件......

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
    }

您可以從中獲取網格視圖單元格值。

GridView.Rows[RowIndex].Cells[CellIndex].Text

這里“RowIndex”是您想要獲取數據的行號,“CellIndex”是您想要從中獲取數據的單元號。

我認為gridview的事件“OnRowCommand”最適合您的問題。 使用blow link了解更多詳情

http://www.codeproject.com/Tips/564619/Example-of-gridview-rowcommand-on-Button-Click

它應該與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);
        }


或者你可以使用imagebutton的PostBackUrl屬性,它將是這樣的:

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

檢查此代碼段。

這是aspx文件中的代碼,其中包含兩列DataBound“AppId”和包含Image Button的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM