【问题标题】:how do i display the "On Offer" indicator when the product is on offer in the database of asp.net?当产品在 asp.net 的数据库中提供时,我如何显示“提供”指示器?
【发布时间】:2011-06-09 19:27:58
【问题描述】:

如果产品在数据库的“Offered”列中有数字“1”,我需要在 gridview 中的产品旁边显示“on Offer”指示器。如果为零,则不显示。有什么方法可以实现吗?谢谢。

在我的产品列表页面中: 将 objCat 调暗为新类别 将 objProduct 调暗为新产品 将 i 调暗为整数 Dim boolError As Boolean = False

objCat.ID = CType(Request.QueryString("CatID"), 整数)

' 获取类别的详细信息
objCat.GetDetails()

' 显示类别名称
lblCatName.Text = objCat.Name
lblCatName2.Text = objCat.Name

' 显示分类描述
lblCatDesc.Text = objCat.Description

 objCat.GetOfferedProducts()     

For i = 0 To gvProduct.Rows.Count - 1        
' Get the ProductId from the first cell        
 objProduct.ID = gvProduct.Rows(i).Cells(0).Text   

 Dim lblOffer As Label    
 lblOffer = CType(gvProduct.Rows(i).FindControl("lblOffer"), Label)   

   If objCat.Offered = "1" Then            
   lblOffer.Visible = True        
   Else            
  lblOffer.Visible = False         
   End If    

下一个
gvProduct.DataSource = objCat.GetProducts()
gvProduct.DataBind()

在我的类别中: 公共子 GetOfferedProducts()

' Define a conection to database    
' Read connection string from the web.config file.  
  Dim strConn As String    
 strConn = ConfigurationManager.ConnectionStrings("AppDb").ToString   
 Dim conn As New SqlConnection(strConn)   

' Retrieve details of a given Category ID from the database    
  Dim strSql As String     
 strSql = "SELECT * FROM CatProduct cp INNER JOIN Product p " & _     
           "ON cp.ProductID=p.ProductID INNER JOIN Category c ON                       cp.CategoryID=c.CategoryID " & _         
     "WHERE cp.CategoryID=@CategoryID"   

'定义一个Command对象来执行SQL语句
将 cmd 调暗为新的 SqlCommand(strSql, conn)
' 为 SQL 命令添加参数

cmd.Parameters.AddWithValue("@CategoryID", ID)

' 定义一个数据适配器来获取数据
Dim da As New SqlDataAdapter(cmd)

' 定义一个数据集来保存获取的数据
暗淡 ds 作为新数据集

' 打开数据库连接
conn.Open()

da.Fill(ds, "CatProduct")
'关闭数据库连接

conn.Close()

如果 ds.Tables("CatProduct").Rows.Count 0 那么
Name = ds.Tables("CatProduct").Rows(0)("CatName")
说明 = ds.Tables("CatProduct").Rows(0)("CatDesc") ImageFile = ds.Tables("CatProduct").Rows(0)("CatImage")
提供 = CType(ds.Tables("CatProduct").Rows(0)("Offered"), 整数) 结束如果

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    make this happen 有多种方式。基本上,您只是在网格中寻找conditionally show/hide an element

    在众多方法中,哪一种恰好是最好的方法,完全取决于您检索、绑定和显示数据的方式。您可以将逻辑放在业务层中(根据业务规则设置某个属性或为 null 等),在数据绑定代码中(如果您正在循环显示记录或其他内容,例如在 ItemDataBound处理程序),在您的显示代码中(如果您只是在 aspx 中声明所有内容并且只需要折腾一个 Eval 和一个条件)等等。

    【讨论】:

    • 所以我需要做一个for循环来找到显示消息的标签控件吗?但是我如何从数据库中检索值是零还是 1?
    • @user576785:这完全取决于您现在如何将数据绑定到网格。在您的问题中发布该代码,我们可以提供更多帮助。
    【解决方案2】:

    我会在你的 aspx 页面中连接 gridview 的 OnRowDataBound:

    <asp:gridview id="MyGridView"  
        autogeneratecolumns="true"
        allowpaging="true"
        onrowdatabound="MyGridView_RowDataBound" 
        runat="server">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label runat="server" id="lblOffer"/>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    
      </asp:gridview>
    

    然后在后面的代码中你可以这样做:

     void MyGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
          var lbl = e.Row.FindControl("lblOffer");
          If objCat.Offered = "1" Then            
              lbl.Visible = True        
          Else            
              lbl.Visible = False         
          End If    
        }
      }
    

    希望有帮助!!

    【讨论】:

    • 我没有在 Visual Studio 中绑定行数据。我不能使用你提供的那些代码。我很抱歉。 :(
    猜你喜欢
    • 2017-09-24
    • 1970-01-01
    • 2015-11-30
    • 2020-06-09
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    相关资源
    最近更新 更多