5

aspx で gridview を使用しています。登録と details.aspx の 2 つのページがあります。登録が完了すると、details.aspx の詳細ページに移動する必要があります。その GV に gridview を保持しています。アイテムテンプレートを使用したすべての学生の最後の列として編集ボタンを使用して、学生のすべての結果を表示する必要があります。しかし、行コマンドイベントでは、ユーザーが編集をクリックしてユーザーIDを使用して編集ページに移動する必要がある場合、書き込む機能がわかりません。IDは正午に編集可能モードであり、他のフィールドは編集可能である必要があります。

詳細.aspx

   <asp:GridView ID="GridView3" runat="server" CellPadding="3" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false"  OnRowCommand="GridView3_RowCommand" OnSelectedIndexChanged="GridView3_SelectedIndexChanged">
     <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
         <Columns>
             <asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True"/>
             <asp:BoundField DataField="Password" HeaderText="Password" />
             <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
             <asp:BoundField DataField="LastName" HeaderText="LastName" />
             <asp:BoundField DataField="EMail" HeaderText="Emaid-ID" />
             <asp:BoundField DataField="PhoneNo" HeaderText="Phone Number" />
             <asp:BoundField DataField="Location" HeaderText="Location" />

              <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                   <asp:Button ID="btnedit" runat="server" Text="Edit" CommandName="Edit" CommandArgument="UserName"/>
                </ItemTemplate>          
             </asp:TemplateField> 
         </Columns>
     <EditRowStyle BackColor="#999999" />
     <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
     <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
     <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
     <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
     <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
     <SortedAscendingCellStyle BackColor="#E9E7E2" />
     <SortedAscendingHeaderStyle BackColor="#506C8C" />
     <SortedDescendingCellStyle BackColor="#FFFDF8" />
     <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
 </asp:GridView>
    </div>
</form>

details.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
 {
    string connection2 = System.Web.Configuration.WebConfigurationManager.ConnectionStrings        ["connection1"].ConnectionString;
    SqlConnection con = new SqlConnection(connection2);
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from User_Information", con);
    SqlDataReader rdr = cmd.ExecuteReader();
    GridView3.DataSource = rdr;
    GridView3.DataBind();
    con.Close();
}
protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
{

}


protected void GridView3_SelectedIndexChanged(object sender, EventArgs e)
{

}

}

4

2 に答える 2

7

最初に、ボタン コントロールCommandArgumentプロパティの各行に一意の値が必要です。

 <asp:Button ID="btnedit" runat="server" Text="Edit" CommandName="Edit" 
             CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />

次に、コード ビハインドGridView3_RowCommandイベントで、以下のコードのようなものが表示されます。

protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int index = 0;
    GridViewRow row;
    GridView grid = sender as GridView;

    switch (e.CommandName)
    {
        case "Edit":
            index = Convert.ToInt32(e.CommandArgument);
            row = grid.Rows[index];

            //use row to find the selected controls you need for edit, update, delete here
            // e.g. HiddenField value = row.FindControl("hiddenVal") as HiddenField;

            break;
    }
}
于 2014-07-16T06:47:06.617 に答える
7

2 つの方法

方法 1

マークアップでこれらを変更してください

  1. 変化するCommandName="EditUserName"
  2. を省略しCommandArgumentます。これは必要ありません

分離コード

protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "EditUserName")
    {
        //first find the button that got clicked
        var clickedButton = e.CommandSource as Button;
        //find the row of the button
        var clickedRow = clickedButton.NamingContainer as GridViewRow;
        //now as the UserName is in the BoundField, access it using the cell index.
        var clickedUserName = clickedRow.Cells[0].Text;
    }
}

方法 2

を与えますCommandArgument。これらのような多くの異なる引数を与えることができます

  1. CommandArgument="<%# Container.DataItemIndex %>"
  2. CommandArgument="<%# Container.DisplayIndex %>"
  3. CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"(アリがくれたもの)

コードで、これを行います

protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "EditUserName")
    {            
        var clickedUserName = CustomersTable
            .Rows[Convert.ToInt32(e.CommandArgument)]//find the row with the clicked index
            .Cells[0]//find the index of the UserName cell
            .Text;//grab the text
    }
}

PS:

1. CommandName を変更する理由は、 の場合、このエラーを発生させるイベントが発生するためですCommandName="Edit"RowEditing

GridView 'GridView3' は、処理されなかったイベント RowEditing を発生させました。

2.Page_Loadコードを内部に配置するif(!IsPostBack)か、上記の方法が機能しない.

于 2014-07-16T10:58:25.243 に答える