4

2行目の画像編集ボタンをクリックした場合、コードビハインドで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);
}
4

4 に答える 4

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 に答える
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 に答える
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 に答える