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

您必须启用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

将您的按钮放在模板字段中,并为其分配一个正常的点击事件,如下所示,

<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

否 但它有 SelectedIndexChanged 事件可以达到目的。这是 GridView 支持的事件列表。 http://msdn.microsoft.com/en-us/library/hf8xwy0t.aspx

于 2012-10-20T09:27:20.253 回答
0

您必须首先通过简单的插入、更新、删除来查看此 gridview 的事件......这是链接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 + ".";
    }
  }

Gridview 看起来像这样

<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 回答