如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)

NET 2.0就改用SqlDataSource和GridView了,LinkButtom一樣得放在TemplateField,但GridView沒有ItemCommand event,取而代之的是RowCommand event。

14如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)  protected void Page_Load(object sender, EventArgs e) 如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET){

15如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)    if (!IsPostBack)

16如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)      GridView1_DataBind();

17如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)  }

18如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)

19如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)  protected void GridView1_DataBind() 如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET){

20如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)    SqlDataSource1.ConnectionString = @"Data Source=.\sqlexpress;Initial Catalog=pubs;Integrated Security=True";

21如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)    SqlDataSource1.SelectCommand = "SELECT TOP 10 " +

22如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)                                          "fname," +

23如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)                                          "lname " +

24如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)                                   "FROM employee";

25如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)    GridView1.DataSourceID = SqlDataSource1.ID;

26如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)    GridView1.DataKeyNames = new string[] 如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET){ "lname" };

27如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)    GridView1.AutoGenerateColumns = false;

28如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)  }

29如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)

30如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET){

31如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)    if (e.CommandName == "Select") 如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET){

32如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)      int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;

33如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)      Label1.Text = GridView1.DataKeys[rowIndex].Value.ToString();

34如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)    }

35如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)  }

36如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)</script>

37如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)

38如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)<html xmlns="http://www.w3.org/1999/xhtml">

39如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)<head runat="server">

40如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)  <title>Untitled Page</title>

41如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)</head>

42如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)<body>

43如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)  <form id="form1" runat="server">

44如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)    <div>

45如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)      <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">

46如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)        <Columns>

47如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)          <asp:TemplateField HeaderText="First Name">

48如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)            <ItemTemplate>

49如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)              <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select" Text='<%#Eval("fname")%>'></asp:LinkButton>

50如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)            </ItemTemplate>

51如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)          </asp:TemplateField>

52如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)        </Columns>

53如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)      </asp:GridView>

54如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)    </div>

55如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)    <asp:Label ID="Label1" runat="server"></asp:Label>

56如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)    <asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>

57如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)  </form>

58如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)</body>

59如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)</html>

最難理解的應該是32行

如何獲得GridView內LinkButton的RowIndex? (初級) (.NET) (ASP.NET)int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;

e.CommandSource傳的是按下去的LinkButton,不過由於傳回的是Object,就得自行轉成LinkButton,但由於我們想知道的是RowIndex,而LinkButton是包含在GridViewRow內,所以透過NamingContainer傳回目前的GridViewRow,但傳回的是Control,所以需在轉成GridViewRow後才能有RowIndex property。

你可能感兴趣的:(GridView)