我有一个我需要支持的现有项目,它有一个数据库驱动的自定义控件(子控件由数据库行决定)。我过去已经成功地扩展了这一控制,但是,我遇到了一个问题。
我被要求添加对单选按钮列表(RBL)和复选框列表(CBL)的支持。我添加了代码,它们出现在屏幕上。在我为它们添加一个明确的链接之前,一切似乎都运行良好,而且,RBL控件值在服务器上的代码中保留了旧值。
我在LoadPostData中放了一个断点并检查了它们的值。当他们有值时,我会在集合中看到一个具有适当值的条目,当它们清楚时,它们都没有条目。正如所料。
由于CBL有效且RBL没有,这是否是.Net中RBL的那些可爱的“功能”之一?
非常感谢任何帮助。
编辑:请求的代码。我想保持这与实际解决方案完全相同,因此它使用了两个项目。一个用于控件的项目,Survey和一个用于Web应用程序的项目。请注意,webapp是C#。
重现: 在Save中的返回行上放置一个断点。点击两个“清除”链接。然后单击“保存”按钮。 CBL值为“”而RBL保持为“绿色”
Survey.vb:
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Public Class Survey
Inherits WebControl
Implements INamingContainer, IPostBackDataHandler
Private ReadOnly rblId As String = "rbl1"
Private ReadOnly cblId As String = "cbl1"
Public Function Save() As Boolean
Dim cbl = DirectCast(FindControl(cblId), CheckBoxList)
Dim rbl = DirectCast(FindControl(rblId), RadioButtonList)
Dim cb = cbl.SelectedValue
Dim rdo = rbl.SelectedValue
Return True
End Function
Protected Overrides Sub CreateChildControls()
Dim listItem As ListItem
Dim radioButtonList As New RadioButtonList
radioButtonList.ID = rblId
radioButtonList.Items.Add("red")
listItem = New ListItem("green")
listItem.Selected = True
radioButtonList.Items.Add(listItem)
Dim clearLink As HtmlAnchor = CreateLink(rblId)
Me.Controls.Add(radioButtonList)
clearLink.Attributes("onclick") = "$('#" & radioButtonList.ClientID & " input[type=radio]').attr('checked', false);"
Dim checkBoxList As New WebControls.CheckBoxList
checkBoxList.ID = cblId
checkBoxList.Items.Add("this")
listItem = New ListItem("that")
listItem.Selected = True
checkBoxList.Items.Add(listItem)
clearLink = CreateLink(cblId)
Me.Controls.Add(checkBoxList)
clearLink.Attributes("onclick") = "$('#" & checkBoxList.ClientID & " input[type=checkbox]').attr('checked', false);"
End Sub
Public Function CreateLink(ByVal id) As HtmlAnchor
CreateLink = New HtmlAnchor
CreateLink.ID = "linkClear" + id
CreateLink.HRef = "#"
CreateLink.InnerText = "Clear selection"
Me.Controls.Add(CreateLink)
End Function
Public Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean Implements System.Web.UI.IPostBackDataHandler.LoadPostData
Return True
End Function
Public Sub RaisePostDataChangedEvent() Implements System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent
End Sub
End Class
Default.aspx的:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestCustomControl._Default" %>
<%@ Register Assembly="Survey" Namespace="Survey" TagPrefix="cc1" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<cc1:Survey ID="ccSurvey" runat="server" />
<asp:Button ID="btnSave" runat="server" Text="Save" />
</asp:Content>
Default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TestCustomControl
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
ccSurvey.Save();
}
}
}
}