无法读取自定义复选框服务器控件的选中状态

本文关键字:状态 服务器控件 复选框 读取 自定义 | 更新日期: 2023-09-27 17:50:23

我无法读取自定义复选框控件的已检查状态。

我创建了这个控件,以便它能够正确地呈现,以便与bootstrap一起使用。控件呈现完美,我有我想要/需要的所有功能,除了能够读取输入的检查状态,当用户单击"提交"。

自定义服务器控制代码(c#):

    public class InlineBootstrapCheckBox : CheckBox, IPostBackDataHandler, ICheckBoxControl
    {
        private string value;
        private string labelCSS;
        private string labelID;
        public string LabelID
        {
            get
            {
                if (!string.IsNullOrEmpty(this.labelID))
                {
                    return this.labelID;
                }
                else
                {
                    return null;
                }
            }
            set
            {
                this.labelID = value;
            }
        }
        public string LabelCSS
        {
            get
            {
                if (!string.IsNullOrEmpty(this.labelCSS))
                {
                    return this.labelCSS;
                }
                else
                {
                    return null;
                }
            }
            set
            {
                this.labelCSS = value;
            }
        }
        public string Value
        {
            get
            {
                if (!string.IsNullOrEmpty(this.value))
                {
                    return this.value;
                }
                else
                {
                    throw new NotImplementedException("You must set a 'Value' for InlineBootstrapCheckBox Controls");
                }
            }
            set
            {
                this.value = value;
            }
        }
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            writer.WriteLine(string.Format("<label id='"{0}'" class='"{1}'" for='"{2}'">", (this.LabelID != null) ? this.LabelID : string.Concat(this.ID, "_Label"), (this.LabelCSS != null) ? string.Concat(this.LabelCSS, " checkbox-inline") : "checkbox-inline", this.ID));
            writer.WriteLine(string.Format("<input type='"checkbox'" id='"{0}'" value='"{1}'">", this.ID, this.Value));
            writer.WriteLine(this.Text);
            writer.WriteLine("</label>");
        }
    }

标记代码(.ASPX):

<div class="form-group">
    <label id="lblCategories" class="col-sm-3 control-label">Categories</label>
    <div class="col-sm-9">
        <cookout:InlineBootstrapCheckBox runat="server" ID="chkRibs" Text="Ribs" Value="1" />
        <cookout:InlineBootstrapCheckBox runat="server" ID="chkChicken" Text="Chicken" Value="2" />
        <cookout:InlineBootstrapCheckBox runat="server" ID="chkBrisket" Text="Beef Brisket" Value="3" />
        <cookout:InlineBootstrapCheckBox runat="server" ID="chkPork" Text="Pork" Value="4" />
        <cookout:InlineBootstrapCheckBox runat="server" ID="chkAnythingBut" Text="Anything But" Value="5" />
    </div>
</div>

代码隐藏(c#):

这就是我认为我的问题所在?

protected void btnSubmit_Click(object sender, EventArgs e)
{
    ProRegistration regInfo = new ProRegistration();
    regInfo.TeamName = this.txtTeamName.Text.ToString();
    regInfo.ContactName = this.txtContactName.Text.ToString();
    regInfo.StreetAddress = this.txtContactAddress.Text.ToString();
    regInfo.City = this.txtContactCity.Text.ToString();
    regInfo.State = this.txtContactState.Text.ToString();
    regInfo.ZipCode = this.txtContactZip.Text.ToString();
    regInfo.Email = this.txtContactEmail.Text.ToString();
    regInfo.Phone = this.txtContactPhone.Text.ToString();
    regInfo.IsRibs = this.chkRibs.Checked;
    regInfo.IsChicken = this.chkChicken.Checked;
    regInfo.IsBrisket = this.chkBrisket.Checked;
    regInfo.IsPork = this.chkPork.Checked;
    regInfo.IsAnythingBut = this.chkAnythingBut.Checked;
    regInfo.Created = DateTime.Now;
    DataManager.InsertProfessionalRegistration(regInfo);
}

…基本上,我需要的是在提交这个对象到数据库时能够得到一个正响应。目前,无论点击状态如何,我都会得到一个错误的结果。

我已经花了大约3个小时来研究这个问题的答案,但无济于事。

谢谢!

无法读取自定义复选框服务器控件的选中状态

我不知道bootstrap,但看起来你正在向输出中注入html复选框。如果是这种情况,它将不会被识别为回发时的服务器控件,但是您可以通过使用请求获得其值。表单对象。

string result=Request.Form["thecheckboxid"];