Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sir i have taken two field in table id and filename .i uploaded file successfully and also show that file name in grid,i want to do when i click on filename in grid that file open in new window please help. regards dnyaneshwar

my code is here .cs code

C#
protected void btnSaveFile_Click(object sender, EventArgs e)
    {
        string uploadFolder = Server.MapPath ("FileStore\\");

        FileUpload1.SaveAs(uploadFolder + FileUpload1.FileName);
        Label1.Text = "File uploaded successfully: " + FileUpload1.PostedFile.FileName;
 string filename=FileUpload1.PostedFile.FileName.ToString();
        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
        SqlCommand cmd = new SqlCommand("sp_SaveFile", sqlcon);
        SqlParameter[] sqlp = new SqlParameter[] { new SqlParameter("@filename",filename)};
        cmd.Parameters.AddRange(sqlp);
        cmd.CommandType = CommandType.StoredProcedure;

        sqlcon.Open();
        int ret =cmd.ExecuteNonQuery();
        sqlcon.Close();
        if (ret == 1)
        {
            lblmsg.Text = "Filename inserted.";
        }
        BindGrid();
    }

.aspx code
XML
<table width="100%">
                <tr>
                    <td colspan="2" class="heading">
                        <asp:Label ID="lblFileTitle" runat="server" Text="File Upload" Font-Size="XX-Large"> </asp:Label>
                    </td>
                </tr>
                <tr>
                    <td style="width: 10%" align="right">
                        <asp:Label ID="lblchosefile" runat="server" Text="Choose File : "></asp:Label>
                    </td>
                    <td>
                        <asp:FileUpload ID="FileUpload1" runat="server" />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="FileUpload1"
                            ErrorMessage="Choose a file!">
                        </asp:RequiredFieldValidator>
                        <asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="OrangeRed"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="width: 100%; padding-left: 200px;">
                        <asp:Button ID="btnSaveFile" runat="server" Text="Save" Font-Bold="true" OnClick="btnSaveFile_Click" />
                        <asp:Label ID="lblmsg" runat="server" ForeColor="Blue"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="padding-left: 50px">
                        <asp:GridView ID="GridViewFile" runat="server" AllowPaging="true" AllowSorting="true"
                            AutoGenerateColumns="false" PageSize="3" DataKeyNames="FileID" OnRowDataBound="GridViewFile_RowDataBound"
                            OnRowCreated="GridViewFile_RowCreated">
                            <Columns>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        <asp:CheckBox ID="HeaderChk" runat="server" ToolTip="Select All" onclick="javascript:CheckAll(this)" />
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:CheckBox ID="ChildChk" runat="server" ToolTip="Select" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        Serial No.</HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblSRNO" runat="server" Text='<%# Container.DataItemIndex+1 %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="NameFile" HeaderText="File Name" SortExpression="NameFile" />

                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>
            </table>
Posted
Updated 11-Jun-12 0:14am
v2
Comments
Sandeep Mewara 11-Jun-12 8:26am    
You get any error?

1 solution

Hi dnyaneshwar,

Add a hyperlink column in your gridview like:

CSS
<asp:HyperLinkField DataNavigateUrlFields="NameFile"
                    DataNavigateUrlFormatString="(a location here)"
                    DataTextField="NameFile" HeaderText="File Name"
                    NavigateUrl="(a location)" />


and make the visible property of
<asp:boundfield datafield="NameFile" headertext="File Name" sortexpression="NameFile" xmlns:asp="#unknown" />
false.


Thanks
 
Share this answer
 
v2

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