总结ASP.NET C#中经常用到的13个JS脚本代码

内容摘要
在C#开发过程中,免不了写一些JS,其实做后端开发的,本身不擅长写JS,干脆总结一下,方便自己也方便别人,分享给大家。呵呵~~
1.按钮前后台事件
\\r\\n ";

strAlertContent += "bbb"
文章正文

在C#开发过程中,免不了写一些JS,其实做后端开发的,本身不擅长写JS,干脆总结一下,方便自己也方便别人,分享给大家。呵呵~~

1.按钮前后台事件

\\r\\n ";
strAlertContent += "bbb" +" \\r\\n ";

11.点击GridView上的某一行时,行首列处的RadioButton处于选中状态,同时保存相关值在隐藏栏位

复制代码 代码如下:

//用查询得的数据集进行绑定
if (dt.Rows.Count > 0)
{
    //绑定
    this.gv_InfoFromSendModule.DataSource = dt;
    this.gv_InfoFromSendModule.DataBind();
    //确定按钮显示
    this.btn_OK.Visible = true;
    this.txthid_RowCount.Text = dt.Rows.Count.ToString();
}
//GridView的RowDataBound
protected void gv_InfoFromSendModule_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowIndex < 0)
      return;
   e.Row.Attributes.Add("onclick", "radButton('" + e.Row.RowIndex.ToString() + "','" + e.Row.Cells[1].Text.Trim() + "');");
   //RadioButton rad = (RadioButton)e.Row.Cells[0].FindControl("rad_Select");
   //rad.Attributes.Add("onclick", "radButton('"+e.Row.RowIndex.ToString()+"','"+ e.Row.Cells[1].Text.Trim()+"');");
}
//行上所绑定的JS
function radButton(rowIndex,rowGUID)
{
    //gv_InfoFromSendModule$ctl02$rad_Select
    var rowCount = parseInt(document.all.txthid_RowCount.value)+2;
    for(var i=2;i<rowCount;i++)
    {
        var tmpName;
        if(i<10)
        {
            tmpName = "gv_InfoFromSendModule$ctl0"+i+"$rad_Select";              
        }
        else
        {
            tmpName = "gv_InfoFromSendModule$ctl"+i+"$rad_Select";  
        }
        //取得对应的Radio对象
        var tmpRadio = document.getElementById(tmpName);
        //当前选中 其他取消选中
        if((i-2) == rowIndex)
        {                
            tmpRadio.checked = true;
        }
        else
        {
            tmpRadio.checked = false;
        }
    }
    document.all.txthid_GUID.value = rowGUID;
}

12.去掉前后空格

复制代码 代码如下:

function fn_Trim(obj)
{
    if(obj==null)
    {
       return;
    }
    else
    {
        var oldStr = obj.value;
        var newStr = oldStr.replace(/^\s+|\s+$/g,"");
        obj.value = newStr;
    }     
}

13.TextBox文本内容长度判断 看是否超过长度 超过返回true

复制代码 代码如下:

function fn_IsTooLong(obj,varLength)
{
    if(obj==null)
    {
       return false;
    }
    else
    {
        var valueStr = obj.value;
        var len = valueStr.match(/[^ -~]/g) == null ? valueStr.length : valueStr.length + valueStr.match(/[^ -~]/g).length ;
        if(len > parseInt(varLength) )
        {
            return true;
        }
        else
        {
            return false;
        }
    }     
}


代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!