当查询字符串为空时,如何对特定类别进行计数?
我的主页/默认页面上有一个菜单,其中列出了 x 个类别。 我想计算每个类别中有多少产品。
例如:
香蕉(20)
苹果(8)
草莓(5)
到目前为止,我有这个:
var listSubMenu = __account.GetAllProductCategories();
var sb = new StringBuilder();
for (int i = 0; i < listSubMenu.Rows.Count; i++)
{
var r = listSubMenu.Rows[i];
var catid = Request.QueryString["thespecific_category_id_but_how_do_i_get_it?"];
var count = __account.GetSpecificCategory(id);
sb.AppendFormat(String.Format(@"<li{0}><a href='/account/products.aspx?categoryid={0}'>{1} ({2})</a></li>", r["cat_id"], r["cat_name"], count.Rows.Count));
}
active_sub_products.Text = sb.ToString();
My DataTable:
public DataTable GetAllProductCategories()
{
const string request =
@"
SELECT * FROM products_category
WHERE cat_active = 1
ORDER BY cat_name ASC
";
using (var query = new MySqlCommand(request))
{
return __dbConnect.GetData(query);
}
}
显然我需要特定的类别ID,但如何在不运行查询字符串的情况下请求该类别ID,因为它位于默认页面上。 我错过了一些明显的事情吗?
多谢。
I have a menu on my masterpage / defaultpage where I'm listing x categories.
I would like to make a count of how many products there are in each category.
EX:
Bananas(20)
Apples(8)
Strawberries(5)
So far, I have this:
var listSubMenu = __account.GetAllProductCategories();
var sb = new StringBuilder();
for (int i = 0; i < listSubMenu.Rows.Count; i++)
{
var r = listSubMenu.Rows[i];
var catid = Request.QueryString["thespecific_category_id_but_how_do_i_get_it?"];
var count = __account.GetSpecificCategory(id);
sb.AppendFormat(String.Format(@"<li{0}><a href='/account/products.aspx?categoryid={0}'>{1} ({2})</a></li>", r["cat_id"], r["cat_name"], count.Rows.Count));
}
active_sub_products.Text = sb.ToString();
My DataTable:
public DataTable GetAllProductCategories()
{
const string request =
@"
SELECT * FROM products_category
WHERE cat_active = 1
ORDER BY cat_name ASC
";
using (var query = new MySqlCommand(request))
{
return __dbConnect.GetData(query);
}
}
Obiously i need the specific categoryid, but how to I request that without having querystrings running since it is on the default page.
Am I missing something obious?
Thanks alot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该循环遍历您的类别并从那里获取 ID。不是来自查询字符串,因为它与您的页面相关(正如您自己编写的那样)。
鉴于您的示例,我希望 __account.GetAllProductCategories() 已经返回您需要的 ID
在这种情况下,您将使用类似的内容
但 id 取决于您 __account 返回的类型。
You should loop through your categories and get the ID from there. Not from the querystring since that is related to you page (as you wrote yourself as well).
Given your example, I would expect that __account.GetAllProductCategories() would return already the ID's you need
In that case you would use something like
But id depends on the type of what your __account returns.
如果我对 GetAllProductCategories() 的结果模式的猜测是正确的...
["cat_id"]["cat_name"]
[1][Apples]
[2][Bananas]
[3][Oranges]
或者可能
我也会更改:
到
:(有两个更改,(1)
除此之外,我建议您研究像 LinqToSql 这样的 ORM,以便您可以直接使用对象...
If I'm correct in my guess at your result schema from GetAllProductCategories()...
["cat_id"]["cat_name"]
[1][Apples]
[2][Bananas]
[3][Oranges]
or possibly
I would also change:
To:
(There are two changes, (1) <li{0}> to <li> {proper html syntax} and (2) r["cat_id"] to cat_id {you already have it in a variable and string.Format doesn't mind recasting to a string for you})
Beyond that I would suggest looking into an ORM like LinqToSql so you could work directly with objects...
首先在母版页中呈现类别链接:
** 当您调用 GetAllProductCategories 时,结果的每一行将至少有两列(cat_id 和 cat_name)。
当您按索引 (var r = listSubMenu.Rows[i]) 获取每一行时,它返回的行将具有该记录的 cat_id 和 cat_name,我添加了 (var name = r["cat_name"]) 以进行说明。
如果您对此进行调试并逐步执行,您应该会看到 for 循环的每次迭代都会为 id 变量提供下一个类别的 id,然后在行 (var count = __account.GetSpecificCategory(id);) 中使用该变量,
然后进入另一个文本框或区域实际页面“products.aspx”
** 注意:当您调用 Request.QueryString["value"] 时,它会:
返回 null 表示不存在名称匹配的查询字符串参数
或
返回表示 value= 与 url 末尾或下一个 & 之间内容的字符串找到了。
** 这不是完全生产质量的代码,您应该对查询字符串值进行额外的检查,例如切换到 tryparse,检查返回的产品数量并显示“找不到该类别的产品”...等**
First render the category links in the master page:
** When you call GetAllProductCategories, each row of the result will have at least two columns (cat_id and cat_name).
When you get each row by index (var r = listSubMenu.Rows[i]) the row it returns will have the cat_id and cat_name for that record, I added (var name = r["cat_name"]) for illustration.
If you debug this and step through you should see each iteration through the for loop gives the id variable the next category's id which is then used in the line (var count = __account.GetSpecificCategory(id);)
Then into another textbox or area of the actual page "products.aspx"
** Note: when you call Request.QueryString["value"] it will either:
Return null indicating that there isn't a querystring parameter with a matching name
or
Return the string representing the content between value= and the end of the url or the next & found.
** this isn't fully production quality code, there are additional checks you should be doing on the query string value, switch to tryparse for example, check number of products returned and show "No products found for that category" ... etc **