How to display image in gridview from server floder? - c#

I get error following code
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="img" runat="server" ImageUrl="~/Attachment/<%#Eval("Image") %>" />
</ItemTemplate>
</asp:TemplateField>
error
Parser Error Message: The server tag is not well formed.

First Try this:
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="img" runat="server" ImageUrl="<%# Page.ResolveClientUrl(String.Format("~/Attachment/{0}",Eval("Image"))) %>" />
</ItemTemplate>
</asp:TemplateField>
There is another option to do that in the server not in the client. its is useful if you need to set the image url in run time.
<asp:GridView runat="server" ID="gvActivities" AllowSorting="true" AllowPaging="true"
PageSize="25" AutoGenerateColumns="false" Width="100%" OnSorting="gvActivities_Sorting"
OnRowDataBound="gvActivities_RowDataBound">
<Columns>
<asp:TemplateField HeaderText='Image' HeaderStyle-Width="4%"
SortExpression="ActivityType">
<ItemTemplate>
<asp:Image ID="ImageType" runat="server" AlternateText='<%# Eval("Type") %>' />
</ItemTemplate>
</asp:TemplateField>
Has you can see, i am using the OnRowDataBound to set the image url.
I am not setting the ImageURL in the client.
I am checking if the Row type is a Data Row.
Then i am creating an image and putting the image from the grin into it. see that i am using the FindControl method. the "ImageType" is the id of the image in the grid.
then i am setting the imageURL Property
protected void gvActivities_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Image img = (Image)e.Row.Cells[0].FindControl("ImageType");
img.ImageUrl = Page.ResolveClientUrl("Image URL path);
img.AlternateText = "Text";
img.ToolTip = "Tooltip";
}
}

Related

How to put each cell value for each row from GridView in variables

I tried some ways in stackoverflow solutions. But Those are not implemented well by me. Most of the time I got null/empty for the variables and Exception like "Object reference not set to an instance of an object."
My GridView:
<asp:GridView runat="server" ID="TestReportGridView" AutoGenerateColumns="false" Width="370px">
<Columns>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<%#Eval("TestId") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sr">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Test">
<ItemTemplate>
<%#Eval("TestName") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fee">
<ItemTemplate>
<%#Eval("Fee") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle HorizontalAlign="Left" />
</asp:GridView>
I want to put cell text in 4 different variables for each row when I click in Save button:
protected void SaveButton_Click(object sender, EventArgs e)
{
string patient = PatientNameTextBox.Text;
string birthDate = BirthDateTextBox.Text;
string mobile = MobileNoTextBox.Text;
int rows = TestReportGridView.Rows.Count;
foreach (GridViewRow row in TestReportGridView.Rows)
{
Label test = (Label)row.FindControl("Test");
string testName = test.Text;
//Label lblQuantity = (Label)row.FindControl("Quantity");
//string Quantity = lblQuantity.Text;
}
}
Image of the UI
I added a label in ItemTemplate. does it right way to add Label
<asp:TemplateField HeaderText="Test">
<ItemTemplate>
<asp:Label ID="Test" runat="server" ><%#Eval("TestName") %></asp:Label>
</ItemTemplate>
</asp:TemplateField>
You need to add Label control in your ItemTemplate. An example below:
<ItemTemplate>
<asp:Label ID="Test" runat="server" Text='<%# Eval("TestName") %>' />
</ItemTemplate>
Adding <%#Eval("TestId") %> directly inside ItemTemplate will not automatically add any control to search from code behind.

How to get the Id from Gridview of Chechbox.checked?

I have GridView and a button as follows. Then i am binding the gridview with data from my database. GridView has two hiddenfield for Id and ClassIndex.
when i selecting a checkbox and click button, i want to get the corresponding Id and FileName.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="check" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdfId" runat ="server" Value='<%#Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdfClssIndex" runat ="server" Value='<%#Eval("ClassIndex") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblFileName" runat ="server" Text='<%#Eval("FileName") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and Button Like
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Send Request" />
the code behind button is
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
var check = row.FindControl("check") as CheckBox;
if (check.Checked)
{
int Id = Convert.ToInt32(row.Cells[1].Text);
//some logic follws here
}
}
}
but i am getting an error like
Input string was not in a correct format.
What is the error and how to solve it?
Your looping correct.
But you forgot to notice one thing here, when you wanted to access CheckBox you did a FindControl on row. Which means you are trying to find some control in that row.
Then why are you accessing HiddenField control inside row with row.Cell[1].Text?
Try to find that also.
int Id = Convert.ToInt32(((HiddenField)row.FindControl("hdfId")).Value);

FindControl in gridview returns null

I am currently in a dilemma with my gridview not returning a label, which is within a detailsview...
My C# code is:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
// get pet number for when removing a pet from reservation
int numberSelected = -1;
String numbertxt = "-1";
GridView gv1 = (GridView)sender;
GridViewRow rvRow = gv1.Rows[gv1.SelectedRow.RowIndex];
Label numberLbl = (Label)rvRow.Cells[0].FindControl("lblNumber");
// find selected index, and get number in column 0
// label within GridView1 within dvReservation DetailsView
numbertxt = numberLbl.Text;
...
Gridview:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="dsObjGet"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField InsertVisible="False" ShowHeader="False">
<AlternatingItemTemplate>
<asp:Label ID="lblNumber" runat="server"
Text='<%# Eval("NUMBER") %>' Visible="False"></asp:Label>
</AlternatingItemTemplate>
<ItemTemplate>
<asp:Label ID="lblNumber" runat="server"
Text='<%# Eval("NUMBER") %>' Visible="False"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<AlternatingItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("NAME") %>'>
</asp:Label>
</AlternatingItemTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("NAME") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField SelectText="Remove" ShowSelectButton="True"
CausesValidation="False">
<ControlStyle CssClass="link" />
</asp:CommandField>
</Columns>
</asp:GridView>
When I breakpoint
Label numberLbl = (Label)rvRow.Cells[0].FindControl("lblNumber");
the label comes out as null (numberLbl)...
The message returned from the exception is:
"Object reference not set to an instance of an object"
EDIT:
This seems to be resolved if I generate lblNumber in an external gridview (on the page) with Eval("NUMBER"), though I don't see why it doesn't work in the current GridView I was trying to work with, given that GridView1 is within a DetailsView.
You should not use the Cell Collection when using FindControl. Just use this
GridView gv1 = (GridView)sender;
GridViewRow rvRow = gv1.SelectedRow;
Label numberLbl = (Label)rvRow.FindControl("lblNumber");

Binding hyperlink in gridview using c#

How to Bind hyperlink in GridView using C#?
ASP.NET code:
<asp:GridView ID="GridView1" runat="server" onselectedindexchanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HyperLink id="HyperLink2" NavigateUrl="" Text="<%#Eval("pdfname") %>" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Just use eval:
<asp:HyperLink id="HyperLink2" NavigateUrl='<%#Eval("YourUrl") %>' Text='<%#Eval("pdfname") %>' runat="server"/>
In case your Url is in DataBound item property called "YourUrl"
If you need to construct it dynamically you can use method:
<asp:HyperLink id="HyperLink2" NavigateUrl='<%# CreatePageUrl(Container.DataItem)%>' Text='<%#Eval("pdfname") %>' runat="server"/>
Define CreatePageUrl method in your code behind.
Use OnRowDataBound Event of grid view find the hyperlink control then bind the url which u want. Example.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hl= (HyperLink)e.Row.findControl("HyperLink2");
hl.NavigateUrl = "Your Url";
}
}

Select any particular data from the gridview and dislay it in the label

hai pupils
I am doing a project on online education. In my project i display the trainees/trainers in in a gridview. If i select any data from the gridview it has to be dispalyed in the label kept below. How can i do this?
Thanks in advance for those who answers.
Use asp:LinkButton to show data in each column. Set commandArgument as the data. set same comandname and handle rowcommand in gridview rowcommand event.
try the following:
in aspx page:
<asp:GridView ID="GridView1" OnRowCommand="GridView1_RowCommand" runat="server">
<Columns>
<asp:TemplateField HeaderText="CategoryID">
<ItemTemplate>
<asp:LinkButton ID="lnkID" runat="server" CommandName="sel" CommandArgument='<%# DataBinder.Eval(Container,"DataItem.CategoryID") %>'
Text='<%# DataBinder.Eval(Container,"DataItem.CategoryID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CategoryName">
<ItemTemplate>
<asp:LinkButton ID="lnkName" runat="server" CommandName="sel" CommandArgument='<%# DataBinder.Eval(Container,"DataItem.CategoryName") %>'
Text='<%# DataBinder.Eval(Container,"DataItem.CategoryName") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblCat" runat="server"></asp:Label>
in aspx.cs page:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "sel")
{
lblCat.Text = e.CommandArgument;
}
}

Categories