Click to See Complete Forum and Search --> : Using Gridview to display document information


mmcshane
09-30-2006, 04:11 PM
I am using VWD 2005 Express and I am attempting to use the Gridview to display document information and one of my columns in the gridview is a hyperlink to view the document. I have a webform that is getting all my data out of my SQL server database but when I click on my hyperlink to view the document it clears my table on the page and all thats left are my controls to get the document. What I want is for the Table to stay and to pop up a new window to view the document each time the user clicks one of my hyperlinks.

Any ideas on how I would go about popping up a window to view the document?

I have not found any methods of the Gridview to pop open a new window and I am realatively new to C#

Cipher
10-01-2006, 03:23 PM
To use popup you need JavaScript function, i didnt try this before but it should work fine with you.
In <head> section:

<script language="javascript">
function popupWin(data)
{
var newWin = window.open("popupPage.aspx?query=" + data + "", "window", "width=800, height=550, toolbar=0, menubar=0, status=0, location=0, scrollbars=1");
}
</script>

In you grid view link column, use html <href> tage:

<a href="javascript:popupWin('<%# eval("Data to be sent to the popup page from sql database") %>')"

then in the popupPage you can get the QueryString by using Request.QueryString["data"];

Hope this helps

sirpelidor
10-02-2006, 05:14 PM
this example (http://geekswithblogs.net/azamsharp/archive/2005/10/30/58551.aspx) shows to use hyperlink control in case you want to avoid javascript.

mmcshane
10-02-2006, 08:39 PM
Sipelidor - I looked at your code and it is very close to what I have. A couple questions I have. Do I need to create the smallwindow.aspx?

Also the SmallWindow.aspx?id={0}" should the {0} only contain the document name or the fullpath\document.txt ? Currently my database has the absolute path\docname.txt\

Thanks for the help in advance.

sirpelidor
10-03-2006, 02:20 AM
Do I need to create the smallwindow.aspx?
yes


Also the SmallWindow.aspx?id={0}" should the {0} only contain the document name or the fullpath\document.txt ? Currently my database has the absolute path\docname.txt\


you can call a new page, passing the id (primary key in your sql table) using http get method, then use sql to grab the data off the table based on the id using request.querystring["id"].

mmcshane
10-03-2006, 11:57 AM
When I debug through this on my Smallwindow.aspx I have the following code:

LoadPage()

path = Request.QueryString["id"]
Response.Redirect(path)


When I try this my path is getting set to what I have in my SQL table but when i click on the link it tries to go to the smallwindow.aspx but I am getting HTTP Error 404 Not Found Server Error. Do I need to make any changes in IIS in order to get this to work or am I using the wrong method to open the doc or both?

sirpelidor
10-03-2006, 12:02 PM
path = Request.QueryString["id"]
Response.Redirect(path)



what are you trying to accomplish by doing a redirect??

mmcshane
10-03-2006, 05:23 PM
I am just trying to get my document to view on the smallwindow.aspx page. Maybe Response.redirect is the wrong command to accomplish this. Which HTTP get method would I need to use to accomplish this?

sirpelidor
10-03-2006, 05:27 PM
I am just trying to get my document to view on the smallwindow.aspx page


are you saying you have many static documents you want to embedded into smallwindow.aspx,

or you just have 1 document to embedded into smallwindow.aspx?

what kind of document are we talking about here?

or you are trying to display "detail view" of a record based on the selected row from your gridview?

mmcshane
10-03-2006, 07:08 PM
I have many static documents that I just want to be able to view when I put in the selection criteria on my webform which brings back my Gridview which has the link to the path of the document I want to view. The following is a sample Gridview I have.

ReportName Date DocPath and DocName
DOCA 10-01-06 LinkUrl
DOCA 10-02-06 LinkUrl
DOCA 10-03-06 LinkUrl

Hopes this helps clarify what Im trying to do. Just want to view the docs when the LinkUrl is clicked in my Gridview.

sirpelidor
10-03-2006, 10:57 PM
sorry i got caught up with the choice of word: id.... (to me it is a key id field for a database) ok... I know what you want. You want user click on the grid, new window shows up with the doc.

you can make use of server.mapPath() (http://www.devx.com/vb2themax/Tip/18488)


in this case...as long as browser understands the document type, you won't even need the new file (smallwindow.aspx).

just go ahead and give the following path at your asp:hyperlinkfield control:


DataNavigateUrlFormatString=<%#Server.MapPath("/" + LinkURL + date + docName)%>


where i assume you got your date and docname LinkURL programmanically taken care of.

mmcshane
10-04-2006, 11:53 AM
Well I tried your suggestion and below is the exact line that I have for the Hyperlink in my Gridview:

<asp:HyperLinkField DataNavigateUrlFields="ReportDocPath" DataNavigateUrlFormatString=<%#Server.MapPath("/" + ReportDocPath)%> Text="Link" />

When I try to run this it gives me an error at runtime stating:

Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.HyperLinkField does not have a DataBinding event.

The ReportDocPath has the whole path and document name retrieved from the SQL Database.

sirpelidor
10-04-2006, 12:41 PM
ReportDocPath is a String type right?
try to bind it using Container.DataItem:


<asp:HyperLinkField DataNavigateUrlFields="ReportDocPath" DataNavigateUrlFormatString=<%#Server.MapPath("/") + ((String)Container.DataItem).ReportDocPath %> Text="Link" />

mmcshane
10-04-2006, 01:06 PM
Yes ReportDocPath is a String type. When I tried your section of code with the Container.DataItem I get the following error at Runtime:

Data Binding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.HyperLinkField does not have a DataBinding event.

sirpelidor
10-04-2006, 01:49 PM
where exactly does ReportDocPath come from? is it a string field sitting in your sql table? or is it a variable you compiled based on diff programming techique?

if it is a field off your sql table, you should be able to pull it off like you did with any other fields, (like that smallwindow.aspx=id{0} example).

once you got that string pulled, next thing you have to concern, is to match the correct path, that's where Server.MapPath(" --path goes here-- ") come into place.

mmcshane
10-04-2006, 03:17 PM
ReportDocPath is a string field from my sql table. Server.MapPath("ReportDocPath")

The Hyperlinks column on my Gridview is based on my ReportDocPath but its not actually part of what I have displayed in the Gridview.

I dont think it will allow me to embed this into my <asp hyperlink> tag. Keeps telling my that I there is no databinding event. I am just so confused.

Do I need to post back the page if I pass the URL to the smallwindow.aspx ?

sirpelidor
10-04-2006, 06:09 PM
I dont think it will allow me to embed this into my <asp hyperlink> tag. Keeps telling my that I there is no databinding event.

Do I need to post back the page if I pass the URL to the smallwindow.aspx ?

it is <asp:HyperLinkField> tag and it is capable of binding data because you blinded data last time through using smallwindow.aspx={0}....

lets get this clear first:
1)bind the data (string) from sql table (you did it through smallwindow.aspx={0}
2)you were unable to get to the correct path (use Server.MapPath("/")

maybe you are confuse because you try to do few things at the same time. lets just make sure you are binding the data correct (like you did before) first.

then when it is binded, go ahead and add "server.mappath" to whatever you've binded.....

sirpelidor
10-04-2006, 06:39 PM
I just made a link for you to review as an example, hope it'll help somewhat....

I have a page call: webform1.aspx
I have 3 text files, blah.txt, blah2.txt, blah3.txt

when you call webform1.aspx, it'll default display "no file embedded"....

when you append the "path" attribute, e.g: webform1.aspx?path=blah.txt
it will use response.redirect(filename) and point to the document....

here's the link (http://localhost/webapplication1/webform1.aspx) ...you can try all 3 text doc by calling the ?path=<filename goes here>

pay attention to your url what it changes to....

if this is what you want, you can combine this technique with smallwindow.aspx?path= ..... by clicking your datagrid...

mmcshane
10-05-2006, 12:47 AM
That is what I am trying to do but how is your path getting set? Is it through the NavigateURL property in the Gridview control?

mmcshane
10-05-2006, 10:03 AM
I have it working now. I made the Hyperlink field a template field and then everything started working. I appreciate all your help on this sirpelidor

samkris
10-06-2006, 02:15 AM
i stored some data in db from textbox using the following code

string strsql = "INSERT into Cue1 (Name,EmailId,TeleNo) VALUES ('" + NameTB.Text + "', '" + EmailTB.Text + "', '" + TeleTB.Text + "')";

now i want to store some data in the same db from "textarea"......so what should i add to this code........
i am using c# .net

thanking u.......
sam :)

sirpelidor
10-06-2006, 01:58 PM
you can treat strings from the textArea like textbox. Just make sure the field that store your text from your textArea is big enough in sql server or else your string will be truncated.

to avoid sql injection attack, i wouldn't recommand you build a insert string like the way you did, i suggest use the following:


string strsql = "INSERT into Cue1 (Name, EmailId,TeleNo, TextArea) VALUES (@Name, @EmailId, @TeleNo, @TextArea)"
SqlCommand cmd = new SqlCommand();
cmd.parameter.Add("@Name", NameTB.Text);
cmd.parameter.Add("@EmailId", EmailTB.Text);
cmd.parameter.Add("@TeleNo", TeleTB.Text);
cmd.parameter.Add("@TextArea", TextAreaTB.Text);
//the rest of sql code ....


p.s: this post does not appear to directly related to the orginal question, to make it easier for the reader to locate post, you should consider posting a new thread.

Otoniel
10-07-2008, 12:31 AM
Hi... I trying to do something like this (the post above) in a gridview to open a word document
DOCA 10-01-06 LinkUrl
DOCA 10-02-06 LinkUrl
DOCA 10-03-06 LinkUrl
The link is the path to the document....I would like to open microsoft word with the document represented in the hyperlink. I have set the templatefield with the fallowing code:
<asp:TemplateField HeaderText="Show document" SortExpression="document">
<ItemTemplate>
<asp:HyperLink id="lnkDocument" runat="server" text="Show document"
NavigateUrl='<%# Eval("document") %>'>
</asp:HyperLink >
</ItemTemplate>
</asp:TemplateField>

What more do I have to do?

Thank a lot....