0

我有一個aspx頁面的gridview。我已經通過javascript進行了行選擇。當我使用母版頁時,selectedindexchanged事件不起作用。否則,它工作正常。有沒有解決方案?選定的索引更改事件不發射(gridview)

請看看我身後

public partial class WebForm2 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      GridView1.DataSource = Enumerable.Range(1, 10).Select(a => new 
      { 
       ID = a, 
       FirstName = String.Format("First Name {0}", a), 
       LastName = String.Format("Last Name {0}", a) 
      }); 
      GridView1.DataBind(); 
     } 
    } 
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     lblSelectedRow.Text = String.Format("You selected row {0} with {1} {2}", 
               GridView1.SelectedIndex + 1, 
               GridView1.SelectedRow.Cells[0].Text, 
               GridView1.SelectedRow.Cells[1].Text); 
    } 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      //add css to GridViewrow based on rowState 
      e.Row.CssClass = e.Row.RowState.ToString(); 
      //Add onclick attribute to select row. 
      e.Row.Attributes.Add("onclick", 
      String.Format("javascript:__doPostBack('GridView1','Select${0}')", e.Row.RowIndex)); 
     } 
    } 

這裏代碼標記

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"  CodeBehind="WebForm2.aspx.cs"  Inherits="SelectGridRow.WebForm2" EnableEventValidation="false" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> 
<style type="text/css"> 
    body, html 
    { 
     font-family: Tahoma; 
     font-size: small; 
    } 
    .Normal 
    { 
     background-color: #EFF3FB; 
     cursor: hand; 
    } 
    .Normal:Hover, .Alternate:Hover 
    { 
     background-color: #D1DDF1; 
     cursor: hand; 
    } 
    .Alternate 
    { 
     background-color: White; 
     cursor: hand; 
    } 
</style> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <ContentTemplate> 
     <asp:ScriptManager ID="ScriptManager1" runat="server"> 
     </asp:ScriptManager> 
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
      CellPadding="4" DataKeyNames="ID" Font-Names="Tahoma" Font-Size="Small" 
      ForeColor="#333333" GridLines="None"  OnRowDataBound="GridView1_RowDataBound" 
      OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> 
      <Columns> 
       <asp:TemplateField HeaderText="Row"> 
        <ItemTemplate> 
       <%# Container.DataItemIndex+1 %> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:BoundField DataField="FirstName" HeaderText="First Name" /> 
       <asp:BoundField DataField="LastName" HeaderText="Last Name" /> 
       <asp:CommandField ButtonType="Link" SelectText="Enroll"  ShowSelectButton="true" 
        Visible="false" /> 
      </Columns> 
      <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
      <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333"  /> 
     </asp:GridView> 
     <asp:Label ID="lblSelectedRow" runat="server" Text="" /> 
    </ContentTemplate> 
</asp:UpdatePanel> 
<br /> 
</asp:Content> 

回答

0

ü可以改變

<asp:CommandField ButtonType="Link" SelectText="Enroll"  ShowSelectButton="true" 
       Visible="false" /> 

<asp:CommandField ButtonType="Link" SelectText="Enroll"  ShowSelectButton="true" 
       Visible="true" /> 
相關問題