i have a gridview that i filled it with filenames from a path, know, i want the filenames to be clickable so i can download them and i don't know how, can someone help me.
code-behind (file is the gridview ID)
string path = "//the path";
string[] Fname = Directory.GetFiles(path).Select(Path.GetFileName).ToArray();
file.DataSource = Fname;
file.DataBind();
the web page
<asp:GridView ID="file" runat="server" style="border:hidden" ></asp:GridView>
You can bind a single list/array item and get it in the ItemTemplate like this:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a target="_blank" href="<%# Container.DataItem %>">
<%# Container.DataItem %>
</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:HyperLink ID="UserId" runat="server" DataNavigateUrlFields="UserId" NavigateUrl='<%# "~/Report.aspx?name=" %>'
Text='<%# Eval("UserId") %>' ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Related
I have a gridview with 2 columns ([+] and [data]). On click of [+] sign it opens a girdview inside the same gridview.
Now, in child gridview I have a link button, on click of that I display some data. On postback the gridview retains its original position, I want to show the child gridview as it is.
Code.
<asp:GridView ID="grvNeverTouchedQuartile" class="form-table" Width="100%" OnRowCommand="grvNeverTouchedQuartile_RowCommand"
AutoGenerateColumns="false" runat="server" OnRowDataBound="grvNeverTouchedQuartile_RowDataBound"
DataKeyNames="QuartileType">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%--<asp:LinkButton ID="lnkbtnNTQuartile" runat="server" Text='<%# Eval("Quartile") %>'
CommandArgument='<%# Eval("QuartileType") %>' CommandName="NeverTouched"></asp:LinkButton>--%>
<img alt="Image not available" style="cursor: pointer" src="../images/plus.png" />
<asp:Panel ID="pnl_NTChildGrid" runat="server" Style="display: none">
<table>
<tr>
<td>
<div style="overflow: auto;">
<asp:GridView ID="grdNTInsuranceData" runat="server" AutoGenerateColumns="false" OnRowCommand="grdNTInsuranceData_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Insurance Name">
<ItemTemplate>
<asp:Label ID="lblNTQuartileType" runat="server" Text='<%# Eval("QuartileType") %>' Visible="false"></asp:Label>
<asp:Label ID="lblNTInsuranceName" runat="server" Text='<%# Eval("InsuranceName") %>' Visible="false"></asp:Label>
<asp:LinkButton ID="lnkbtnNTInsuranceQuartile" runat="server" Text='<%# Eval("InsuranceNameDetails") %>'
CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' CommandName="NeverTouchedInsurance"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</table>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quartile[Count - $Value]">
<ItemTemplate>
<asp:Label ID="lblNTQuartile" runat="server" Text='<%# Eval("Quartile") %>'></asp:Label>
<%--<asp:LinkButton ID="lnkbtnNTQuartile" runat="server" Text='<%# Eval("Quartile") %>'
CommandArgument='<%# Eval("QuartileType") %>' CommandName="NeverTouched"></asp:LinkButton>--%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Thanks in Advance!!!
There are some ways (This is not with code it is just my suggestions, there are codes over internet or it is based on experience) so in my opinion you can use one of below cases:
1- You should use UpdatePanel to prevent refreshing page and put the grid inside UpdatePanel, just for sample like below:
<asp:UpdatePanel ID="panelId" UpdateMode="Conditional" runat="server" >
<ContentTemplate>
<asp:GridView ID="gvPrList" runat="server" AutoGenerateColumns="false" AllowPaging="false"
AllowSorting="false" CssClass="list-table" HeaderStyle-CssClass="header">
<Columns>
<ItemTemplate>
<asp:LinkButton ID="lnkbtnNTInsuranceQuartile" runat="server" Text='<%# Eval("InsuranceNameDetails") %>'
CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' CommandName="NeverTouchedInsurance"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
2- the runat of lnkbtnNTInsuranceQuartile is server which main after it's click post-back will be occurred so page get refreshed for this you can change your lnkbtnNTInsuranceQuartile to HTML element such as <div><a class="x">click here</a><span class="detail"/></div> and then instead of lnkbtnNTInsuranceQuartile click use ajax and then update details Span something like :
$('.x').click(function () {
var $me = this;
$.ajax({
url: 'Your Web Method address',
data: { youData},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
//update span here
$me.parent().find('.details.'.html(your response);
},
error: function (x, e) {
}
});
});
3- Use client-side Grid not ASP.Net GridView
4- Add collapsed th's id into localstorage and after page load open it again...
5- etc...
all three above steps can be implemented based on your scenario...
Hope will help you
I've got a field that retrieves a folder path. However it makes the column width too wide. I was wondering if there was a way to truncate the returned value so I can show just the file name without it's path.
<asp:TemplateField ControlStyle-CssClass="columnWidth"
HeaderText="File Name " SortExpression="FileName">
<EditItemTemplate>
<asp:Label ID="Label12" runat="server"
Text='<%# Bind("FileName")%>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton runat="server" ID="download"
CausesValidation="false" OnClick="download_Click"
CommandArgument="<% %>" CommandName="Download"
Text='<%#Bind("FileName")%>'></asp:LinkButton>
</ItemTemplate>
<HeaderStyle CssClass="gridHeader columnWidth" ForeColor="#A1ADBB" />
</asp:TemplateField>
.aspx file
<asp:Label ID="Label12" runat="server"
Text='<%# GetFileName( Convert.ToString( Eval("FilePath") ) ) %>'
</asp:Label>
.cs file
using System.IO;
protected string GetImagePath( string FilePath)
{
return Path.GetFileName(FilePath);
}
I have two file upload controls inside GridViewControl for uploading files, I have a button for uploading files inside gridview, I am using its RowCommandEvent for uploading files. How can I get values of fileupload controls in RowCommandEvent?
This is my GridView
<asp:GridView ID="DgFiles" runat="server" AutoGenerateColumns="false" OnRowCommand="DgFiles_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Crime" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="category" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Category") %>' ></asp:Label><br/>
<asp:FileUpload ID="file1" runat="server" /><br />
<asp:FileUpload ID="file2" runat="server" />
<asp:Button ID="btnupload" runat="server" CommandName="upload" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lbldate" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblcity" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "City") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and my RowCommandEvent
protected void DgFiles_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "upload")
{
}
}
I have to display records in GridView like structure, with file upload control inside it, If there is alternate way I can do it, kindly suggest.
First,
You have to add a command column with command name upload
second,
You have to find the control inside the row by using parent property.
FileUpload fl1 = (FileUpload)e.Row[rowindex].Cells<cellindex>.FindControl("File1")
FileUpload fl2 = (FileUpload)e.Row[rowindex].Cells<cellindex>.FindControl("File2")
Now You can use fl1 and fl2 as your filecontrols.
I have a GridView control with an ItemTemplate that has a HiddenField.
<asp:GridView ID="GridView1" runat="server" ...>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField runat="server" ID="HiddenField1" Value='<% Response.Write(Guid.NewGuid()) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The problem is that it actually renders exactly <% Response.Write(Guid.NewGuid()) %> instead of a Guid. How do I set the value to render as a Guid and execute the code rather than interpret it as a literal. I've tried using both single quotes ' and double quotes ".
You can try
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField runat="server" ID="HiddenField1" Value='<%# Guid.NewGuid().ToString() %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You have to add # In inline coding . Ex : '<%# //code here %>' and
you are setting HiddenField1 value ,so you dont need to use Response.Write . The value should be Value='<%# Guid.NewGuid().ToString() %>' . Hope this will do
You can get the GUID in code behind as follows.
HiddenField HiddenField1 = (HiddenField)row.FindControl("HiddenField1");
Guid guid1;
Guid1.TryParse(HiddenField1.Value, out guid1);
To set the value, you can use
HiddenField1.Value = guid1.ToString();
Im working on the front end development for a website built in .net, This is the first time I've done this and I'm having trouble trying to alter a table.
The code which outputs my table is...
<asp:GridView ID="GV_Concepts" runat="server" AutoGenerateColumns="False" DataKeyNames="ConCatID"
BorderStyle="None" GridLines="None" ShowHeader="False" BorderWidth="0px" CssClass="DashBoard_Concepts">
<Columns>
<asp:TemplateField HeaderText="Catalog">
<ItemTemplate>
<asp:Label ID="LB_Cata" runat="server" Text='<%# Bind("ConCatalog") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="col-b" />
</asp:TemplateField>
<asp:TemplateField HeaderText=" Concept Version" ItemStyle-Width="" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<div class="conceptstd">
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" BorderStyle="None"
DataSource='<%# Bind("DS_Version")%>'>
<ItemTemplate>
<asp:HyperLink ID="HL_ConcLoc" runat="Server" CssClass="linkage" NavigateUrl='<%# Bind("FileName") %>'
Target="_blank" Text='<%# Bind("Ver") %>'></asp:HyperLink>
<asp:LinkButton ID="LB_remove" runat="server" CommandArgument='<%# Bind("ConceptID") %>'
OnClick="LB_removecon_Click" CssClass="link-btn">Remove</asp:LinkButton>
<asp:LinkButton ID="LB_sign" runat="server" CommandArgument='<%# Bind("ConceptID") %>'
OnClick="LB_signcon_Click" CssClass="sign-off-btn" Visible='<%# SignedCheck(DataBinder.Eval(Container.DataItem,"SignOff")) ?true:false %>'>Sign Off</asp:LinkButton>
<asp:Literal ID="Lit_SignedCon" Visible='<%# SignedCheck(DataBinder.Eval(Container.DataItem,"SignOff")) ?false:true %>'
runat="server"><b>Signed Off</b></asp:Literal>
</ItemTemplate>
</asp:DataList>
</div>
</ItemTemplate>
<HeaderStyle CssClass="col-c" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
Currently no concepts
</EmptyDataTemplate>
</asp:GridView>
The html equivalent of this is something similar too...
<table>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
However I need 5 columns not 2, I've tried reading up on the syntax but thought I'd ask here also, Thanks for any help.
I'm not exactly sure how your dataset is structured, but do you need that datalist in there or can you just put the controls in their own ItemTemplate in the GridView? This would give you 5 columns:
<asp:GridView ID="GV_Concepts" runat="server" AutoGenerateColumns="False" DataKeyNames="ConCatID"
BorderStyle="None" GridLines="None" ShowHeader="False" BorderWidth="0px" CssClass="DashBoard_Concepts">
<Columns>
<asp:TemplateField HeaderText="Catalog">
<ItemTemplate>
<asp:Label ID="LB_Cata" runat="server" Text='<%# Bind("ConCatalog") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HL_ConcLoc" runat="Server" CssClass="linkage" NavigateUrl='<%# Bind("FileName") %>'
Target="_blank" Text='<%# Bind("Ver") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LB_remove" runat="server" CommandArgument='<%# Bind("ConceptID") %>'
OnClick="LB_removecon_Click" CssClass="link-btn">Remove</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LB_sign" runat="server" CommandArgument='<%# Bind("ConceptID") %>'
OnClick="LB_signcon_Click" CssClass="sign-off-btn" Visible='<%# SignedCheck(DataBinder.Eval(Container.DataItem,"SignOff")) ?true:false %>'>Sign Off</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Literal ID="Lit_SignedCon" Visible='<%# SignedCheck(DataBinder.Eval(Container.DataItem,"SignOff")) ?false:true %>'
runat="server"><b>Signed Off</b></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
Currently no concepts
</EmptyDataTemplate>
</asp:GridView>
Of course, this ruins your binding to your datasource, but I'm not sure if I can accurately help you with fixing that :(
the number of column is automatically generated according to asp:templatefield
when you have two templatefield, the only two column will be generated, and the number
of rows depend on data.
so if you need 5 columns you have to put 5 templatefield inside the girdiview
Why do you "need" 5 columns? For layout? Perhaps a GridView isn't the right solution given what you're trying to accomplish. Could a Repeater that generates your content accomplish the same thing?