GridViews GridView1_SelectedIndexChanged不起作用

时间:2015-07-04 14:25:17

标签: c# asp.net gridview cookies

我有一个gridview来填充来自DB的数据,最后一个列元素要么超链接到不同的页面,要么只是文本显示(没有超链接)。我已将TransactionId存储在GridView1_SelectedIndexChanged()事件的cookie中,用于该gridview中的每条记录。但是这个事件没有被触发。

gridview正确绑定,基于类型的最后一列项在GridView1_RowDataBound()处变为超链接。重定向到不同页面工作正常,但由于未触发SelectedIndexChanged()事件,因此未加载cookie并且重定向页面不显示数据。

请帮帮我们。感谢。

代码:

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="Black" 
GridLines="Vertical" Width="100%" RowStyle-Wrap="true" AllowPaging="True" 
PageSize="5" OnPageIndexChanging="gridView_PageIndexChanging" 

OnRowDataBound="GridView1_RowDataBound" onselectedindexchanged="GridView1_SelectedIndexChanged"

AutoGenerateColumns="False" selectedindex="1" style="word-wrap:break-word; margin-left: 0px;" >
    <Columns>
        <asp:BoundField DataField="COL-1" HeaderText="COL1" />
        <asp:BoundField DataField="COL-2" HeaderText="COL2" />
        <asp:BoundField DataField="COL-3" HeaderText="COL3" />
        <asp:BoundField DataField="COL-4" HeaderText="COL4" />
    </Columns>

    <AlternatingRowStyle BackColor="White" />
    <FooterStyle BackColor="#CCCC99" />
    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
</asp:GridView>


protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    HttpCookie TransId = new HttpCookie("TransId");
    GridViewRow row = GridView1.SelectedRow;
    TransId.Value = row.Cells[0].Text;
    Response.Cookies.Add(TransId);
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.Cells[3].Text.Equals("Pending"))
        {
            HyperLink link = new HyperLink();
            link.Text = "Pending";
            link.NavigateUrl = "NewPage.aspx";
            e.Row.Cells[3].Controls.Add(link);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

当您单击一个单元格时,您正在调用重定向到另一个页面,因此当前页面不会发生回发。这意味着您的SelectedIndexChanged函数未被命中,因为ASP.NET正在加载新页面。

尝试将回发引导到同一页面,然后在自己调用Response.Redirect之前获取Page_Load函数中的选定行。

答案 1 :(得分:0)

删除了GridView1_SelectedIndexChanged()事件并将TransId作为参数传递。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.Cells[3].Text.Equals("Pending"))
        {
            HyperLink link = new HyperLink();
            link.Text = "Pending";
            link.NavigateUrl = "NewPage.aspx?parameter=" + e.Row.Cells[0].Text;
            e.Row.Cells[3].Controls.Add(link);
        }
    }
}