gridview选择索引更改事件未触发 [英] gridview selected index changed event not firing

查看:63
本文介绍了gridview选择索引更改事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



请看我背后的代码



I have a gridview in an aspx page. I have made it row selection by javascript. The selectedindexchanged event is not working when i am using master page. Otherwise it works fine. Is there any solution for this?

Please see my code behind

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





这里是加价





here is the markup

<%@ 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}'),e.Row.RowIndex));
}
}
{0}')", e.Row.RowIndex)); } }





这里是加价





here is the markup

<%@ 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>


try this...



protected void grd_Distributor_RowCreated(object sender, GridViewRowEventArgs e)

{


if (e.Row.RowType == DataControlRowType.DataRow)

{

e.Row.Attributes.Add(\"onmouseover\",



\"this.originalstyle=this.style.backgroundColor;this.style.backgroundColor=’#FF9955’;\");



e.Row.Attributes.Add(\"style\", \"cursor:pointer;\");



e.Row.Attributes.Add(\"onmouseout\",



\"this.style.backgroundColor=this.originalstyle;\");





e.Row.ToolTip = \"Click to select row\";

e.Row.Attributes[\"onclick\"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.grd_Distributor, \"Select
try this...

protected void grd_Distributor_RowCreated(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover",

"this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#FF9955';");

e.Row.Attributes.Add("style", "cursor:pointer;");

e.Row.Attributes.Add("onmouseout",

"this.style.backgroundColor=this.originalstyle;");


e.Row.ToolTip = "Click to select row";
e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.grd_Distributor, "Select


\" + e.Row.RowIndex);

}











}
" + e.Row.RowIndex);
}





}


这篇关于gridview选择索引更改事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆