Postback does not maintain sort direction Image?

Discussion in 'ASP .Net Datagrid Control' started by rockdale, Jun 8, 2006.

  1. rockdale

    rockdale Guest

    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

    }
     
    rockdale, Jun 8, 2006
    #1
    1. Advertising

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

    "rockdale" <> wrote in message
    news:...
    > 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
    >
    > }
    >
     
    Balasubramanian Ramanathan, Jun 14, 2006
    #2
    1. Advertising

  3. rockdale

    rockdale Guest

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

    Balasubramanian Ramanathan wrote:
    > I had the same problem and adding the image in prerender event of the
    > gridview/datagrid solved the problem..
    >
    > "rockdale" <> wrote in message
    > news:...
    > > 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
    > >
    > > }
    > >
     
    rockdale, Jun 19, 2006
    #3
  4. There is no trick in that just use the headerrow property of the gridview

    GridViewRow hdr = Grid.HeaderRow;

    "rockdale" <> wrote in message
    news:...
    > Thanks for your suggestion, But how can I get the DatagridHeader in
    > preRender event?
    > Thanks a lot
    >
    > Balasubramanian Ramanathan wrote:
    >> I had the same problem and adding the image in prerender event of the
    >> gridview/datagrid solved the problem..
    >>
    >> "rockdale" <> wrote in message
    >> news:...
    >> > 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
    >> >
    >> > }
    >> >

    >
     
    Balasubramanian Ramanathan, Jun 21, 2006
    #4
  5. rockdale

    rockdale Guest

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



    Balasubramanian Ramanathan wrote:
    > There is no trick in that just use the headerrow property of the gridview
    >
    > GridViewRow hdr = Grid.HeaderRow;
    >
    > "rockdale" <> wrote in message
    > news:...
    > > Thanks for your suggestion, But how can I get the DatagridHeader in
    > > preRender event?
    > > Thanks a lot
    > >
    > > Balasubramanian Ramanathan wrote:
    > >> I had the same problem and adding the image in prerender event of the
    > >> gridview/datagrid solved the problem..
    > >>
    > >> "rockdale" <> wrote in message
    > >> news:...
    > >> > 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
    > >> >
    > >> > }
    > >> >

    > >
     
    rockdale, Jun 21, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bruno Ferreira
    Replies:
    0
    Views:
    454
    Bruno Ferreira
    Nov 6, 2003
  2. rockdale
    Replies:
    0
    Views:
    800
    rockdale
    Jun 8, 2006
  3. Keithb
    Replies:
    0
    Views:
    991
    Keithb
    Oct 28, 2006
  4. ASF
    Replies:
    3
    Views:
    2,955
  5. Navin
    Replies:
    1
    Views:
    727
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page