GridView是否有OnRowSelect事件?

时间:2012-10-20 05:47:48

标签: c# asp.net .net gridview

我知道这可能是一个愚蠢的问题,但是GridView有一个OnRowSelect,如OnRowUpdating,ASP.NET C#中的OnRowDeleting?

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

当我把上面的代码放在事件中时,我在e.RowIndex

上出错了

任何帮助都会真正有用。

5 个答案:

答案 0 :(得分:1)

这是gridview的event list,您可能需要SelectedIndexChanged

答案 1 :(得分: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")
        }

答案 2 :(得分:0)

您必须启用autogenerateselectbutton = trueSee more here

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

 }

答案 3 :(得分:0)

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

答案 4 :(得分: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"