2011-09-22 40 views
0

我的頁面上有幾個模式彈出窗口,顯示覆選框。複選框是可以添加到特定產品的不同項目。但是,某些產品分配了所有類型的項目。當模態爲空時,我需要一種方式在模態中顯示消息。模式爲空時出現錯誤信息

我已經嘗試在模式中使用標籤「所有功能當前與此產品相關聯」。但是當標籤的可見性被設置爲隱藏並且很煩人時,標籤在模式中留下空間,所以我放棄了這個想法。

有什麼方法可以在模態爲空時顯示隱藏信息?

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

所有****項目是標籤件,如果END IF語句不工作,沒有人知道我怎樣才能改變這種狀況得到它找到的錯誤消息的空模態?

enter image description here

這是它看起來像現在,請注意標籤顯示。我不知道它爲什麼不會消失! enter image description here

編輯11年9月29日

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 

它幾乎作品!標籤不可見僅基於此代碼,但當我清空模式時,無法取消隱藏

+0

簡單地增加「顯示=無」到ASP:標籤標記不會做刪除空間的伎倆。存在空白區域是因爲asp.net的隱藏控件默認行爲仍然會在渲染文檔流中留下空間。爲了解決這個問題,你可以嘗試改變你的For Each循環中的** FeatureError.Visible = False **爲** FeatureError.Attributes.Add(「Style」,「Display:None;」)**。該樣式應將其從文檔流中移除。這裏有一個鏈接,描述了我想要更詳細解釋的內容。 http://www.w3schools.com/css/css_display_visibility.asp – CheckRaise

+0

我試過了屬性代碼,空間仍然顯示出來。我不知道爲什麼它在那裏,但我只需要習慣它在那裏,因爲我無法讓它離開哈哈 – jlg

回答

1

當您說it's visibility is set to hidden時,請參閱CSS可見性屬性?如果是這樣,請嘗試使用display:none,並且應該修復您不喜歡的間距問題。

如果不這樣做(我認爲會),只需在觸發彈出對話框的事件時將標籤的Visible屬性設置爲false。我相信當Visible屬性爲false時,ASP.NET不會渲染元素,所以這肯定會起作用。

我的意思是這樣的:

annoyingLabel.Visisble=False 

您可以相應地切換Visible屬性取決於你是否需要顯示的信息或沒有。

希望這會有所幫助。

UPDATE:

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 

我們將完成這項工作。

UPDATE 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 
+0

我用display:none代替visible = false,但空間仍然存在。 – jlg

+0

@jlg請給我一些關於'dsNewFeatures'的背景信息來自哪裏?如果'ds'代表'DataSet',則不應該與'DBNull進行比較。價值',而是用'Nothing' – Icarus

+0

@jlg沒關係。我看到你的標記。這是DataSourceID,我認爲它是更高級的SQLDataSource或類似的東西。再次,你必須比較'Nothing',而不是'DBNull.Value'。 'DBNull.Value'用於比較結果集中某行的特定值,而不是與數據源本身進行比較。此外,無論是否有數據,dsNewFeatures都可能不爲null(在VB.NET中不是Nothing)。您需要針對數據庫中的表格發出另一個select語句,並計算是否有特定項目的行,並顯示FeatureError – Icarus

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

+0

'If(dsNewFeatures)== DBNull.Value Then'行不起作用。它表示「期望的表達」,並且在其中一個=下具有波浪線,當我嘗試刪除一行時,整條線得到一條波浪線。 – jlg

+0

我明白你的意思了,更新了我的答案。您將不得不以另一種方式檢查空數據集,而不是您嘗試執行的操作。 – CheckRaise

+0

好主意,但標籤仍然存在。 – jlg