GridView的SelectedIndexChanged不起作用 [英] GridView's SelectedIndexChanged not working

查看:76
本文介绍了GridView的SelectedIndexChanged不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



请帮助我的人,并分享一些示例代码,以便更好地理解。谢谢。



I have a gridview to populate data from DB with the last column elements either hyperlinked to a different page or just a text display (without hyperlink). I have stored the TransactionId in a cookie at GridView1_SelectedIndexChanged() event for each record in that gridview. But this event is not being triggered.

The gridview is binded properly and the last column items based on type turns into Hyperlink at GridView1_RowDataBound(). The redirection to different page works fine, but since the SelectedIndexChanged() event is not triggered, the cookie is not loaded and the redirected page does not displays data.

Please help me out guys and do share some sample code for better understanding. Thanks.

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





代码:



Code:

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

推荐答案

所有东西看起来都很好但是你错过了AutoGenerateSelectButton = gridview控件中的True,因此在gridview控件中进行更改为:



All thing's look good to run but you missed AutoGenerateSelectButton="True" in gridview control, so make a change in your gridview control as:

<asp:GridView Runat="server" ID="GridView1" AutoGenerateSelectButton="true" 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;" />


上面的建议没有从数据库中获取Row.Cells [3]。谢谢对于你的建议。



以下代码有效。根据我的理解,在使用Link属性时,它会重定向到新页面,因此当前页面不会产生因此,我不能使用GridView1_SelectedIndexChanged事件而是将其作为参数传递。感谢您的建议。



The above suggestion doesn't fetches Row.Cells[3] from database. Thank you for your suggestions.

The below code worked. As per my understanding, while using Link property, it redirects to the newpage and hence the current page doesn't incurs a postback. So, I cant use GridView1_SelectedIndexChanged event rather passing it as parameter. Thank you for your suggestions.

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


这篇关于GridView的SelectedIndexChanged不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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