4

如果我单击第二行中的编辑图像按钮,如何从代码隐藏中的 gridView 获取 AppId。

申请清单

ASP 代码:

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

4 回答 4

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

编辑: 我认为您在定义 RowEditingEvent 时有问题.....好吧,您可以这样做...无需更改,只需在您的 Click 事件中编写此代码...

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
    }
于 2013-05-18T06:19:38.700 回答
2

您可以从中获取网格视图单元格值。

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

这里“RowIndex”是您要从中获取数据的行号,“CellIndex”是您要从中获取数据的单元格编号。

我认为 gridview 的事件“OnRowCommand”最适合您的问题。使用打击链接了解更多详情

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

于 2013-05-18T06:29:40.257 回答
1

它应该与 命令参数 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" />
于 2013-05-18T06:32:53.557 回答
0

检查此代码段。

这是 aspx 文件中的代码,其中包含两列 DataBound "AppId" 和 TemplateColumn "Action",其中包含图像按钮。观察图像按钮的 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());
            }
        }

让我知道这是否解决了您的问题。

干杯!!!皮尤什·德什潘德

于 2013-05-18T16:00:45.803 回答