locked
How to get control value from gridview column in dd? RRS feed

  • Question

  • User961784352 posted

    I'm saving image paths in a db field.

    When I want to delete the record, I also want to delete the image from disk as well.

    In a standard gridview I would write something like this:

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
            {
                Image oImg = GridView1.Rows[e.RowIndex].FindControl("Image1") as Image;
                if (oImg != null)
                {
                    string sUrl = oImg.ImageUrl;
    
                    File.Delete(Server.MapPath(sUrl));
    
                    GridView1.DataBind();
                }
            }
    

    But with dynamic data it doesn't seem to be the case - if i remove my if(oimg !=null) check I'll get the object reference not set to an instance of an object error.

    Any idea how we can find the control?
    It's also for a single purpose custom page, so If I need to reference the specific column (ie cells[0] that's fine too).

    Thanks all!

    Thursday, March 3, 2011 9:43 PM

Answers

  • User-330204900 posted

    you would need to use the column name not the control id

    Trace.Warn("x", (Image)e.Values["Path"].ImageURL);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 6, 2011 10:53 AM

All replies

  • User1983487377 posted

    hi..

    Take another field which will be hidden in grid and store imagePath in the Field..

    when trying to delete, as you are doing.. find the path of Image from the hiddenfield(Imagepath)

    and in try to delete the file using that path instead of fetching URL from displayed Image..

     

    all the Best

     

    Friday, March 4, 2011 12:12 AM
  • User16212438 posted

    Depends on what you mean by Dynamic Data. If you mean you do something in the RowDataBound handler, you may want to consider doing it in the RowCreated handler instead. But that will only work if there is no relation to the data. If there is a relation to the data, consider setting EnableViewState="false" on the GridView. This will cause a DataBind on each PostBack, so RowDataBound fires again. If the performance impact of this is too much, a more complicated solution will be needed, where you use ViewState to restore each Row in RowCreated.

    Friday, March 4, 2011 9:44 AM
  • User961784352 posted

     It's a DD project, (sorry had originally put it in the wrong forum)

    Friday, March 4, 2011 8:25 PM
  • User3866881 posted

    Hi,

    Please first confirm that your "Image1" is really the Id of your Image control (No case sensetive problem, no wrong spelling……).

    I wrote another specific kind of FindControl, it will deeply search for the control of the specific kind according to the specific name, you can see this (This is a  generally created deeply searching control). Hope this helps you can have a try:

    private static T DeepFindControl<T>(Control control, string id, List<T> controls) where T : Control

            {
                T result = control.FindControl(id) as T;
                if (result != null && result.ID != null && result.ID.Equals(id) && !controls.Contains(result))
                {
                    controls.Add(result);
                }
                    if (control.HasControls())
                    {
                        foreach (Control c in control.Controls)
                        {
                             DeepFindControl<T>(c, id, controls);
                        }
                    }
                return result;
            }
            public static List<T> DeepSearchControl<T>(this Page page, string id) where T : Control
            {
                List<T> controls = new List<T>();
                if (page.HasControls())
                {
                    foreach (Control c in page.Controls)
                    {
                        DeepFindControl<T>(c, id, controls);
                    }
                }
                return controls;
            }
    You can call this: DeepSearchControl<Image>(Page,"Image1")[0];
    Saturday, March 5, 2011 8:41 PM
  • User-330204900 posted

    Hi Zhuges, have a look at the e.Values this is a Dictionary<String,Object> you could get the value like;

    e.Values["Image1"]
    
    Sunday, March 6, 2011 7:29 AM
  • User961784352 posted

    Steve,

    Been messing with this one. Kept getting object reference not set.... so maybe you can help clear this up:

    The control name is Image1
    The table column name is Path
    The metadata I've set to rename the column is Media

    I'm trying to do a simple trace.warn just so I can see the output but I keep getting the object reference msg.
    I tried passing in all three (Image1, Path, Media) as the value but it can't seem to find any of them.

    (here's my line: Trace.Warn("x", e.Values["Image1"].ToString());)

    Other posts have mentioned you should pass in the name of the column rather than the control.
    If it is the control, wouldn't I need to specify more than just ToString since we are actually trying to get the ImageURL from the control?

    I was hoping for I could do something like a Trace.Warn("x", (Image)e.Values["Image1"].ImageURL); but I think it might be too late in the process for that (or I'm just not accessing it right of course ;) )

    Or, do you know how to output the list of values? That might be good enough to get me going in the right direction.

    Your thoughts?

    Sunday, March 6, 2011 10:48 AM
  • User-330204900 posted

    you would need to use the column name not the control id

    Trace.Warn("x", (Image)e.Values["Path"].ImageURL);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 6, 2011 10:53 AM