Get Query String

Get Query String

Query Strings

  • The part of a URL that is passed to the program
  • Composed of a series of field-value pairs
  • The field-value pairs are each separated by an equal sign
  • The series of pairs is separated by the ampersand, '&' or semicolon, ';'
  • Example: http://server/path/program?query_string

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Items.Item
  • Sitecore.Web.WebUtil
  • System.String

Query String C# Web Control Code

Sitecore.Data.Items.Item item = Sitecore.Context.Item;
if (item == null) return;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //  
    
string qString = Sitecore.Web.WebUtil.GetQueryString();
if (!System.String.IsNullOrEmpty(qString))
    output.Write("{0}&#58;&#160;{1}", "Query String found", "<b>" + qString + "</b>");
else
    output.Write("{0}", "Query String not found.  Please add a query string, such as ?tic=1, to the end of the URL in the address bar and refresh the page.");

output.RenderEndTag(); //Div


Query String C# Web Control Output

Query String not found. Please add a query string, such as ?tic=1, to the end of the URL in the address bar and refresh the page