【问题标题】:Error message when modal is emptymodal 为空时的错误消息
【发布时间】:2011-09-29 22:32:19
【问题描述】:

我的页面上有几个带有复选框的模式弹出窗口。复选框是可以添加到特定产品的不同项目。但是,有些产品分配了一种类型的项目。当模态框为空时,我需要一种在模态框中显示消息的方法。

我尝试在模式中使用“所有功能当前都与此产品相关联”的标签。但是当标签的可见性设置为隐藏时,标签会在模式中留下一个空间,这很烦人,所以我放弃了这个想法。

当模态框为空时显示隐藏消息的好方法是什么?

<asp:LinkButton ID="FeatureButton" runat="server">Feature</asp:LinkButton>
  <asp:Panel ID="FeaturePanel" runat="server" CssClass="modalPopup"
  Style="display:none">
     <div class="PopupHeader">Add a Feature</div>
       <asp:CheckBoxList ID="cbxAddFeature" runat="server" 
       DataSourceID="dsNewFeatures" DataTextField="FeatureTitle"
       DataValueField="FeatureID"></asp:CheckBoxList>
       **<asp:Label ID="FeatureError" runat="server" 
       Text="All features are currently associated to this product." 
       Display="none"></asp:Label>**
         <asp:Button ID="SubmitFeatures" runat="server" Text="Submit" />
         <asp:Button ID="CancelSubmitFeatures" runat="server" Text="Cancel" />
   </asp:Panel>
<asp:ModalPopupExtender ID="FeatureModal" runat="server" 
BackgroundCssClass="modalBackground" CancelControlID="CancelSubmitFeatures"
DropShadow="True" DynamicServicePath="" Enabled="True" 
PopupControlID="FeaturePanel" TargetControlID="FeatureButton">
</asp:ModalPopupExtender>   


Protected Sub SubmitFeatures_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles SubmitFeatures.Click
FeatureModal.Hide()
For Each feature As ListItem In cbxAddFeature.Items
**FeatureError.Visible = False**
If feature.Selected Then
'SQL INSERT: Marketing Table
Dim strSQL As String = "INSERT INTO Marketing (ProductID, MarketingTypeID, MarketingTitle, MarketingData) VALUES (@ProductID, 3, 'Feature', @MarketingData)"

Using cn As New   SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString)

Using cmd As New SqlCommand(strSQL, cn)

cmd.Parameters.Add(New SqlParameter("@ProductID", ProductID.Value))
cmd.Parameters.Add(New SqlParameter("@MarketingData", feature.Value))

cn.Open()

cmd.ExecuteNonQuery()
End Using
End Using
End If
**If (dsNewFeatures) == DBNull.Value Then
  FeatureError.Visible = True
End If**
 Next
 Response.Redirect(Request.RawUrl)
End Sub

<asp:SqlDataSource ID="dsNewFeatures" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ProductsConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:ProductsConnectionString.ProviderName %>" 
    SelectCommand="SELECT DISTINCT f.FeatureID, f.FeatureTitle 
    FROM Feature f LEFT JOIN Category c ON c.CategoryID = f.CategoryID 
    WHERE f.CategoryID IN 
    (SELECT CategoryID FROM CategoryLink 
    WHERE ProductID = @ProductID) AND f.FeatureID NOT IN 
    (SELECT m.MarketingData FROM Marketing m 
    WHERE m.MarketingTypeID = 3 AND m.ProductID = @ProductID) 
    ORDER BY f.FeatureTitle">
    <SelectParameters>
        <asp:QueryStringParameter Name="ProductID" QueryStringField="id" />
    </SelectParameters>
    <SelectParameters>
        <asp:QueryStringParameter Name="CategoryID" QueryStringField="id" />
    </SelectParameters>
</asp:SqlDataSource>

所有 **** 项都是标签的一部分,If, End If 语句不起作用,有谁知道我可以如何更改它以找到错误消息的空模式?

这就是现在的样子,注意显示的标签。我不知道为什么它不会消失!

编辑 9/29/11

Protected Sub dsNewFeatures_Selected(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles dsNewFeatures.Selected
    If FeatureError.Text = String.Format("rows count: {0}", e.AffectedRows) Then
        FeatureError.Visible = True
    Else
        FeatureError.Visible = False
    End If
End Sub

它几乎可以工作!仅根据此代码,标签不可见,但是当我清空模式时无法取消隐藏

【问题讨论】:

  • 简单地将“Display = none”添加到 asp:label 标记并不能消除空格。空白之所以存在,是因为 asp.net 隐藏控件的默认行为仍会在呈现的文档流中为它们留出空间。要解决此问题,您可以尝试将 For Each 循环中的 FeatureError.Visible = False 更改为 FeatureError.Attributes.Add("Style", "Display:None;") .该样式应将其从文档流中删除。这是一个链接,描述了我要更详细地解释的内容。 w3schools.com/css/css_display_visibility.asp
  • 我试了属性码,还是出现空格。我不知道它为什么在那里,但我必须习惯它在那里,因为我无法让它消失哈哈

标签: asp.net sql vb.net checkbox label


【解决方案1】:

当您说it's visibility is set to hidden 时,您指的是 CSS 可见性属性?如果是这样,请尝试改用display:none,这应该可以解决您不喜欢的间距问题。

如果那不这样做(我认为它会),只需在触发弹出对话框的事件时在后面的代码中将标签的 Visible 属性设置为 false。我相信当 Visible 属性为 false 时 ASP.NET 不会呈现元素,所以这肯定可以工作。

我的意思是:

annoyingLabel.Visisble=False

您可以根据是否需要显示消息相应地切换 Visible 属性。

希望这会有所帮助。

更新:

How about this?

Dim showNextTime As Boolean = False
If feature.Selected Then
  '' your code here
Else 
   showNextTime =True 
End If

FeatureError.Visible = showNextTime

由于您正在遍历所有项目并检查它们是否被选中,所以您需要做的就是设置一个标志,当其中一个项目未被选中时该标志将变为真(意思是,至少会有一个项目待补充)。

如果没有要通过的项目,则默认情况下 FeatureError.Visible 应该为 false。

这对你有用吗?

更新 2

Dim showNextTime As Boolean = False
If feature.Selected Then
  '' your code here
Else 
   showNextTime =True 
End If
' Add this condition to make it visible if Items.Count==0 
FeatureError.Visible = (showNextTime Or cbxAddFeature.Items.Count==0)

更新 3 现在试试这个:

向您的 FeatureButton 添加一个 OnCLick 事件,如下所示:

<asp:LinkButton OnClick="FeatureButton_Click" ID="FeatureButton" runat="server">Feature</asp:LinkButton>

在你的代码后面:

  Sub FeatureButton_Click(sender As Object, e As EventArgs) 
     FeatureError.Visible = (cbxAddFeature.Items.Count=0)
  End Sub

我们将完成这项工作。

更新 4: 将您的 OnSelected 代码更改为此(我不知道您为什么要比较文本):

Protected Sub dsNewFeatures_Selected(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles dsNewFeatures.Selected
    If  e.AffectedRows=0 Then
        FeatureError.Visible = True
        FeatureError.Text= "All features are currently associated to this product." 
    Else
        FeatureError.Text= "" 
        FeatureError.Visible = False
    End If
End Sub

【讨论】:

  • 我用 display:none 代替了 visible=false 但空间还在。
  • @jlg 请给我一些关于dsNewFeatures 的背景信息?如果ds 代表DataSet,则不应与DBNull.Value 进行比较,而应与Nothing 进行比较
  • @jlg 没关系。我在你的标记上看到了。这是我认为是 SQLDataSource 或类似的 DataSourceID。同样,您必须与Nothing 进行比较,而不是DBNull.ValueDBNull.Value 用于比较结果集中一行中的特定值,而不是用于与数据源本身进行比较。此外,无论是否有数据,dsNewFeatures 都可能不为空(在 VB.NET 中为 Not Nothing)。您需要针对数据库中的表发出另一个选择语句并计算特定项目是否存在行并显示 FeatureError
  • 我在原始帖子中发布了数据源。我将 DBNull.Value 更改为 Nothing
  • @jlg 它不起作用。正如我上面所说,dsNewFeatures 也不会是 Nothing。我将用更多指针更新我的答案,看看是否可行。我不确定我是否完全理解发生了什么,但我会试一试。
【解决方案2】:

由于 asp.net 控制幕后可见性的方式,您得到了空白。当控件呈现给浏览器时,它的 css 样式设置为visibility: hidden,这将在文档中元素应该存在的位置留下空白。如果要删除空白,则必须使用 css 样式display: none

因此,在您的代码中: 改变

<asp:Label ID="FeatureError" runat="server" 
       Text="All features are currently associated to this product." 
       Display="none"></asp:Label>

<asp:Label ID="FeatureError" runat="server" 
       Text="All features are currently associated to this product."></asp:Label>

改变

For Each feature As ListItem In cbxAddFeature.Items
**FeatureError.Visible = False**
....

 For Each feature As ListItem In cbxAddFeature.Items
    FeatureError.Attributes.Add("Style", "Display: None;")
...

改变

**If (dsNewFeatures) == DBNull.Value Then
  FeatureError.Visible = True
End If**

Dim dv as DataView
dv = CType(dsNewFeatures.Select(DataSourceSelectArguments.Empty), DataView)
If(dv.Count == 0)
  FeatureError.Attributes("Style") = "Display: Inline;"
End If

如果需要,您还可以将样式重构为样式表中。

参考: Visibility vs Display in CSS

【讨论】:

  • If (dsNewFeatures) == DBNull.Value Then 行不正常。它说“预期的表达式”,并且在其中一个 = 下有一条波浪线,当我尝试删除一个时,整行都会变成一条波浪线。
  • 我明白你的意思,更新了我的答案。您将不得不以不同于您尝试的方式检查空数据集。
  • 好主意,但标签还在。
  • 你认为我想要完成的事情可以用 Javascript 完成吗?我读过这样的问题可以用另一种方式完成,但我对 Javascript 比 VB.net 更陌生
猜你喜欢
  • 2016-03-03
  • 2015-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多