日期:2014-05-17  浏览次数:20469 次

ListView控件,加一个客户端的JS代码,就不能插入、更新了!?
需求:一个用户表 字段为 名字,年龄 和性别 (性别是CheckBox ),要求:CheckBox 选中时为男性,不必填写年龄,(即插入项,年龄输入框隐藏,用js实现)不选中时为女,年龄框可见,

  js和程序效果我都实现了,奇哉怪也的是:InputAttributes["onchange"] = "changeed(this,'" + txtage.ClientID + "')";时, 插入,更新都不可用了!!即在 编辑、插入时,填好数据后,点更新或插入,数据会被清空,且维持编辑状态不变,经探索:毛病出在 "changeed(this,'" + txtage.ClientID + "')"; 把此javascript函数的形式改一下 即 不用txtage.ClientID 或其他字符串时,该现象就会消失!!
不知所以然,望高手指教
我的源代码在:http://www.w8le.com/lkfup/Solution1.rar

        protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem || e.Item.ItemType == ListViewItemType.InsertItem)
            {
                CheckBox chb = e.Item.FindControl("UserGenderCheckBox") as CheckBox;
                TextBox txtage = e.Item.FindControl("UserAgeTextBox") as TextBox;
                if (chb != null && txtage !=null)
                {
                    //chb.Attributes["onchange"] = "changeed(this,'" + txtage.ClientID + "')"; 
                    chb.InputAttributes["onchange"] = "changeed(this,'" + txtage.ClientID + "')";
                }
            }
        }

------解决方案--------------------
前台给<asp:checkbox随便加个css的样式,如css="mycheck"
    给UserAgeLabel随便加个css样式,如:css="myAge"

前台js代码
$(document).ready(function(
{
   $(".mycheck").click(function()
   {
        if($(this).attr("Checked"))
        {
           $(this).parent().parent().Find(".myAge").hide();
        }
        else
        {
            $(this).parent().parent().Find(".myAge").show();
         }
   });
});


后台ListView1_ItemCreated里的代码可以去掉