当用户单击下一行时,删除网格视图中的文本框属性

时间:2011-11-02 13:31:08

标签: asp.net gridview

大家好我编写了一个代码,用于在用户点击网格视图行时突出显示Row colourtext-box back ground colour。但是,当用户点击下一行时,我想清除文本框的应用颜色。

这是我将颜色添加到文本框

的代码
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    DataRowView drv = e.Row.DataItem as DataRowView;
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int iEmpID = Convert.ToInt32(drv["ID"]);

        //e.Row.Attributes.Add("ondblclick", "location='cliMaintainEEpersonaldetails.aspx?EmpID=" + iEmpID + "'");

        TextBox txb = (TextBox)e.Row.Cells[0].FindControl("lbl");
        if (txb.Text == string.Empty)
        {
            e.Row.Attributes.Add("onclick", "javascript:ChangeRowColor('" + e.Row.ClientID + "','" + iEmpID + "')");
            //txb.Attributes.Add("onclick", "document.all['" + e.Row.ClientID + "'].style.backgroundColor = ''red';");
            txb.Attributes.Add("onclick", "this.previous_color=this.style.backgroundColor;this.style.backgroundColor='#ffffda'");
            //txb.Attributes.Add("onmouseout", "this.style.backgroundColor=this.previous_color;");
        }
    }

 }

或者我可以为此编写java脚本函数,还是可以对现有脚本进行任何更改。

这是我的剧本

<script type="text/javascript">
//variable that will store the id of the last clicked row
var previousRow;

function ChangeRowColor(row,iEmpID)
{
//If last clicked row and the current clicked row are same
if (previousRow == row)
return;//do nothing
//If there is row clicked earlier
else if (previousRow != null)
 document.getElementById(previousRow).style.backgroundColor = "#ffffff"; //change the color of the previous row back to white

document.getElementById(row).style.backgroundColor = "#ffffda"; //change the color of the current row to light yellow
//location="Default9.aspx";
//assign the current row id to the previous row id for next row to be clicked

previousRow = row;
//document.getElementById('ctl00_ContentPlaceHolder1_HiddenField1').value = iEmpID ;
}
    </script>

示例图片

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用JQuery将单击处理程序附加到GridView中的每个文本输入区域,并管理处理程序中单元格和输入的样式。

请参阅以下示例:http://jsfiddle.net/dYBfU/