动态加载一些控件

1、jquery去掉前后空格:

例: var Phone=$.trim($("#Phone").val());

 

 

 

2 ,在C#程序中动态加载下拉列表框,单选框,复习框。

客户端代码

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Control.aspx.cs" Inherits="Control" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>动态加载控件</title>
</head>
<body>  
    <div class="form" id="personalMoreInfoCellForm">
          <ul class="formInput">
            <li>
              <label>居住状况</label>
              <dl>
              <dd>
              <asp:Literal ID="litLiveStatus" runat="server"></asp:Literal>
              </dd>
              </dl>
            </li>                           
   
            <li>
              <labe>婚姻状况</label>
              <asp:Literal ID="LitMarry" runat="server"></asp:Literal>
            </li>
            
            
             <li>
               <label>平均每月花费</label>
               <select id="money">
               <asp:Literal ID="LitMoney" runat="server"></asp:Literal>
               </select>
             </li>
            </ul>
            
        </div>  
    
</body>
</html>

服务端代码

using System;
using System.Text;

public partial class Control : System.Web.UI.Page 
{
    private const string SelectedMark = "selected='selected'";
    private const string CheckedMark = "checked='checked'";

    private const string SelectOption = "<option {0} value='{1}'>{2}</option>";
    private const string CheckOption = "<label class='checklabel'><input type='checkbox' class='checkbox' name='{0}' value='{1}' {2}>{3}</label>";
    private const string RadioOption = " <label class='checklabel'><input type='radio' name='{0}' value='{1}' {2}> {3}</label>";
   
    protected void Page_Load(object sender, EventArgs e)
    {
        this.litLiveStatus.Text = this.BuildCheckOptions("LiveStatus", "和亲人,独居,和家人,和小孩", "和家人");
        this.LitMarry.Text = this.BuildRadioOptions("Marry", "已婚,未婚,保密", "保密");
        this.LitMoney.Text = this.BuildOptionsByString("1000,1500,2000,3000,4000,5000", "3000");
    }

    /// <summary>
    /// 绑定单选框的数据源
    /// </summary>
    /// <param name="name"></param>
    /// <param name="strString"></param>
    /// <param name="valueString"></param>
    /// <returns></returns>
    private string BuildRadioOptions(string name, string strString, string valueString)
    {
        StringBuilder options = new StringBuilder();
        string[] strSplit = strString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        for (int i = 0; i < strSplit.Length; i++)
        {
            if (valueString == strSplit[i].ToString())
            {
                options.Append(string.Format(RadioOption, name, strSplit[i], CheckedMark, strSplit[i]));
            }
            else
            {
                options.Append(string.Format(RadioOption, name, strSplit[i], string.Empty, strSplit[i]));
            }
        }
        return options.ToString();
    }

    /// <summary>
    /// 绑定复选框的值
    /// </summary>
    /// <param name="name"></param>
    /// <param name="strString"></param>
    /// <param name="viewString"></param>
    /// <returns></returns>
    private string BuildCheckOptions(string name, string strString, string viewString)
    {
        StringBuilder options = new StringBuilder();
        string[] strSplit = strString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        string[] strView = viewString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        bool isFind = false;
        for (int i = 0; i < strSplit.Length; i++)
        {
            for (int j = 0; j < strView.Length; j++)
            {
                if (strView[j] == strSplit[i].ToString())
                {
                    options.Append(string.Format(CheckOption, name, strSplit[i], CheckedMark, strSplit[i]));
                    isFind = true;
                    break;
                }
                else
                {
                    isFind = false;
                }

            }
            if (!isFind)
            {
                options.Append(string.Format(CheckOption, name, strSplit[i], string.Empty, strSplit[i]));
            }


        }
        return options.ToString();
    }

    /// <summary>
    /// 根据字符串,以,截取构造据源
    /// </summary>
    /// <param name="strString"></param>
    /// <param name="selectedValue"></param>
    /// <returns></returns>
    private string BuildOptionsByString(string strString, string selectedValue)
    {
        StringBuilder options = new StringBuilder("<option value='0'>请选择</option>");
        string[] strSplit = strString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        for (int i = 0; i < strSplit.Length; i++)
        {
            if (selectedValue == strSplit[i].ToString())
            {
                options.Append(string.Format(SelectOption, SelectedMark, strSplit[i], strSplit[i]));
            }
            else
            {
                options.Append(string.Format(SelectOption, string.Empty, strSplit[i], strSplit[i]));
            }
        }
        return options.ToString();
    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值