Postback does not maintain sort direction Image?

R

rockdale

Hi, All:
I dynamic add an arrow up/down image in OnItemDataBound event for my
datagrid, it works fine. But I have another textbox which trigger
postback and doing something, after this event get executed, I lost the
image but the datagrid maintain the same sort order. Should the
datagrid remain what it looks like (with the riget arrow up/down image)
before the postback caused by the textbox?

I tried to get the datagrid header in the text postback event
(OnChanged), but I do not know how to get the datagrid header. I do not
want to rebind the datagrid in the textbox OnChanged event.

Or should I use other method to show the sort direction image?


private void dgrid_OnItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//Sorting Image
if(e.Item.ItemType == ListItemType.Header)
{
foreach(TableCell tc in e.Item.Cells)
{
if(tc.Controls.Count > 0)
{
try
{
LinkButton lkbSortCol =
(System.Web.UI.WebControls.LinkButton)tc.Controls[0];
if(lkbSortCol.CommandArgument.ToString().Equals(dgrid.Attributes["SortExpression"]))
{
System.Web.UI.WebControls.Image imgSort = new
System.Web.UI.WebControls.Image();
if(dgrid.Attributes["SortDirection"].Equals("DESC"))
{
imgSort.ImageUrl = Server.MapPath("Images/sort_desc.gif");
}
else
{
imgSort.ImageUrl = Server.MapPath("Images/sort_asc.gif");
}


tc.Controls.AddAt(1, imgSort);
}
}
catch(Exception ex)
{
Trace.Write(ex.Message);
}
}//end if tc.controls.count > 0
}//end for

} //end item type

}
 
B

Balasubramanian Ramanathan

I had the same problem and adding the image in prerender event of the
gridview/datagrid solved the problem..
 
R

rockdale

Thanks for your suggestion, But how can I get the DatagridHeader in
preRender event?
Thanks a lot

Balasubramanian said:
I had the same problem and adding the image in prerender event of the
gridview/datagrid solved the problem..

rockdale said:
Hi, All:
I dynamic add an arrow up/down image in OnItemDataBound event for my
datagrid, it works fine. But I have another textbox which trigger
postback and doing something, after this event get executed, I lost the
image but the datagrid maintain the same sort order. Should the
datagrid remain what it looks like (with the riget arrow up/down image)
before the postback caused by the textbox?

I tried to get the datagrid header in the text postback event
(OnChanged), but I do not know how to get the datagrid header. I do not
want to rebind the datagrid in the textbox OnChanged event.

Or should I use other method to show the sort direction image?


private void dgrid_OnItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//Sorting Image
if(e.Item.ItemType == ListItemType.Header)
{
foreach(TableCell tc in e.Item.Cells)
{
if(tc.Controls.Count > 0)
{
try
{
LinkButton lkbSortCol =
(System.Web.UI.WebControls.LinkButton)tc.Controls[0];
if(lkbSortCol.CommandArgument.ToString().Equals(dgrid.Attributes["SortExpression"]))
{
System.Web.UI.WebControls.Image imgSort = new
System.Web.UI.WebControls.Image();
if(dgrid.Attributes["SortDirection"].Equals("DESC"))
{
imgSort.ImageUrl = Server.MapPath("Images/sort_desc.gif");
}
else
{
imgSort.ImageUrl = Server.MapPath("Images/sort_asc.gif");
}


tc.Controls.AddAt(1, imgSort);
}
}
catch(Exception ex)
{
Trace.Write(ex.Message);
}
}//end if tc.controls.count > 0
}//end for

} //end item type

}
 
B

Balasubramanian Ramanathan

There is no trick in that just use the headerrow property of the gridview

GridViewRow hdr = Grid.HeaderRow;

rockdale said:
Thanks for your suggestion, But how can I get the DatagridHeader in
preRender event?
Thanks a lot

Balasubramanian said:
I had the same problem and adding the image in prerender event of the
gridview/datagrid solved the problem..

rockdale said:
Hi, All:
I dynamic add an arrow up/down image in OnItemDataBound event for my
datagrid, it works fine. But I have another textbox which trigger
postback and doing something, after this event get executed, I lost the
image but the datagrid maintain the same sort order. Should the
datagrid remain what it looks like (with the riget arrow up/down image)
before the postback caused by the textbox?

I tried to get the datagrid header in the text postback event
(OnChanged), but I do not know how to get the datagrid header. I do not
want to rebind the datagrid in the textbox OnChanged event.

Or should I use other method to show the sort direction image?


private void dgrid_OnItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//Sorting Image
if(e.Item.ItemType == ListItemType.Header)
{
foreach(TableCell tc in e.Item.Cells)
{
if(tc.Controls.Count > 0)
{
try
{
LinkButton lkbSortCol =
(System.Web.UI.WebControls.LinkButton)tc.Controls[0];
if(lkbSortCol.CommandArgument.ToString().Equals(dgrid.Attributes["SortExpression"]))
{
System.Web.UI.WebControls.Image imgSort = new
System.Web.UI.WebControls.Image();
if(dgrid.Attributes["SortDirection"].Equals("DESC"))
{
imgSort.ImageUrl = Server.MapPath("Images/sort_desc.gif");
}
else
{
imgSort.ImageUrl = Server.MapPath("Images/sort_asc.gif");
}


tc.Controls.AddAt(1, imgSort);
}
}
catch(Exception ex)
{
Trace.Write(ex.Message);
}
}//end if tc.controls.count > 0
}//end for

} //end item type

}
 
R

rockdale

Thanks, but I am not using GridView.
Anyway, I move my code to OnItemCreate event and get what I wanted.



Balasubramanian said:
There is no trick in that just use the headerrow property of the gridview

GridViewRow hdr = Grid.HeaderRow;

rockdale said:
Thanks for your suggestion, But how can I get the DatagridHeader in
preRender event?
Thanks a lot

Balasubramanian said:
I had the same problem and adding the image in prerender event of the
gridview/datagrid solved the problem..

Hi, All:
I dynamic add an arrow up/down image in OnItemDataBound event for my
datagrid, it works fine. But I have another textbox which trigger
postback and doing something, after this event get executed, I lost the
image but the datagrid maintain the same sort order. Should the
datagrid remain what it looks like (with the riget arrow up/down image)
before the postback caused by the textbox?

I tried to get the datagrid header in the text postback event
(OnChanged), but I do not know how to get the datagrid header. I do not
want to rebind the datagrid in the textbox OnChanged event.

Or should I use other method to show the sort direction image?


private void dgrid_OnItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//Sorting Image
if(e.Item.ItemType == ListItemType.Header)
{
foreach(TableCell tc in e.Item.Cells)
{
if(tc.Controls.Count > 0)
{
try
{
LinkButton lkbSortCol =
(System.Web.UI.WebControls.LinkButton)tc.Controls[0];
if(lkbSortCol.CommandArgument.ToString().Equals(dgrid.Attributes["SortExpression"]))
{
System.Web.UI.WebControls.Image imgSort = new
System.Web.UI.WebControls.Image();
if(dgrid.Attributes["SortDirection"].Equals("DESC"))
{
imgSort.ImageUrl = Server.MapPath("Images/sort_desc.gif");
}
else
{
imgSort.ImageUrl = Server.MapPath("Images/sort_asc.gif");
}


tc.Controls.AddAt(1, imgSort);
}
}
catch(Exception ex)
{
Trace.Write(ex.Message);
}
}//end if tc.controls.count > 0
}//end for

} //end item type

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top