I want to create an asp.net web forms website that has a products page. I want to create a products.aspx page that has a specific layout and there I want to demonstrate the products. So in the url I will have something like www.mysite.gr/products.aspx?productid=1 In other words I want to display different content according to the id from the query string parameter. Please can you propose me some ways to do this and also if you know provide some links that I can study?
Depending on how your site is laid out and how your project is built, there are various ways to approach this. For example, you can get the value of the QueryString like this:
string prodID = Request.QueryString["productid"];
if (prodID != null)
{
//perform database request passing the productid
selectedProduct = GetProductData(prodID);
}
Then, as a simple example, you can add the relevant details. For example, lets say you've got a product class from your GetProdutData() method. You can then fill out the elements on your page with the relevant data.
titleLabel.Text = selectedProduct.Title;
descriptionLabel.Text = selectedProduct.Decription;
image.ImageUrl = selectedProduct.ImageURL;
This is just one approach and there are various others, such as using the MVC pattern.
Related
I'm currently developing an app that generates a URL for a certain number of Subscribers. That has the same content. For example a web page with an image in it.
The URL pattern can be like this http://host:XXXX/foo/randomNumbers
I'm planning to have 7 random numbers. That won't repeat for each subscribers.
I'm currently using Java with SpringMVC for this one. Hope you can help me I'm currently stuck with generating the URLs with the same content.
Thanks! :)
Generate URL any way you like but you have some static aspects:
e.g.
String staticPart = "http://www.yoursite.com/foo/"
//randomly generate the integer number and store in variable (e.g. ranNum)
String finalURL = staticPart + Integer.toString(ranNum)
That way your user gets randomly generated url.
Below is method for accessing a randomly generated URL:
#RequestMapping("/foo/{id}")
public ModelAndView returnView(#PathVariable int id)
I would on top of this store the randomly generated URL for the user and then in the returnView method check if its legitimate.
Content doesn't change that way only url and is made unique to a user.
Is this going to meet your requirement?
I am using asp.net mvc to build a page where i have to show alot of users in a table. I would like to make a paged view with an url like:
mypage.com/Customer/{pageno}/{sorttype}/{somethingelse}
The urls would be located different places on a page. E.g. the "next page" button would have the same url, with just {pageno} increased, and the table headers would have {sorttype} different.
Is there a nice way to do this in razor, or will i just have to get the raw url, and parse it myself?
Thanks.
You should leave URL generation to ASP.NET MVC routing. In the global.asax you should configure route for your Customer page correctly so it can take pageno, sorttype and somethingelse parameters.
Then in Razor you should simply use Url.Action (or Html.ActionLink) method, so the URL would be generated for you. You can use following overload in your case:
http://msdn.microsoft.com/en-us/library/dd470197(v=vs.118).aspx
If you find it too complex to write stuff like #Url.Action("Index", "Customer", new { pageno = 1, sorttype = "type", somethingelse = "test"), then you can add another extension method like Url.MyAction which will take 3 parameters (int pageno, string sorttype, string somethingelse) and call Url.Action internally. However I wouldn't suggest doing that because the readability of your code will surprisingly decrease. Everybody know Url.Action method, while your new Url.MyAction would be something new to other developers reading your code.
Again, the key is correct routing, so Url.Action() will be rendered to "/Customer/1/type/test". You can find more information on the link below: http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs
i wanted to add to my website the option of seeing the usernames of the people that are logged in my website as a list.
i did it using the Application state so when a person loges in the application variable adds it to itself.
but when a person loges out of the website i need to remove his username from the list and i'm having trouble with this... any code suggestions?
in the Login page:
Application["UserList"] += Session["UserName"].ToString() + "<br/>";
and i tried this in the Logout page but it didnt work...:
String name = Session["UserName"].ToString();
Application.Remove(name);
you would use something along the lines of this:
Application["UserList"].ToString.Replace(Session["UserName"].ToString() + "<br/>", "");
But you really should not try manage a string. Use something like a HashTable and store that object in the application state. It's far easier to manage key/value pairs in a dictionary type construct.
Also it separates the data from the formatting which gives you much greater control over the output.
I have a page "Demo.aspx". I need to set some parameters using post method and redirect the page to "DemoTest.aspx".
Is there any way to set parameters in post method in asp.net? I don't want to set "Querystring" due to security propose.
Also I need server side code for this. I am not able to use "Javascript" or "Jquery" for the same.
Here is some more description for the same.
Right now I am using Response.Redirect("www.ABC.Com/DemoTest.aspx?P1=2"). So the page is simply redirect to the given URL.
Now I don't want to pass that "P1" in "Querystring". Instead of query string I want to use Post method.
Please note that redirection page is not in my own application. So I cant maintain session or "Viewstate".
Thanks in advance.
Use a session variable and response.redirect to the next page.
Session["MyVariable"] = "someThing";
Response.Redirect("DemoTest.aspx");
The value stored in Session variables will be accessible across application.
you can store in session like this :
Session["id"] = "anyID";
To get values On another page you need to write
string id = Convert.ToString(Session["Id"]);
However
By default, in .NET pages post() do the things automatically.
You will need to do sumthing like this:
Server.Transfer("DemoTest.aspx", True)
I'm creating a time sheet for work to learn more about asp and making database connections I am also using this time to prepare for my next C# and database design class which start on Wednesday. I'd like to know how I can get data from default.aspx and display it in timesheetdisplay.aspx, and I would also like to know how I can make it so the person doesn't have to enter the full id "100000111" as it appears in the database just the last 3.
<asp:TextBox id="xBadgeTextBox" runat="server" width="100px"></asp:TextBox>
As far as passing data between pages you can pass it via QueryString, Session variables, or by persisting it to some sort of data store such as a Database. In the situation above I would look at passing via Querystring parameter. Be sure that if you do do this that you validate the data on the new page to ensure its safety and validity before using it (think SQL Injection Attack).
How to: Pass Values Between ASP.NET Web Pages
As far as your second question goes I would say that this could be handled on the server side if you are sure that the last 3 digits will always be unique. Or were you looking to prompt the user entering data similar to Google? If so look at the AutoComplete Extender in the AJAX Control Toolkit or look at doing something similar in JQuery.
If you're redirecting from page to page, consider using the Server.Transfer("timesheetdisplay.aspx", true) method when navigating away from your default.aspx page. Note the second parameter, true, which will persist all ViewState and QueryString data across from page to page.
I would generate a unique key, store the value you are transfering in the users session, redirect the user and include the key in the query string, grab the key, and then get the value. Something like this:
//---On Default---
var value = "can be a single string or even a complext object...";
var keyName = Guid.NewGuid().ToString();
HttpContext.Current.Session[keyName] = value;
HttpContext.Current.Response.Redirect("timesheetdisplay.aspx?SID=" + keyName);
//---On TimeSheet---
var getKeyName = HttpContext.Current.Request.QueryString["sid"].ToString();
var myValue = HttpContext.Current.Session[keyName];
To get the id from a partial ID I would do it just like Muhammad Akhtar said:
select * From yourtable where id like '%111'