我想实现如下功能:
DataGrid中有一个编辑的模板列
现在我已经实现了点中一行,然后此行就触发编辑事件.
想让某一行失去焦点的时候,就触发更新事件.
可以经过我的测试,好像在点中行的时候,行上面根本就没有焦点.所以就谈不上失去焦点的事件了.
我想问各位有没有办法使点中一行得到焦点,然后当某一行失去焦点的时候触发当前行的更新事件.程序主要代码如下:
private void DataGrid2_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onclick","setItemBackColor(this,'"+(e.Item.ItemIndex+2)+"')");
}
}-----------------------------------------------------------------<script language="javascript">
var Curr_TR=document.all.tags("tr");
function setItemBackColor(obj,index)
{
for(var i=1;i<Curr_TR.length;i++)
{
if (Curr_TR[i].style.backgroundColor =="yellow")
{
Curr_TR[i].style.backgroundColor="white";
}
}
obj.style.backgroundColor="yellow";
__doPostBack('DataGrid2$_ctl'+index+'$_ctl0','');
}
</script>
DataGrid中有一个编辑的模板列
现在我已经实现了点中一行,然后此行就触发编辑事件.
想让某一行失去焦点的时候,就触发更新事件.
可以经过我的测试,好像在点中行的时候,行上面根本就没有焦点.所以就谈不上失去焦点的事件了.
我想问各位有没有办法使点中一行得到焦点,然后当某一行失去焦点的时候触发当前行的更新事件.程序主要代码如下:
private void DataGrid2_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onclick","setItemBackColor(this,'"+(e.Item.ItemIndex+2)+"')");
}
}-----------------------------------------------------------------<script language="javascript">
var Curr_TR=document.all.tags("tr");
function setItemBackColor(obj,index)
{
for(var i=1;i<Curr_TR.length;i++)
{
if (Curr_TR[i].style.backgroundColor =="yellow")
{
Curr_TR[i].style.backgroundColor="white";
}
}
obj.style.backgroundColor="yellow";
__doPostBack('DataGrid2$_ctl'+index+'$_ctl0','');
}
</script>
解决方案 »
- 如何过滤掉非数字,但不要过滤逗号。
- 求正则表达式(急)
- 有关自定义控件的简单问题
- DirectoryEntry.Properties包括哪些项?
- 命名空间"System.Data.SQL"和"System.Data.ADO"为何不能使用啊?盼解答...
- 如何隐藏服务端控件或者某个按钮,b/s,c#
- Asp.net 里TreeView控件的一个奇怪问题。
- 关于COM口读取数据的问题
- DropDownList1 数据库绑定问题
- 释放资源:讨论Dispose和析构函数的使用
- 100分 急寻《c#入门经典》这本书最后3章的2个案例和一个范例的源代码! 马上就可以给分!
- 怎么把Request.QueryString的值传到一个框架页面中去?
Onclick的时候记录改行,并根据记录根新上一次记录的行。就是在Onclick的时候触发该行的编辑事件,那么为何不在编辑事件里面更新上次点击的记录?
你可以这样,把失去焦点的对象放在你点击编辑按钮之后出现输入框上。
你可在你的编辑行里面,放入一个按钮,此按钮的commandname=Update,然后在点击编辑时,让此按钮隐藏,style="display:none",这样,点击编辑出现编辑框时,此按钮不可见。然后,在每个输入框里面都添加脚本事件onblur="document.all("此按钮客户端ID").click();",如此,当你的焦点在任意一个输入框失去之时,即触发了隐藏的按钮的点击,此按钮的点击,触发了控件的update事件,即会更新。
昨天晚上回家按你的思路做了一下,发现有问题,即无法取得上次点击记录的编辑文本框里的值啊!望赐教!
{
string CurrentID = e.Item.Cells[0].Text;
DataGrid2.EditItemIndex = e.Item.ItemIndex; TextBox Text1 = (TextBox)e.Item.Cells[1].Controls[0];
TextBox Text2 = (TextBox)e.Item.Cells[2].Controls[0];
TextBox Text3 = (TextBox)e.Item.Cells[3].Controls[0];
TextBox Text4 = (TextBox)e.Item.Cells[4].Controls[0]; string txt1,txt2,txt3,txt4;
txt1=Text1.Text;
txt2=Text2.Text;
txt3=Text3.Text;
txt4=Text4.Text; if(Session["OldID"]!=null)
{
//更新数据库
SqlHelper sh = new SqlHelper();
string strSql = "UPDATE Employees SET LastName='"+txt1+"',FirstName='"+txt2+"',Title='"+txt3+"',City='"+txt4+"' WHERE EmployeeID='"+Session["OldID"]+"'";
sh.update(strSql);
//DataGrid2.EditItemIndex = -1;
}
Session["OldID"] = CurrentID;
BindGrid();
} private void DataGrid2_Cancel(object sender, DataGridCommandEventArgs e)
{
DataGrid2.EditItemIndex = -1;
BindGrid();
} private void DataGrid2_Update(object sender,DataGridCommandEventArgs e)
{
string ID = e.Item.Cells[0].Text; TextBox Text1 = (TextBox)e.Item.Cells[1].Controls[0];
TextBox Text2 = (TextBox)e.Item.Cells[2].Controls[0];
TextBox Text3 = (TextBox)e.Item.Cells[3].Controls[0];
TextBox Text4 = (TextBox)e.Item.Cells[4].Controls[0];
//更新数据库
SqlHelper sh = new SqlHelper();
string strSql = "UPDATE Employees SET LastName='"+Text1.Text+"',FirstName='"+Text2.Text+"',Title='"+Text3.Text+"',City='"+Text4.Text+"' where EmployeeID='"+ID+"'";
sh.update(strSql);
DataGrid2.EditItemIndex = -1;
BindGrid();
}
为何提示我在"TextBox Text1 = (TextBox)e.Item.Cells[1].Controls[0];"出错;
出错提示为<指定的参数已超出有效值的范围。参数名: index>
出错提示为<指定的参数已超出有效值的范围。参数名: index>
-----------------------------------------------------------------
点击编辑按纽,在DataGrid2.EditItemIndex = e.Item.ItemIndex;后数据要重新绑定
否则肯定找不到编辑模式下的TextBox