Tuesday, March 13, 2012
the http reponse to file? ASP.NET 2.0
It is a ASP.NET aspx file created partly from GET parameters, but most often
using the ASP.NET event model (i.e. clicking buttons etc).
One of the buttons is called "Generate a pdf document of the current page"
and it clicking it should create a pdf document on the server side and
returning the pdf to the user.
To my aid I have a server side component that can convert a file or the html
content of a URL to a pdf document. So if I somehow can generate the
information of the current aspx page to file, I can convert it to pdf and
change the Response.ContentType to "application/pdf".
But the question is how to generate the file that I want to convert. If I
send the current URL to the pdf component, it will point to the page and how
it looks when no interaction has been done. What I really want is the page
after all clicks, i.e. how it looks "right now".
Any ideas? Thank you,
/FredrikThus wrote Fredrik,
> I want to generate a pdf file of "the current page".
> It is a ASP.NET aspx file created partly from GET parameters, but most
> often using the ASP.NET event model (i.e. clicking buttons etc).
> One of the buttons is called "Generate a pdf document of the current
> page" and it clicking it should create a pdf document on the server
> side and returning the pdf to the user.
> To my aid I have a server side component that can convert a file or
> the html content of a URL to a pdf document. So if I somehow can
> generate the information of the current aspx page to file, I can
> convert it to pdf and change the Response.ContentType to
> "application/pdf".
> But the question is how to generate the file that I want to convert.
> If I send the current URL to the pdf component, it will point to the
> page and how it looks when no interaction has been done. What I really
> want is the page after all clicks, i.e. how it looks "right now".
> Any ideas? Thank you,
HttpResponse allows you set another stream as a filter using HttpResponse.Fi
lter.
You could use that to write the response to a temporary file, create the
PDF, and write the PDF back to the actual response stream.
Cheers,
--
Joerg Jooss
news-reply@.joergjooss.de
Very interesting!
It seems the file isn't created until the Response ends. This seems natural
to me when thinking about it.
But when the response has ended, I per definition have lost my chance to
alter the response (remeber, I wamt to send the pdf instead). I guess I will
have to trigger another request somehow, and in the new Response show the pd
f.
Havn't still figured out the last details yet. Thanks, anyhow, for your kind
help.
Regards
/Fredrik
"Joerg Jooss" wrote:
> Thus wrote Fredrik,
>
> HttpResponse allows you set another stream as a filter using HttpResponse.
Filter.
> You could use that to write the response to a temporary file, create the
> PDF, and write the PDF back to the actual response stream.
> Cheers,
> --
> Joerg Jooss
> news-reply@.joergjooss.de
>
>
Thus wrote Fredrik,
> Very interesting!
> It seems the file isn't created until the Response ends. This seems
> natural to me when thinking about it.
Are you sure you just don't need a Response.Flush()?
Cheers,
--
Joerg Jooss
news-reply@.joergjooss.de
the http reponse to file? ASP.NET 2.0
It is a ASP.NET aspx file created partly from GET parameters, but most often
using the ASP.NET event model (i.e. clicking buttons etc).
One of the buttons is called "Generate a pdf document of the current page"
and it clicking it should create a pdf document on the server side and
returning the pdf to the user.
To my aid I have a server side component that can convert a file or the html
content of a URL to a pdf document. So if I somehow can generate the
information of the current aspx page to file, I can convert it to pdf and
change the Response.ContentType to "application/pdf".
But the question is how to generate the file that I want to convert. If I
send the current URL to the pdf component, it will point to the page and how
it looks when no interaction has been done. What I really want is the page
after all clicks, i.e. how it looks "right now".
Any ideas? Thank you,
--
/FredrikThus wrote Fredrik,
> I want to generate a pdf file of "the current page".
> It is a ASP.NET aspx file created partly from GET parameters, but most
> often using the ASP.NET event model (i.e. clicking buttons etc).
> One of the buttons is called "Generate a pdf document of the current
> page" and it clicking it should create a pdf document on the server
> side and returning the pdf to the user.
> To my aid I have a server side component that can convert a file or
> the html content of a URL to a pdf document. So if I somehow can
> generate the information of the current aspx page to file, I can
> convert it to pdf and change the Response.ContentType to
> "application/pdf".
> But the question is how to generate the file that I want to convert.
> If I send the current URL to the pdf component, it will point to the
> page and how it looks when no interaction has been done. What I really
> want is the page after all clicks, i.e. how it looks "right now".
> Any ideas? Thank you,
HttpResponse allows you set another stream as a filter using HttpResponse.Filter.
You could use that to write the response to a temporary file, create the
PDF, and write the PDF back to the actual response stream.
Cheers,
--
Joerg Jooss
news-reply@.joergjooss.de
Very interesting!
It seems the file isn't created until the Response ends. This seems natural
to me when thinking about it.
But when the response has ended, I per definition have lost my chance to
alter the response (remeber, I wamt to send the pdf instead). I guess I will
have to trigger another request somehow, and in the new Response show the pdf.
Havn't still figured out the last details yet. Thanks, anyhow, for your kind
help.
Regards
--
/Fredrik
"Joerg Jooss" wrote:
> Thus wrote Fredrik,
> > I want to generate a pdf file of "the current page".
> > It is a ASP.NET aspx file created partly from GET parameters, but most
> > often using the ASP.NET event model (i.e. clicking buttons etc).
> > One of the buttons is called "Generate a pdf document of the current
> > page" and it clicking it should create a pdf document on the server
> > side and returning the pdf to the user.
> > To my aid I have a server side component that can convert a file or
> > the html content of a URL to a pdf document. So if I somehow can
> > generate the information of the current aspx page to file, I can
> > convert it to pdf and change the Response.ContentType to
> > "application/pdf".
> > But the question is how to generate the file that I want to convert.
> > If I send the current URL to the pdf component, it will point to the
> > page and how it looks when no interaction has been done. What I really
> > want is the page after all clicks, i.e. how it looks "right now".
> > Any ideas? Thank you,
> HttpResponse allows you set another stream as a filter using HttpResponse.Filter.
> You could use that to write the response to a temporary file, create the
> PDF, and write the PDF back to the actual response stream.
> Cheers,
> --
> Joerg Jooss
> news-reply@.joergjooss.de
>
Thus wrote Fredrik,
> Very interesting!
> It seems the file isn't created until the Response ends. This seems
> natural to me when thinking about it.
Are you sure you just don't need a Response.Flush()?
Cheers,
--
Joerg Jooss
news-reply@.joergjooss.de
The LINQ "where" clause
Hi guys,
I'm trying to write a LINQ query in which the where clause can accept 6 parameters. The problem is that all of them are optional parameters and need to be validated as not Null or Empty in order to be used. Normally when I used the SqlCommand I would build a string for the query via conditional statements, like:
if (!String.IsNullOrEmpty(TextBox1.Text)) { SqlString +="..." } How do I do the equivalent to that in LINQ?
Thanks in advance!
not sure if i understand what you mean. do you mean validate a statement as a parameter? In that case you can write like this with the ? syntax:
object o;
o = (cond1 = cond2 ? truevalue : falsevalue);
MyFunction( String.IsNullOrEmpty(TextBox1.Text) ? truevalue : falsevalue)
Well, this is how I did it the non-LINQ way:
string SqlString =string.Empty;if (!String.IsNullOrEmpty(TextBox1.Text)) { SqlString +="...";}if (!String.IsNullOrEmpty(TextBox2.Text)) { SqlString +="...";}SqlCommand MyCommand =new SqlCommand(SqlString, MyConnection);
In the code above I created the SqlString variable. After that I would check if the Text property is Null or Empty for the TextBox1 & TextBox2 andonly append to the SqlString if they are not. Then I run the newly built Sql query. I need to do the equivalent in LINQ and I just don't know how.
The magical button? Declarative parameters tutorial
Hey folks,
Long time follower, first time poster.
I'm going through the"Working with Data in ASP.NET 2.0" tutorials, and this one bit has me mystified.
Page 4-6 (or about half way down, section "Setting the Parameter Value to the Property Value of a Web Control"
http://www.asp.net/learn/dataaccess/tutorial05cs.aspx?tabid=63
I've been through all the steps, and they all work - I just don't understand why. The mystery lies in the "Show suppliers" button. How does
my datagrid know I have clicked this button? How can it figure out, by this button click, to grab what's in the textfield, and send to the datagrid?
I haven't specified any code on the "on click" event for the button, and the markup source doesn't seem to reveal any kind of relationship between the textfield and the button.
If I put more buttons on the form, they all seem to behave in the same way - populating the datagrid.
Magic?
I believe you will find the answer to that question in this part of Scott's tutorial:
When visiting the page for the first time the CountryName TextBox is empty. The ObjectDataSource's Select method is still invoked by the GridView, but a value of null is passed into the GetSuppliersByCountry(country) method. The TableAdapter converts the null into a database NULL value (DBNull.Value), but the query used by the GetSuppliersByCountry(country) method is written such that it doesn't return any values when a NULL value is specified for the @.CategoryID parameter. In short, no suppliers are returned.
Once the visitor enters in a country, however, and clicks the Show Suppliers button to cause a postback, the ObjectDataSource's Select method is requeried, passing in the TextBox control's Text value as the country parameter.
tmorton:
Once the visitor enters in a country, however, and clicks the Show Suppliers button to cause a postback, the ObjectDataSource's Select method is requeried, passing in the TextBox control's Text value as the country parameter.
Thanks for the reply. I'm figuring the operative here is "postback" - the button causes a postback, but I still can't see the relationship between the button's postback, the textfield, and the datagrid.
My appoligies for newbie-ness, but I'm really trying to understand the workings of ASP.NET, coming from a Java Swing world, everything is new to me!
mortdk:
Thanks for the reply. I'm figuring the operative here is "postback" - the button causes a postback, but I still can't see the relationship between the button's postback, the textfield, and the datagrid.
Hi there,
The relation is set when you configure the newly created ObjectDataSource, see in Figure 8, where the parameter source is set to a "control" and then you specify that the ID of the control to use is "CountryName". CountryName is the textbox and effectivly what you're doing is to tell the method the ObjectDataSource is calling (GetSuppliersByCountry()) to use the value from the "CountryName" text box as the value to pass along to the method.
You can physically see this in the markup (in the code view of your .aspx page) as shown in the code between Figure 8 and 9 of the article, in the ControlParameters tag inside the SelectParameters you can see that the ControlID is set to the ID of the text box and the type and property are set to string/text.
Hopefully this makes sense
mortdk:
Thanks for the reply. I'm figuring the operative here is "postback" - the button causes a postback, but I still can't see the relationship between the button's postback, the textfield, and the datagrid.
Well, I doubt that I can do a better job at explaining things than Scott Mitchell can, but I'll give it a go. :-) For reference, here is the pertinent part of the DelcarativeParameters.aspx page:
<h3>View Suppliers By Country</h3>
<p>
Enter a country:
<asp:TextBox ID="CountryName" runat="server"></asp:TextBox> <asp:Button ID="Button1"
runat="server" Text="Show Suppliers" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="SupplierID"
DataSourceID="ObjectDataSource2" EnableViewState="False">
<Columns>
<asp:BoundField DataField="CompanyName" HeaderText="Company" SortExpression="CompanyName" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetSuppliersByCountry" TypeName="SuppliersBLL" UpdateMethod="UpdateSupplierAddress">
<UpdateParameters>
<asp:Parameter Name="supplierID" Type="Int32" />
<asp:Parameter Name="address" Type="String" />
<asp:Parameter Name="city" Type="String" />
<asp:Parameter Name="country" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="CountryName" Name="country" PropertyName="Text"
Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
On that page you have 4 server controls coming into play for the "View Suppliers by Country" functionality:
Textbox (ID=CountryName)
Button (ID=Button1)
GridView (ID=GridView1)
ObjectDataSource (ID=ObjectDataSource2)
As you can see, GridView1's DataSource is ObjectDataSource2. That means that GridView1's data comes from the data supplied by ObjectDataSouce2's SelectMethod, which is method GetSuppliersByCountry. Any parameters needed by that method are passed into it from the ObjectDataSource's SelectParameters. In this case, there is one SelectParameter, and a ControlParameter is being used. This means that the value stored in a server control is used as the parameter value. The server control whose value is being passed is the CountryName Textbox.
So, the first time this page is accessed, the CountryName Textbox does not hold any value. The GridView asks the ObjectDataSource to load it up with data, the ObjectDataSource uses the CountryName value as a parameter to the GetSuppliersByCountry method, and since the CountryName is empty, the method returns no results. As a result, the GridView is empty.
Now, when the site visitor keys a value into the CountryName Textbox, and presses the button, the button causes a postback since it is a server control. So, once again, the GridView asks the ObjectDataSource to load it up with data, the ObjectDataSource uses the CountryName value as a parameter to the GetSuppliersByCountry method. This time, the CountryName is not empty, so the method returns results, and the GridView is populated.
Does this help at all?
Thanks for the excelent explanations. I understand the bindingbetween the textfield and the datagrid, this seems logical - take theCountryName.Text and use this as a parameter to look for data. Myproblem is understanding how the button1 can know anything about anytextfields or datagrids, when I haven't told it to do anything. All Idid was drop a button on the form, and it works "out of the box".
Now, when the site visitor keys a value into the CountryName Textbox,and presses the button, the button causes a postback since it is aserver control. So, once again, the GridView asks the ObjectDataSourceto load it up with data,
After fiddling a little with the tutorial, however odd I find the behavior, I think I might understand... please, correct me if I'm wrong:
The button only causes a postback. It's the postback that actually triggers the "reload" of the datasouce? I've tried with a LinkButton, a CheckBox (set to postback), and they all trigger a reload. Maybe it's just the tutorial's simplicity that caught me off guard. What if I have 3 other controls on the form that also need to perform postbacks, but must not change the contents of the mentioned gridview?
tmorton:
Now, when the site visitor keys a value into the CountryName Textbox, and presses the button, the button causes a postback since it is a server control. So, once again, the GridView asks the ObjectDataSource to load it up with data, the ObjectDataSource uses the CountryName value as a parameter to the GetSuppliersByCountry method.
Looks like I mucked up the quotes - sorry, hope you can decipher.
mortdk:
After fiddling a little with the tutorial, however odd I find the behavior, I think I might understand... please, correct me if I'm wrong:
The button only causes a postback. It's the postback that actually triggers the "reload" of the datasouce? I've tried with a LinkButton, a CheckBox (set to postback), and they all trigger a reload. Maybe it's just the tutorial's simplicity that caught me off guard. What if I have 3 other controls on the form that also need to perform postbacks, but must not change the contents of the mentioned gridview?
Sounds like you've got the hang of how the postback gets triggered and the data getting loaded.
In regards to having other controls that also trigger postbacks on a page, keeping the value of a control in ASP.NET is done with something called viewstate. If you enable viewstate on a control the values will get stored in a hidden field and then used to re-populate the control after the postback. I'm no expert on the use of viewstate but I guess in the example case, you could enable viewstate for the country textbox so that it keeps the value you input and therefore use that value as a parameter again after the postback. Or you could just enable viewstate on the gridview, I think both ways has positives and negatives.
More info on viewstate:
http://aspalliance.com/909
http://www.extremeexperts.com/Net/Articles/ViewState.aspx