Reputation: 128
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 isn't triggered, the cookie isn't loaded and the redirected page doesn't displays data.
Please help me out guys. Thanks.
Code:
<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);
}
}
}
Upvotes: 0
Views: 2479
Reputation: 128
Removed GridView1_SelectedIndexChanged() event and passed TransId as parameter.
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);
}
}
}
Upvotes: 0
Reputation: 1045
You are calling a redirect to another page when you click a cell, so the current page is not incurring a postback. This means that your SelectedIndexChanged function is not being hit because ASP.NET is instead loading the new page.
Try causing a postback to the same page, then getting the selected row in the Page_Load function before calling a Response.Redirect yourself.
Upvotes: 0