0

これはばかげた質問かもしれませんが、GridView には ASP.NET C# の OnRowUpdating や OnRowDeleting のような OnRowSelect がありますか?

protected void GridView_OnRowSelect(object sender, GridViewSelectEventArgs e)
{
    loc_id_hf.Value = (((HiddenField)(GridView2.Rows[e.RowIndex].FindControl("loc_id_hf"))
}

上記のコードをイベント内に配置すると、e.RowIndex でエラーが発生します

どんな助けでも本当に感謝します。

4

5 に答える 5

1

これはgridview のイベント一覧SelectedIndexChangedです。必要になる場合があります。

于 2012-10-20T05:53:20.870 に答える
0

簡単な挿入、更新、削除を使用して、このグリッドビューのイベントを最初に確認する必要があります...リンクは次のとおりですhttp://www.dotnetspider.com/resources/40129-grid-view-save-edit-update.aspx

次に、このリンクhttp://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.buttonfield.commandname.aspxを参照してください。

void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    if(e.CommandName=="Select")
    {
      int index = Convert.ToInt32(e.CommandArgument);    
      GridViewRow selectedRow = CustomersGridView.Rows[index];
      TableCell contactName = selectedRow.Cells[1];
      string contact = contactName.Text;  
      Message.Text = "You selected " + contact + ".";
    }
  }

グリッドビューは次のようになります

<asp:gridview id="CustomersGridView" datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="false" onrowcommand="CustomersGridView_RowCommand"
        runat="server">

        <columns>

          <asp:buttonfield buttontype="Button" 
            commandname="Select" headertext="Select Customer" text="Select"/>

          <asp:boundfield datafield="CompanyName" headertext="Company Name"/>

          <asp:boundfield datafield="ContactName" headertext="Contact Name"/>

        </columns>

</asp:gridview>

申請することもできますCommandName="Edit", CommandName="Delete" instead of CommandName="Select"

于 2012-10-20T15:38:48.593 に答える
0

テンプレート フィールド内にボタンを配置し、次のような通常のクリック イベントを割り当てます。

<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False" 
                EnableModelValidation="True">
                <Columns>
                    <asp:BoundField HeaderText="field 1" />
                    <asp:BoundField HeaderText="field2" />
                    <asp:TemplateField HeaderText="Select">
                        <ItemTemplate>
                            <asp:Button ID="btnSelect" runat="server" onclick="btnSelect_Click" 
                                Text="Select" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

クリックイベントは次のようになります

protected void btnSelect_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            GridViewRow r = (GridViewRow)b.NamingContainer;
            ////// once you have the row in which the event occured, you can do every thing with it
            // like
            int id = Convert.ToInt32(gvProducts.DataKeys[r.RowIndex].Value);
            // or you can find control like
            HiddenField hf = (HiddenField)r.FindControl("myHiddenField")
        }
于 2012-10-20T06:51:10.150 に答える
0

を有効にする必要がありautogenerateselectbutton = trueます。詳細はこちら

<asp:GridVew id="Gridview1" OnSelectedIndexChanging="GridView1_SelectedIndexChanging"
   OnSelectedIndexChanged="GridView1_SelectedIndexChanged" autogenerateselectbutton="True">

</asp:GridVew >

イベント ハンドラー

 protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
 {

       GridViewRow row = GridView1.Rows[e.NewSelectedIndex];

 }

 protected void GridView1_SelectedIndexChanged(object sender, GridViewSelectEventArgs e)
 {

       GridViewRow row = GridView1.SelectedRow;

 }
于 2012-10-20T07:46:22.320 に答える
0

いいえしかし、目的を果たすことができる SelectedIndexChanged イベントがあります。GridView でサポートされているイベントの一覧を次に示します。 http://msdn.microsoft.com/en-us/library/hf8xwy0t.aspx

于 2012-10-20T09:27:20.253 に答える