Click here to Skip to main content
15,908,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to fetch images from a website by typing the website link in a text box and then click on fetch button, then i bind them with my gridview. That gridview contains images, checkboxes, a submit button out side the gridview. here is my code for grid view and code behind:
ASP.NET
<asp:TextBox ID="txt_link" runat="server" CssClass="form-control"></asp:TextBox>
    <span class="input-group-btn">
        <asp:Button ID="btn_load" runat="server" Text="Load Values!" 
        CssClass="btn btn-info" onclick="btn_load_Click" />
   <asp:GridView ID="GridView1" CssClass="table table-responsive table-condensed" runat="server"
    AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDeleting="GridView1_RowDeleting"
    OnRowEditing="GridView1_RowEditing">
    <Columns>
      <asp:TemplateField>
            <HeaderTemplate>
                S No
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Container.DataItemIndex + 1 %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Left" CssClass="ItemSize" />
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>
              img
            </HeaderTemplate>
            <ItemTemplate>
               <asp:Image ID="Image" runat="server" ImageUrl='<%# Container.DataItem %>' />
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Left" CssClass="ItemSize" />
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>
                Product Link
            </HeaderTemplate>
            <ItemTemplate>
             <asp:CheckBox ID="chebx" runat="server"/>

            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
    </asp:GridView>

Code Behind:
C#
protected void btn_load_Click(object sender, EventArgs e)
{
 HtmlWeb web = new HtmlWeb();
    HtmlAgilityPack.HtmlDocument doc = web.Load(txt_link.Text);
  var rateNode1 = from info in doc.DocumentNode.SelectNodes("//div[@class='ui-box ui-box-normal product-custom-desc']")
                       from linkee in info.SelectNodes("//img").Where(x=>x.Attributes.Contains("src"))
                       select new
                       {
                        linkee.Attributes["src"].Value
                       };
 List<String> images = new List<string>(rateNode1.Count());
  foreach (var a in rateNode1)
   {

       result = a.Value;

       images.Add(result.Substring(0,result.Length-10));

   }
   GridView1.DataSource = images;
   GridView1.DataBind();}

Up to here it worked fine. And for saving to local folder here is the code:
C#
var folder = Server.MapPath("~/assets/" + txt_orig_id.Text);
    string filePath="";
    if (!Directory.Exists(folder))
    {
        Directory.CreateDirectory(folder);
         filePath = folder;
    }
    foreach (GridViewRow row in GridView1.Rows)
    {

            //CheckBox checkbox = row.FindControl("chebx") as CheckBox;
            CheckBox chk = (CheckBox)row.FindControl("chebx");
            Image img = row.FindControl("Image") as Image;
            if (chk.Checked)
            {
                Response.ContentType = ContentType;
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
                Response.WriteFile(filePath);
                Response.End();

            }
            else {

            }

    }

but the main issue is it shows that none of the check box is checked. please tell me whats wrong in my code
Posted
Comments
Do you bind the GridView inside page load?
saifullahiit 16-Aug-15 1:12am    
no. 1. enter web page link in text box and hit load button
2. then it loads images in grid view.
Then what you do with GridView?
saifullahiit 16-Aug-15 5:08am    
the entire code is above please read it carefully
Black Mamba Elapidae 17-Aug-15 1:57am    
Hey, what do you mean by "..but the main issue is it shows that none of the check box is checked..", Does that means when you debug the code , you never enter in, " if (chk.Checked) { }" loop ???
Because when I wrote a simpler form of your code(i.e same UI just simpler code behind) I was able to get inside the loop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900