Display Default Content If No QueryString Provided
Hi,
I have a query that takes data (articles) from a database and displays them on the page. If someone visits Page.aspx?id=1 they will see article 1. My problem is I want to display some default content such as a list of articles on Page.aspx or, if someone just manually removes the query string from the URL. I used to do this all the time in PHP/MySQL but don't know how to do it in ASP.NET C#. The code provided shows where I am up to. I did this with the "Configure Data Source".
Muhammad Ousama GhazaliSolution Analyst & ArchitectCommented:
Try the code provided in snipped box below in your code behind file. Make sure to make appropriate changes as per your situation.
'Define this on class/page levelPrivate m_intArticleId As IntegerPrivate m_strName As String'Write below code in Page_Load eventIf Request.QueryString("id") IsNot Nothing Then 'For Integer Values If Integer.TryParse(Request.QueryString("id"), m_intArticleId) Then SqlDataSource1.SelectParameters.Add("ArticleId", TypeCode.Int32, m_intArticleId) Else 'Send some default value here such as 0 SqlDataSource1.SelectParameters.Add("ArticleId", TypeCode.Int32, 0) 'Alternatively hide the grid and display default content on page directly 'GridView1.Visible = False 'Show some default text on some Label, etc. or unhide a pre End If 'For String Values If Request.QueryString("id").Length > 0 Then SqlDataSource1.SelectParameters.Add("ArticleId", TypeCode.Int32, m_intArticleId) Else 'Send some default value here such as 0 SqlDataSource1.SelectParameters.Add("ArticleId", TypeCode.Int32, 0) 'Alternatively hide the grid and display default content on page directly 'GridView1.Visible = False 'Show some default text on some Label, etc. or unhide a pre End IfEnd If
Muhammad Ousama GhazaliSolution Analyst & ArchitectCommented:
The String part in the above posted code has been wrongly posted, Use this one instead.
'For String Values If Request.QueryString("name").Length > 0 Then SqlDataSource1.SelectParameters.Add("ArticleId", TypeCode.String, Request.QueryString("name")) Else 'Send some default value here SqlDataSource1.SelectParameters.Add("ArticleId", TypeCode.Int32, "SomeDefaultName") 'Alternatively hide the grid and display default content on page directly 'GridView1.Visible = False 'Show some default text on some Label, etc. or unhide a pre End If
There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.
//Define this on class or page levelprivate int m_intArticleId;private string m_strName;//Write below code in Page_Load eventif (Request.QueryString("id") != null) { //For Integer Values if (int.TryParse(Request.QueryString("id"), m_intArticleId)) { SqlDataSource1.SelectParameters.Add("ArticleId", TypeCode.Int32, m_intArticleId); } else { //Send some default value here such as 0 SqlDataSource1.SelectParameters.Add("ArticleId", TypeCode.Int32, 0); } //Alternatively hide the grid and display default content on page directly //GridView1.Visible = False //Show some default text on some Label, etc. or unhide a pre //For String Values if (Request.QueryString("name").Length > 0) { SqlDataSource1.SelectParameters.Add("C_Name", TypeCode.String, Request.QueryString("name")); } else { //Send some default value here SqlDataSource1.SelectParameters.Add("C_Name", TypeCode.Int32, "SomeDefaultName"); } //Alternatively hide the grid and display default content on page directly //GridView1.Visible = False //Show some default text on some Label, etc. or unhide a pre}
Hi Moghazali,
I'm getting errors from line 9 in your code. The first error is: "The best overloaded method match for 'int.TryParse(string, out int)' has some invalid arguments"?
Hi,
Thanks for your help guys, in the end I used the first solution that GuitarRich posted.
Moghazali, I couldn't get your code to work but thanks anyway for the work you did.
Keith
0
Question has a verified solution.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.
Open in new window