[ASP.NET]GridView触发SelectedIndexChanged事件

 

首先在GridView中必须加上“选择”的按钮,然后在Page_Load中添加

view plain copy to clipboard print ?
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. gvTest.SelectedIndexChanged+=new EventHandler(GridView1_SelectedIndexChanged);
  4. }
protected void Page_Load(object sender, EventArgs e) { gvTest.SelectedIndexChanged+=new EventHandler(GridView1_SelectedIndexChanged); }

最后在点击绑定的GridView项的时候就可以触发SelectedIndexChanged事件了。

这里我把“选择”按钮设置成了TemplateField,然后让它的Text绑定Email字段,数据库的其余字段也可以绑定到GridView上,但是在这个程序里我把他们的可见属性都设置成了False。

页面代码:

view plain copy to clipboard print ?
  1. "form1" runat="server">
  2. "overflow-y: auto; width: 100%; height: 100%">
  3. "gvTest" runat="server" AutoGenerateColumns="False"
  4. DataKeyNames="UserId" DataSourceID="SqlDataSource1" ShowHeader ="false">
  5. "UserId" HeaderText="UserId" InsertVisible="False"
  6. ReadOnly="True" SortExpression="UserId" Visible="False" />
  7. "EMail" HeaderText="EMail" SortExpression="EMail"
  8. Visible="False" />
  9. "Passwd" HeaderText="Passwd" SortExpression="Passwd"
  10. Visible="False" />
  11. "LastLogin" HeaderText="LastLogin"
  12. SortExpression="LastLogin" Visible="False" />
  13. "False">
  14. "LinkButton1" runat="server" CausesValidation="False"
  15. CommandName="Select" Text='<%# Bind("EMail", "{0}") %>'>


  16. "lblRegionCaption" runat="server" Text="Label">

  17. "SqlDataSource1" runat="server"
  18. ConnectionString="<%$ ConnectionStrings:vMessageCenterConnectionString %>"
  19. SelectCommand="SELECT [UserId], [EMail], [Passwd], [LastLogin] FROM [Users]">



  • 在页面中加入了垂直滚动条。

    SelectIndexChenged事件的代码:

    view plain copy to clipboard print ?
    1. protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    2. {
    3. int index = GridView1.SelectedIndex;
    4. string ID = GridView1.SelectedDataKey.Values["UserId"].ToString();
    5. lblRegionCaption.Text = ID;
    6. GridViewRow row = GridView1.SelectedRow;
    7. }

    你可能感兴趣的:(asp.net,asp,email,server,object,数据库)