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

时间:2014-03-16 04:38:58

标签: c# asp.net twitter-bootstrap custom-controls

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

我创建了这个控件,以便它可以正确地重新编译以便于使用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>

Code Behind(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个小时现在无济于事。

谢谢!

1 个答案:

答案 0 :(得分:0)

我不知道bootstrap,但看起来你在输出中注入一个html复选框。如果是这种情况,它将不会被识别为回发时的服务器控件,但是您可以使用request.form对象获取其值。

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