Hello guys,
I'm a newbie to asp.net....I've recently designed a website in which I've taken a gridview control and added a button in the itemtemplate field of the gridview........What I want is that if anyone clicks on this button then the data in the particular row of the gridview comes for the download to user (like open,save etc.).....please make your response as soon as possible.
Thanks in advance.

Recommended Answers

All 14 Replies

so wot's the problem...
on click event set PostBackUrl = "Download Link depend on id"

try that and let me know the o/p

actually I don't know how to do it all.........I've taken a button field here's the .aspx code.......

<asp:TemplateField HeaderText="Download Link">
<ItemTemplate>
<asp:Button ID="DownloadPicb" runat="server" Text="Download" OnClick="DownloadPicb_OnClick" />
</ItemTemplate>
</asp:TemplateField>

and I'm asking what to write in.........

protected void DownloadPicb_OnClick(object sender, EventArgs e)
{
        ??????? 
}

Hope you'll make a quick reply as earlier.

Thanks.

You can't use button events. You need to handle the events of GridView control. Read MSDN pages or reference book about "GridView Events".

Sorry to all experts but I'm still unable to find an appropriate solution.
If anyone who knows how to resolve my problem then show your kind response as soon as possible.......

I've recently used the following code in Pics.aspx........

<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="DownloadPicl" runat="server" Text="Download" PostBackUrl='<%# (string) FormatImageUrl1( (string) Eval("PicUrl")) %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

And in Pics.cs, I've used.........

protected string FormatImageUrl1(string url1)
{
if (url1 != null && url1.Length > 0)
return ("~/" + url1);
else return null;
}

And the error is as follows...........

The HTTP verb POST used to access path '/KMWorld/Pictures/fatmanpolarbear.jpg' is not allowed.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The HTTP verb POST used to access path '/KMWorld/Pictures/fatmanpolarbear.jpg' is not allowed.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[HttpException (0x80004005): The HTTP verb POST used to access path '/KMWorld/Pictures/fatmanpolarbear.jpg' is not allowed.]
   System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback callback, Object state)
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +3391004
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +161
<asp:LinkButton 
  ID="DownloadPicl" 
  runat="server" 
  Text="Download" 
  PostBackUrl='<%# Eval("PicUrl","~/{0}")) %>'
/>

Sorry adatapost, It's showing the same error again.......

Post the complete code please.

Have a look at code-snippet.

Markup

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" 
                            PostBackUrl='<%# Eval("url","~/Images/{0}") %>'>LinkButton</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Code-behind

protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add("url");

            dt.Rows.Add("sample.jpg");

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

Post the complete code please.

Have a look at code-snippet.

Markup

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" 
                            PostBackUrl='<%# Eval("url","~/Images/{0}") %>'>LinkButton</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Code-behind

protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add("url");

            dt.Rows.Add("sample.jpg");

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

Again facing the same problem.
Here's my complete code........

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Pics.aspx.cs" Inherits="Pics" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<table cellpadding="0" cellspacing="0" align="center">
<tr>
<td align="center">
<div align="center">
<asp:GridView ID="Picg" runat="server" Width="700px" Height="650px" AutoGenerateColumns="False"
BorderWidth="1px" BackColor="White" CellPadding="3" BorderStyle="None" BorderColor="#CCCCCC"
Font-Names="Arial">
<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
<PagerStyle ForeColor="#000066" HorizontalAlign="Left" BackColor="White"></PagerStyle>
<HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#006699"></HeaderStyle>
<Columns>
<asp:BoundField DataField="PicID" HeaderText="ID" />
<asp:BoundField DataField="PicName" HeaderText="Name" />
<asp:BoundField DataField="PicCategory" HeaderText="Category" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageAlign="Middle" ImageUrl='<%# (string) FormatImageUrl( (string) Eval("PicUrl")) %>' Height="100px" Width="100px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="DownloadPicl" runat="server" Text="Download" PostBackUrl='<%# Eval("PicUrl","~/{0}") %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle ForeColor="White" Font-Bold="True" BackColor="#669999"></SelectedRowStyle>
<RowStyle ForeColor="#000066"></RowStyle>
</asp:GridView>
</div>
</td>
</tr>
</table>
</asp:Content>

And here's the c# code...........

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Pics : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["KMCon"].ToString());
SqlDataAdapter da = new SqlDataAdapter("select PicID,PicName,PicCategory,PicUrl from Pics order by PicName", con);
DataSet ds = new DataSet();
da.Fill(ds);
Picg.DataSource = ds;
Picg.DataBind(); 
}
protected string FormatImageUrl(string url)
{
if (url != null && url.Length > 0)
return ("~/" + url);
else return null;
}
}

Do not use LinkButton, use Hyperlink.

<asp:HyperLink ID="link1" runat="server"
                           NavigateUrl='<%# Eval("url","~/{0}") %>'
                          >
                           Show
 </asp:HyperLink>
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) {
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["KMCon"].ToString());
SqlDataAdapter da = new SqlDataAdapter("select PicID,PicName,PicCategory,PicUrl from Pics order by PicName", con);
DataSet ds = new DataSet();
da.Fill(ds);
Picg.DataSource = ds;
Picg.DataBind(); 
 }
}
commented: You're a genius bro..... +1

Thanks for your kind and quick response.

Can you also tell me how to make a download link for the Songs retrieved in gridview......

In itemTemplate setup a link
where you bind your link with Id of Music which you have kept in u r database..
on click go to some page with id and initiate downloading..

hope that helps/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.