Question

I have been given some c# code and have been asked to create a markup (.aspx) file that would go along with it.

I am not asking for help to write the code, but instead, how to go about it.

Here is the code:

public partial class search : Page
{
protected override void OnLoad(EventArgs e)
{
  int defaultCategory;
  try
  {
     defaultCategory = Int32.Parse(Request.QueryString["CategoryId"]);
  }
  catch (Exception ex)
  {
     defaultCategory = -1;
  }

  Results.DataSource = GetResults(defaultCategory);
  Results.DataBind();

  if (!Page.IsPostBack)
  {
     CategoryList.DataSource = GetCategories();
     CategoryList.DataTextField = "Name";
     CategoryList.DataValueField = "Id";
     CategoryList.DataBind();
     CategoryList.Items.Insert(0, new ListItem("All", "-1"));
     CategoryList.SelectedIndex = CategoryList.Items.IndexOf(CategoryList.Items.FindByValue(defaultCategory.ToString()));
     base.OnLoad(e);
  }         

}

private void Search_Click(object sender, EventArgs e)
{
  Results.DataSource = GetResults(Convert.ToInt32(CategoryList.SelectedValue));
  Results.DataBind();
}

private DataTable GetCategories()
{
  if (Cache["AllCategories"] != null)
  {
     return (DataTable) Cache["AllCategories"];
  }

  SqlConnection connection = new SqlConnection("Data Source=DB;Initial Catalog=Store;User Id=User;Password=PW;");
  string sql = string.Format("SELECT * From Categories");
  SqlCommand command = new SqlCommand(sql, connection);

  SqlDataAdapter da = new SqlDataAdapter(command);
  DataTable dt = new DataTable();

  da.Fill(dt);
  Cache.Insert("AllCategories", dt, null, DateTime.Now.AddHours(1), System.Web.Caching.Cache.NoSlidingExpiration);

  connection.Dispose();
  return dt;

}
private DataTable GetResults(int categoryId)
{
  SqlConnection connection = new SqlConnection("Data Source=DB;Initial Catalog=Store;User Id=User;Password=PW;");
  string sql = string.Format("SELECT * FROM Products P INNER JOIN Categories C on P.CategoryId = C.Id WHERE C.Id = {0} OR {0} = -1", categoryId);
  SqlCommand command = new SqlCommand(sql, connection);

  SqlDataAdapter da = new SqlDataAdapter(command);
  DataTable dt = new DataTable();

  da.Fill(dt);

  connection.Dispose();
  return dt;
}
}

EDIT

In the above code, what is the Results object and is the CategoryList just a listbox?

Was it helpful?

Solution

As Nilesh said this seems like a search page, You can possibly try creating the a Webform using Visual studio which is just drag and drop controls into canvas and that will create the mark up for the controls in the code window.

This code behind seems to be doing the following, On page load at Get request (when its !Page.IsPostBack) page is going to get categories using GetCategories() and fill the drop down list "CategoryList" with all category names (default selected one being the defaultcategory ID from query string).

The search button takes the dropdown's selected value and calls the GetResults() to get data table to fill the grid view "Results". So you need 3 controls (Dropdown list, Button, Gridview) in the webform with these names..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top