I have a class (POCO) that has an Image property. I use a List<> of this class as the datasource for a gridview control. Most of the properties are grabbed from the database. However, I am dynamically generating a barcode (System.Drawing.Image) in my code behind and populating the Image property in the POCO with the result prior to databinding. It is NOT possible for me to store the images on the database or the filesystem. How do I go about displaying the barcodes in the Gridview control? When I try, nothing shows and the images generate 404 errors. The code for the gridview is:
<asp:GridView runat="server" ID="gvInventoryDetail" CssClass="centerpage"
style="width: 700px;" ItemType="DisplayModels.InventoryDetailItem"
DataKeyNames="InventoryId" AutoGenerateColumns="False"
AllowPaging="True" PageSize="11" ShowHeaderWhenEmpty="True">
<Columns>
<asp:BoundField HeaderText="Barcode" DataField="Barcode"/>
<asp:TemplateField HeaderText="Image">
<ItemTemplate >
<asp:Image ID="BarcodeImage" runat="server"
Height="150px" Width="80px" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Comments" DataField="Comments"/>
<asp:BoundField HeaderText="Status" DataField="Status"/>
<asp:BoundField HeaderText="Broken?" DataField="IsBroken"/>
</Columns>
<SelectedRowStyle BackColor="#89d8f6" Font-Bold="True"/>
</asp:GridView>
Any help is MUCH appreciated!
I had a similar issue when generating pie charts using another 3rd party project.
What I did was the following:
Keep the Image Tag in your Grid.
Set that Image Tag to call an ASPX Page in your site, passing what ever params you need on the URL to generate the barcode image.
In that new ASPX do the following
response.clear()
response.binarywrite() of the image.
If you browse that URL you will see the image in the browser, which means, that URL would also show the image in your Image Tag in your grid.
Related
I use GridView to display data from DB. First I query data from DB to list<list<string>>, sort use linq, copy this array to DataTable and bind DataTable to GridView. But if in DataTable more then 1000 rows - it works very long or not works (error in browser).
How to fix this?
UPDATE i use rowspan in columns and create a delete buttons to all rows, and if i use paging wiil it works?
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnDataBinding="GridView1_DataBinding" OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting" >
<Columns>
<asp:BoundField DataField="Number" ItemStyle-Width="200px" > <ItemStyle Width="200px" > </ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Name" ItemStyle-Width="200px" > <ItemStyle Width="200px" > </ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Nameoid" ItemStyle-Width="200px" > <ItemStyle Width="200px" > </ItemStyle>
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="return DeleteConfirm();" OnClick="Button2_Click" />
<asp:HiddenField ID="HiddenField2" runat="server" Value='<%#Bind("Number") %>' />
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
It seems like you are doing lots of server side code in one round trip time. You should make it lesser so that page could show the result in grid view.
Here are few things on which you should work:
There is no need to store first in list then data table and then sorting. You can directly use the DATAVIEW class to sort the result from DB.
I think you should not display all 1000 record in grid view in a single page. You should use PAGING in grid view. This is also in built functionality of grid view by this you can show number of record in a one grid view page as you want..
For reference how to do this there is complete example. Follow the below link
http://www.dotnetgallery.com/kb/resource12-How-to-implement-paging-and-sorting-in-aspnet-Gridview-control.aspx
UPDATE:
If you want to delete a row in gridview with a link button on each row. Then follow the below article
enter link description here
You can put the UpdatePanel outside of gridview. First try to use the above code (from link), then try to add update panel. If you feel any difficulties ask here.
My suggestion would be to use Lazy loading pattern with your GridView .
You can refer to :
LazyLoadUpdatePanelusingTimerControlAJAX
or you can implement custom paging
Custompaging
You can use paging in your grid view having grid size of say 10,20,30 to accommodate large amount of data so that the page doesn't crash.
Try referring to this example
http://www.codeproject.com/Articles/106678/Display-Large-Amount-of-Data-in-GridView-with-Sear
I have some data in database which I am binding it to a bound column.
Data contains many html tags.
But I want to display it as TEXT only "NOT TO RENDER IT"
I am not sure because HTMLENCODE property doesn't work with boundcolumn.
Please help.
Also just to add I have multiple datagrids binding with bind() function and are using one event handler Itemdatabound.
<asp:boundcolumn datafield="content" readonly="True" headertext="Product ID ">
<headerstyle horizontalalign="Center" width="100px"></headerstyle>
<itemstyle horizontalalign="Left" width="100px"></itemstyle>
</asp:boundcolumn>
Prevent HTML encoding in auto-generated GridView columns
extract from possible solution from above link:
<asp:TemplateField HeaderText="myLink" SortExpression="myLink">
<ItemTemplate>
<asp:Literal ID="litHyperLink" runat="server" Text='<%# Bind("myLink", "{0}") %>' />
</ItemTemplate>
</asp:TemplateField>
Or bind your own columns, also shown in above link
Server.HtmlDecode()
This will allow you to print the tags without rendering them to html.
http://msdn.microsoft.com/en-us/library/hwzhtkke(v=vs.110).aspx
What I'm trying to do here is have a datagrid show a list of files on the server which the user can click on to download, or open. The list populates with the files just fine, I get a whole list of all the pdf files in the folder. When I go to click on them in the datagrid, the link is directed to the application root directory and not to the proper folders. How do I tell a datagrid hyperlink column where to go?
C# code behind:
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~") + "/Assets/reports/");
gridList.DataSource = dir.GetFiles("*.pdf");
gridList.DataBind();
asp
<asp:DataGrid runat="server" id="gridList" Font-Name="Verdana"
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="Name" DataTextField="Name"
HeaderText="File Name"/>
</Columns>
</asp:DataGrid>
I tried placing this in:
NavigateUrl = '<%# "~/Assets/reports/" + Eval("Name") %>'>
but I get a "databinding expressions are only supported on objects that have a databinding event." error
Here you go.
<asp:HyperLinkColumn DataNavigateUrlField="Name"
DataTextField="Name"
HeaderText="File Name"
DataNavigateUrlFormatString="~\examfilemanager\{0}" />
Found it here.
asp.net DataGrid file structure and linking back to it
I've created a wallboard application to display outstanding support calls for my ICT department. I've bound a number of gridviews to sqldatasources which execute a stored procedure. This is automated via asp.net ajax controls and partially refreshes the page/data every 30 seconds.
At the moment, when the number of records in the gridview goes over 9, the gridview automatically pages and shows the number of pages in the bottom right hand corner. The helpdesk can then VNC to the box which controls the screen and manually click to see what's on the next page.
What I am after is a way to programmatically (using the c# code-behind file) changing the current displayed page after 10/15 seconds or so, obviously if this is possible in the scope of the gridview. I trailed using javascript (and failed at jquery) of scrolling the gridview within a div, however this didn't work as expected.
Can anyone point me in the right example? I can't find anyone else querying this functionality via a quick Google. Any help/advice of how to fix this issue would be greatly appreciated!!
Gridview Code:
<asp:GridView ID="GridView1" ShowHeader="False" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
GridLines="None" CellPadding="2" Font-Size="35pt" AllowPaging="True" PageSize="9">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID">
<ItemStyle Width="15%" />
</asp:BoundField>
<asp:BoundField DataField="ASSIGNEES" HeaderText="ASSIGNEES" SortExpression="ASSIGNEES">
<ItemStyle Width="32%" Wrap="false"/>
</asp:BoundField>
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title">
<ItemStyle Width="53%" Wrap="false"/>
</asp:BoundField>
</Columns>
</asp:GridView>
SqlDataSource Code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:FPConnectionString %>" SelectCommand="HDMonitoringOutstandingToday" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
Printscreen of wallboard:
You can try something like this in a timer.
if(GridView1.PageIndex == GridView1.PageCount)
{
GridView1.PageIndex = 0;
}
else
{
GridView.PageIndex = GridView.PageIndex + 1;
}
I cant remember if you would need to add one to the PageIndex or not.
But anyway, the properties you need to work with are PageIndex and PageCount.
GridView.PageIndex
You can change pages by setting the PageIndex, how you do it is up to you see here for some examples: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.pageindex.aspx
I'm trying to delete a record from Gridview1 at the same time delete the corresponding image file off the server in a single click. (Each row in Gridview1 has an associated image file on the server.)
To delete the record, I'm using asp:CommandField showDeleteButton="true" along with a sqlDataSource's DELETE statement.
During that process, I'm also using GridView1's "onRowDeleting" event to delete the corresponding image file off the server.
Here's what it does, with the code I have below:
The record indeed gets deleted,
The file on the server does not,
There are NO errors thrown (since I guess that it's not finding the file, and this is the expected behavior).
Also consider:
I've already set up and tested a much simpler "see if files can actually be deleted off the
server" test before I started development on the Gridview. Since our files are on a hosting
company server, I wanted to test for any permissions issues.
Whereby:
Enter the fileName and extension into a text box ("myImage.jpg")
Click the button that uses the File.Delete method
WhaaLa - the file is gone from the server.
However, I can't get the file to go away with my new setup.
Here's the code:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="libraryID"
DataSourceID="SqlDataSource1" Width="800px" onrowdeleting="deleteImageFromServer" CssClass="gridViewSmallText"
OnDataBound="rowCount">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<%--A link that goes to the uploadPage to upload a new version of the image--%>
<asp:HyperLinkField runat="server" HeaderText="SKU (Click to Update)" DataTextField="sku" DataNavigateUrlFields="sku" SortExpression="sku" DataNavigateUrlFormatString="graphicUpload.aspx?sku={0}" >
</asp:HyperLinkField>
<asp:TemplateField HeaderText="Image" SortExpression="imagePath">
<ItemTemplate>
<%--Pull the imagePath column from the database here-it also includes the image file --%>
<asp:Image ID="merchImage" runat="server" Height="100px" ImageUrl='<%# "http://www.ourcompanysite.net/" + DataBinder.Eval(Container.DataItem, "imagePath") %>' /><br />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="View Full Size">
<ItemTemplate>
<%--A link to view the image in it's full size in a new browser window--%>
<asp:HyperLink ID="fullSizeHyperlink" runat="server" NavigateUrl='<%# "http://www.leadingjewelersguild.net/" + DataBinder.Eval(Container.DataItem, "imagePath") %>' Text="View" Target="_blank" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DateUpdated" </asp:BoundField>
<%---some date stuff here--%>
<asp:BoundField DataField="DateCreated" HeaderText="First Uploaded" SortExpression="DateCreated" >
</asp:BoundField>
</Columns>
</asp:GridView>
Code behind:
protected void deleteImageFromServer(object sender, GridViewDeleteEventArgs e)
{
// I read from GridViewGuy.com that you're supposed to reference the row item via e.Values
string imageToDelete = e.Values["sku"] + ".jpg";
//I pulled the value of "imageToDelete" into a lable just to see what I was getting
//during the "onRowDeleting" and it reported back .jpg instead of the full file name
//myImage.jpg, so I guess this is the crux of my problem.
string image = Server.MapPath("/images/graphicsLib/" + imageToDelete);
string image = Server.MapPath("e:\\sites\\oursite\\files\\images\\graphicsLib\\" + imageToDelete);
if (File.Exists(image))
{
File.Delete(image);
}
//at this point the record from GridView1 is gone, but the file on server remains.
}
I think part of your problem may be that in order for e.Values["sku"] to contain a value, it has to be bound first. I don't think HyperlinkField binds its data (I could be wrong on that though so don't quote me)
First try adding a <asp:BoundField DataField="sku" Visible="false" /> in your column list. or change the HyperLinkField to TemplateField and explicitly bind the sku '<%#Bind("sku")%>'
If that doesn't work you can try changing DataKeyNames="libraryID" to DataKeyNames="libraryID,sku". You should be able to get the value out of e.Keys["sku"], or e.Values["sku"].
After you started development using the GridView control, have you verified that the deleteImageFromServer method is being called by setting a breakpoint in there?
And that imageToDelete is being set correctly?
After breaking into it, look at the 'watch' values for 'sender' and 'e'. Drill down into those to see what kind of objects they are and what values they hold.
Sometimes you have to traverse the object hierarchy in the GridVied control to get to the right object.