选定的索引更改事件未触发(gridview)

时间:2013-06-13 04:22:01

标签: javascript asp.net gridview master-pages selectedindexchanged

我在aspx页面中有一个gridview。我已经通过javascript进行了行选择。当我使用母版页时,selectedindexchanged事件无效。否则它工作正常。对此有什么解决方案吗?

请参阅我背后的代码

 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>

2 个答案:

答案 0 :(得分:0)

可以改变

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

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

答案 1 :(得分:0)