1. 程式人生 > >CheckBoxList和RadioButtonList的使用(後臺取值和賦值)

CheckBoxList和RadioButtonList的使用(後臺取值和賦值)

CheckBoxList的html程式碼:
                            <asp:CheckBoxList ID="cblstYWGM" runat="server" RepeatDirection="Horizontal">
                            </asp:CheckBoxList>
                            <asp:CheckBox ID="cboxYWGM" Text="其它" runat="server" />
                            <asp:TextBox ID="txtGMQT" runat="server" />

當選擇其它javascript顯示隱藏TextBox

        function cbQiTaShowHide() {
            var txtId = "txtGMQT";
            var cbid = "cboxYWGM";
            $("#" + txtId).hide();
            $("#" + cbid).change(function () {
                if ($("#" + cbid).attr("checked")) {
                    $("#" + txtId).show();
                } else {
                    $("#" + txtId).hide();
                    $("#" + txtId).val('');
                }
            });
        }

CheckBoxList的後臺取前臺選擇的CheckBox的值,使用,隔開:

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < cblstYWGM.Items.Count; i++)
            {
                if (cblstYWGM.Items[i].Selected == true)
                {
                    sb.AppendFormat("{0},", cblstYWGM.Items[i].Text);
                }
            }
            if (cboxYWGM.Checked && !string.IsNullOrWhiteSpace(txtGMQT.Text.Trim()))
            {
                sb.AppendFormat("{0}q,", txtGMQT.Text.Trim());
            }
            if (sb.Length > 1)
            {
                info.DrugAllergy = sb.ToString().Substring(0, sb.Length - 1);//藥物過敏
            }
CheckBoxList的後臺取使用,隔開的字串,為前臺的CheckBox賦值:
                if (!string.IsNullOrWhiteSpace(info.DrugAllergy))
                {
                    string[] strs = info.DrugAllergy.Split(',');
                    foreach (var s in strs)
                    {
                        for (int i = 0; i < cblstYWGM.Items.Count; i++)
                        {
                            if (cblstYWGM.Items[i].Text == s)
                            {
                                cblstYWGM.Items[i].Selected = true;
                            }
                        }
                        if (s.Length > 1 && s.Substring(s.Length - 1) == "q")
                        {
                            cboxYWGM.Checked = true;
                            txtGMQT.Visible = true;
                            txtGMQT.Text = s.Substring(0, s.Length - 1);
                        }
                    }
                }

RadioButtonList的html程式碼:

                                <asp:RadioButtonList ID="rbtlstYszt" runat="server" RepeatDirection="Horizontal">
                                    <asp:ListItem Text="清醒" />
                                    <asp:ListItem Text="模糊" />
                                    <asp:ListItem Text="瞻望或煩躁" />
                                    <asp:ListItem Text="其它" />
                                </asp:RadioButtonList>
                                <asp:TextBox ID="txtYszt" runat="server" />
                                <asp:RequiredFieldValidator ForeColor="Red" ID="RequiredFieldValidator1" runat="server"
                                    ControlToValidate="rbtlstYszt" ErrorMessage="*"></asp:RequiredFieldValidator>
當選擇其它javascript程式碼顯示隱藏TextBox:
        function rbtQiTaShowHide() {
            var id = "rbtlstYszt";
            var txtId = "txtYszt";
            $("#" + txtId).hide();
            $("input[name=" + id + "]").change(function () {
                if ($("input[name=" + id + "][value=其它]").attr("checked") == "checked") {
                    $("#" + txtId).show();
                } else {
                    $("#" + txtId).hide();
                    $("#" + txtId).val('');
                }
            });
        }

RadioButtonList後臺取前臺選擇的RadioButton的值

            for (int i = 0; i < rbtlstYszt.Items.Count; i++)
            {
                if (rbtlstYszt.Items[i].Selected == true)
                {
                    if (rbtlstYszt.Items.FindByText("其它").Selected)
                    {
                        info.YSZT = txtYszt.Text.Trim();//意識狀態
                    }
                    else
                    {
                        info.YSZT = rbtlstYszt.SelectedItem.Text;
                    }
                }
            }