当模态为空时出现错误消息

时间:2011-09-22 19:33:11

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

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

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

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

<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语句不起作用,是否有人知道如何更改它以使其找到错误消息的空模式?

enter image description here

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

编辑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

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

2 个答案:

答案 0 :(得分: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 现在试试这个:

将OnCLick事件添加到FeatureButton中:

<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

答案 1 :(得分:0)

由于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