GridImageColumn + File.Exists

4 posts, 0 answers
  1. Marko
    Marko avatar
    5 posts
    Member since:
    May 2015

    Posted 07 Feb Link to this post

    Hello!

    I'm testing telerik:RadGrid control..

    I would like to use picture (if it exists) on server. If file doesn't exists i would like to use "default_image.jpg". I suppose have to use RadGrid1_ItemDataBound but don't know how?

    Right now fixing DataImageUrlFields="Artikal_ID" DataImageUrlFormatString="IMG/{0}_tiny.jpg" work without problem, but don't know to test File.Exists and use correct value.

    Any advice?

    Now have something like this:

    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" GroupPanelPosition="Top" PageSize="20" AllowSorting="True" AllowPaging="True" OnItemCommand="RadGrid1_ItemCommand" OnItemDataBound="RadGrid1_ItemDataBound">
        <ExportSettings>
            <Pdf PageWidth="">
            </Pdf>
     
        </ExportSettings>
        <MasterTableView NoMasterRecordsText="Molimo odaberite kategoriju." AllowMultiColumnSorting="True">
            <Columns>
                <telerik:GridBoundColumn DataField="Artikal_ID" FilterControlAltText="Filter Artikal_ID column" HeaderText="Šifra artikla" ReadOnly="True" SortExpression="Artikal_ID" UniqueName="Artikal_ID">
     
                    <ColumnValidationSettings>
                        <ModelErrorMessage Text=""></ModelErrorMessage>
                    </ColumnValidationSettings>
     
                </telerik:GridBoundColumn>
     
                <telerik:GridImageColumn DataImageUrlFields="Artikal_ID" DataImageUrlFormatString="IMG/{0}_tiny.jpg" AlternateText="" DataAlternateTextField="Artikal"
                    ImageAlign="Middle" ImageWidth="60px" HeaderText="" HeaderButtonType="None">
                    <ItemStyle CssClass="RadGrid" Height="80px" HorizontalAlign="Center" VerticalAlign="Middle" />
                </telerik:GridImageColumn>

     

     

  2. Marko
    Marko avatar
    5 posts
    Member since:
    May 2015

    Posted 08 Feb in reply to Marko Link to this post

    Sorry all, forget to wrote known RadGrid1_ItemDataBound code:

    Maybe you can now easyer give me a hint about question:

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
           {
     
     
               if (e.Item is GridDataItem)
               {
                   GridDataItem dataItem = e.Item as GridDataItem;
                   string file_path = "";
                   file_path = ("IMG\\" + dataItem["Artikal_ID"].Text + "_big.jpg");
                   if (File.Exists(Server.MapPath(file_path)))
                   {
                       file_path = ("IMG\\" + dataItem["Artikal_ID"].Text + "_big.jpg");
                       //missing code
                   }
                   else
                   {
                       file_path = ("IMG\\default_big.jpg");
                       ///missing code
                   }
     
               }
           }

    Thank you!
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Marko
    Marko avatar
    5 posts
    Member since:
    May 2015

    Posted 09 Feb in reply to Marko Link to this post

    Finnaly find solution :-)

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
          {
     
     
              if (e.Item is GridDataItem)
              {
                  GridDataItem dataItem = e.Item as GridDataItem;
                  string file_path = "";
                  file_path = ("IMG\\" + dataItem["Artikal_ID"].Text + "_big.jpg");
                  if (File.Exists(Server.MapPath(file_path)))
                  {
                      file_path = ("IMG\\" + dataItem["Artikal_ID"].Text + "_big.jpg");
                      Image img_thumb = dataItem["Artikal"].Controls[0] as Image;
                      img_thumb.ImageUrl = file_path;
                  }
                  else
                  {
                      
                      file_path = ("IMG\\default_big.jpg");
                      Image img_thumb = dataItem["Artikal"].Controls[0] as Image;
                      img_thumb.ImageUrl = file_path;
     
                  }
     
              }
          }

     Best regards!

     

     

     

  5. Eyup
    Admin
    Eyup avatar
    3014 posts

    Posted 11 Feb Link to this post

    Hello Marko,

    Thank you for sharing your approach with our community. I hope it will prove helpful to other developers as well.
    Alternatively, you can also use GridTemplateColumn to build-up your own custom configuration:
    http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/columns/column-types#gridtemplatecolumn

    Regards,
    Eyup
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top