Show images in GridView (loaded from URLs stored in SQL database) - c#

I'm very new to ASP.NET and C# and I would need a help here.
My task:
I would like to show images in Grid View from particular URLS. I'm keeping those URLs in my SQL database, but they are NOT complete. What I mean by this:
URL in my database looks like this: "somefolder/imagename.jpg" (without quotes). I would like to concatinate URL from database with my starting url, for example: "www.mypage.com/somefolder/imagename.jpg" and everything show in GridView.
How to start with this particular task? Any help really appriciated.

You can use the ImageField and use the format string attribute to format how the url is formed in relation to the value in the database. The {0} is placeholder for the value coming from the database and will be substituted when the gridview is generated
<asp:GridView runat="server" ID="xxx" AutoGenerateColumns="false">
<Columns>
<asp:ImageField DataImageUrlFormatString="http://mysite/{0}" DataImageUrlField="url">
</asp:ImageField>
</Columns>
</asp:GridView>

Related

Values GridView in cells

I am trying to get values of string that I keep inside GridView.
When I use regular BoundFields, things work get. I get whatever is needed with:
string my_value = myGrid.Rows [rowIndex].Cells[1].Text;
However, one grid needs to have hyperlinked entries in one of the columns. I did:
<asp:BoundField DataField="domainName"
HeaderText="Domain"
SortExpression="domainName"
HtmlEncode="false"
DataFormatString="<a href=DomainConfiguration.aspx?suffix={0}>{0}</a>"
My ASPX page shows the correctly formed hyperlinks. However, if I retrieve Text for the cell, it returns "<\a href=DomainConfiguration.aspx?suffix=example.com>example.com</a>" [without the two extra slashes], instead of "example.com"
What do I need to do to get GridView working the way I want? [Yes, I would rather use GridView and not another control.]
Thank you.
Instead of asp:BoundField to show hype link it is better use asp:HyperLinkField like:
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="domainName" HeaderText="domainName" DataNavigateUrlFormatString="DomainConfiguration.aspx?suffix={0}" DataTextField="domainName" />
</Columns>
and to get string value of that column you should try this:
string my_value= ((HyperLink)myGrid.Rows[rowIndex].Cells[1].Controls[0]).Text;
instead your asp:Bounfield use
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="editLink" runat="server" onclick='<%#Eval("EditLink") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

Pass data via hyperlink in gridview

I would like to pass data from a database column to another page via a hyperlink in the gridview, the data i want to pass is just a load of text. this is the code i have
<asp:HyperLinkField DataTextField="FullText"
DataTextFormatString="View Text" NavigateUrl="~/Abstract.aspx"
Target="_blank" />
This appears to work as far as opening up the correct page, but im unsure as to how to view the text on the new page, most help topics tell you how to pass the data to a new grid view, but i would just like to view the data within the page or a box or whatever will work.
Thanks
If my understanding is correct you just want to display the text passed as a query string to the new page, if this is correct, just read the query string and display it in a label.
In order for this to work, you need to specify the query string in the link inside your grid, your link would have to look something like;
~/Abstract.aspx?d=your+text
In your datagrid:
<asp:TemplateColumn>
<ItemTemplate>
<asp:HyperLink
NavigateUrl='<%# "~/Abstract.aspx?d=" + HttpUtility.UrlEncode(DataBinder.Eval(Container, "DataItem.Id").ToString()) %>'
runat="server"
Text="Product" />
</ItemTemplate>
</asp:TemplateColumn>
In the target page you would have something like:
string text = string.Empty;
if (this.Request.QueryString["d"] == null)
text = "Not found";
else
text = Server.UrlDecode(this.Request.QueryString["d"]);
// encode the text to avoid XSS (cross-site scripting)
this.myLabel.Text = Server.HtmlEncode(text);
On the grid
<asp:HyperLinkField DataTextField="FullText"
DataTextFormatString="View Text"
NavigateUrl='<%# "~/Abstract.aspx?ft=" + System.Web.HttpUtility.UrlEncode(Eval("FullText").ToString() %>'
Target="_blank" />
On the page you want to read the param you would have
string fullText = Request.QueryString["ft"];
if (string.IsNullOrEmpty(fullText))
{
fullText = HttpUtility.UrlDecode(fullText);
}
You might want to pass a key parameter through querystring to pull data you want to view.
If that is not what you want please make your question more explanatory.
You can pass ID in session or in query string (Abstract.aspx?textid=1). Read this id in page load event get data from database and display.

How to select a row from grid view and edit the details in a new webform using ASP.NET

I have displayed some fields in grid view, Now i want to select a row and i want to display all field from sql server in to a web form, after displaying the data in respective controls(textbox, dropdown), i want to update it.
How i can redirect the page to new webpage after selecting the respective row from grid view (Using primary key). and how the data will be pass on page load in respective fields so i could able to update it by using update query.
As i new to ASP. Net. Please update me in detail. If possible please with code.
It would go something like this.
You will make a 'list' webform with all records containing the gridview
Make another webform called details
The gridview on list page will contain a hyperlink button field something like this
<asp:HyperLinkField Text="View Details" DataNavigateUrlFields="YourId" DataNavigateUrlFormatString="details.aspx?id={0}" />
(Notice I am just redirecting to the details page passing the field id with querystring.
Now, on details page you would just fetch the querystring value & query your database with a datareader & populate your details page.
Something like on page load
if(Request.QueryString["id"])!=null)
{
// Fetch respective item from database here & populate form fields
// Open connection(); execute datareader;populate form fields
}
Easiest way to do this:
<asp:GridView ID="grd" runat="server" autogeneratedcolumn="false">
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<%#Eval("ID")%>
<ItemTemplate>
<asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%#Eval("ID")%>
<ItemTemplate>
<asp:TemplateField>
<asp:TemplateField HaderText="Edit">
<ItemTemplate>
<a href='EditData.aspx?ID=<%#Eval("ID")%>'>Edit</a>
</ItemTemplate>
<asp:TemplateField>
<asp:GridView>
EditData.aspx page will be used to edit the data. After receiving the Query-string Value,we can bind the data to the controls and then update the data after making changes.

What data viewing control will I need to use?

I am new to asp.net and even though I have been successfully developing some decent pages
that access backend SQL server using C# code behind, I have run into a problem that I need
some nudge in the right direction on.
I need to be able to display records from a SQL datasource (could be displayed using SQLDataSource method
OR using a dataReader OR using a gridview data fill) onto a page but I need other information displayed above
and below the returned record. Its best if I show an example:
<horizontal seperator line>
Customer ID
Customer Name
Customer Address
City, ST
COLUMNS FOR THE RETURNED RECORD DATA
THE ACTUAL RECORD DATA
OTHER DYNAMICALLY CALCULATED DATA BELOW THE RECORDS
<horizontal seperator line>
And this repeats for every record that is returned
For the life of me, I can't find determine what view, template, or even best data binder or
data source control it is that I need to use to make this happen.
Thanks for anyone's assistance in advance.
dell
If a datatable or gridview won't do, you can always use a Repeater or a Listview.
In the template, you can have all the controls you want in there.
You probably need a repeater similar to this:
<asp:Repeater runat="server" ID="repeater1">
<ItemTemplate>
<hr/>
<asp:Label runat="server" ID="labelCustomerId" />
<div style="padding-left:150px">
<asp:Label runat="server" ID="labelCustomerName" />
<asp:Label runat="server" ID="labelCustomerAddress" />
<asp:Label runat="server" ID="labelCityState" />
</div>
<asp:GridView runat="server" ID="gridViewRecordData">
<!--define columns & footer calculations here -->
</asp:GridView>
<hr/>
</ItemTemplate>
</asp:Repeater>
You can use the same ItemTemplate if you chose a ListView
Repeated is the best control. but in repeater you need to write code for Paging, Sorting...etc functiontly.
If you don't want to code for paging, sorting then go for GridView.
Try a ListView control. It lets you use templates to define how records, groups of records, etc. show up. For example you could define a template for your customer contact info, then a template for your records and other data.
You can accomplish this easily with a ListView. This article has a display of product information that's a lot like your layout -- horizontal lines between repeated sections.
I also like Scott Gu's tutorial, though the data layout (catalog images) is a little different from yours. Still, there's some good screenshots, and it also demos a Linq to SQL data source, which is a popular choice.

Getting Xml Columns from LinqDataSource to appear in a GridView

I have a LinqDataSource and a GridView displaying a table. Columns of type xml don't show up. I'd like them to show up something like they do in Sql Server query outputs, with a link to display the clickable xml, though there may be another approach i'm not considering (maybe a styled display of the xml data, etc.). Two things i'd like to know how to do
First, get the xml converted to a string, and display it in the table. Maybe the first 30 chars.
Finally, style the xml into something useful, like a clickable link to display the full xml, or a sub-table, or a styled string.
So the following works, and displays a nice table, with edit and delete links. But Xml fields are missing. How would you go about adding support for the Xml fields?
<form id="form1" runat="server">
<div>
<asp:LinqDataSource ID="OrdersDataSource"
OnContextCreating="LinqDataSource_ContextCreating"
runat="server" ContextTypeName="MyDbDataContext"
EnableUpdate="True" TableName="orders"
EnableDelete="true"
OrderBy="Status, UserId">
</asp:LinqDataSource>
<asp:GridView ID="OrdersGridView" DataSourceID="OrdersDataSource"
CssClass="gridview" PageSize="30" AutoGenerateDeleteButton="true"
AutoGenerateEditButton="true" AllowPaging="True" AllowSorting="True"
AlternatingRowStyle-BackColor="Beige" DataKeyNames="OrderId"
runat="server" Width="705px">
<Columns>
</Columns>
</asp:GridView>
</div>
</form>
Page_Load is empty at the moment.
The best approach would be to use GridView's RowDataBound event. This would look something like this:
protected void OrdersGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var dataItem = e.Row.DataItem;
...
}
}
I'm not sure what the type of dataItem is, but probably you can cast it to your order type (easiest to see in the debugger, just set a breakpoint in the RowDataBound event handler). You should be able to get your xml data from this object. When you have the xml data, you can convert it to a string and get the first 30 characters, for example.
The last thing to do is set this text in the correct cell. See the example on MSDN for this.

Categories