Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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.

XML
<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:
C#
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);
        }
    }
}
Posted
Updated 6-Jul-15 18:30pm
v2

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

C#
<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;" />
 
Share this answer
 
v2
Comments
Jones Aaron 7-Jul-15 8:27am    
With AutoGenerateSelectButton="true", I can have access to SelectedIndexChanged() event. The page redirects to the target page and works fine. Additionally, I get another column "Select" at the left most of my grid which is not desired output as per requirement.

The purpose is to perform redirection from an existing column whose values are fetched from database. That's why in my RowDataBound, I was converting the values fetched from database and converted it into hyperlink.

Even if I try to convert the existing BoundField to CommandField, I think it should work. But I get an error "Invalid Argument" when I try to add CommandField control to that column. The code is as below.

Please help me out or share any better working solution for the above requirement. Thank you for your efforts.

Code:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[3].Text.Equals("Pending"))
{
CommandField link = new CommandField();
link.SelectText = "Pending";
link.ButtonType = ButtonType.Link;
link.ShowSelectButton = true;
e.Row.Cells[3].Controls.Add(link); -----> Invalid argument
}
}
}
Rojalin Sahoo 8-Jul-15 2:10am    
if you change this bound field as template field and append item into that may it work for you like:


<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:TemplateField HeaderText="Name">
<itemtemplate>
<asp:Label id="lblcol4" runat="server" Text='<%#Eval("COL-4")%>'>



<alternatingrowstyle backcolor="White">
<footerstyle backcolor="#CCCC99">
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />



and in code behind
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.FindControl("lblcol4") as Label).Text.Equals("Pending"))
{
HyperLink link = new HyperLink();
link.Text = "Pending";
link.NavigateUrl = "NewPage.aspx";
e.Row.Cells[3].Controls.Add(link);

}
}
}
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);
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900