GridView中排在编辑模式下 [英] GridView row in edit mode
问题描述
我以前张贴类似的问题,但我仍然有一些问题的。我使用asp.net 4和C#。
请帮我明白我需要更改标签uxTest的文本,当用户点击编辑按钮一行。
你知道吗?感谢你们再次和抱歉,如果看起来像一个重复的问题。
<%@页面语言=C#%>
<%@导入命名空间=System.Data这%GT; !< DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> <脚本=服务器> 保护无效的Page_Load(对象发件人,EventArgs的发送)
{ 如果(!Page.IsPostBack)
{
//创建一个新表。
数据表taskTable =新的DataTable(任务列表); //创建列。
taskTable.Columns.Add(ID的typeof(INT));
taskTable.Columns.Add(说明中的typeof(字符串));
taskTable.Columns.Add(IsComplete的typeof(布尔)); //数据添加到新表。
的for(int i = 0; I< 20;我++)
{
DataRow中的TableRow = taskTable.NewRow();
tablerow的[ID] =我;
tablerow的[说明] =任务+ i.ToString();
tablerow的[IsComplete] = FALSE;
taskTable.Rows.Add(tablerow的);
} //坚持在表中的Session对象。
会话[TaskTable] = taskTable; //数据绑定到GridView控件。
BindData();
} } 保护无效TaskGridView_PageIndexChanging(对象发件人,GridViewPageEventArgs E)
{
TaskGridView.PageIndex = e.NewPageIndex;
//数据绑定到GridView控件。
BindData();
} 保护无效TaskGridView_RowEditing(对象发件人,GridViewEditEventArgs E)
{
//设置编辑索引。
TaskGridView.EditIndex = e.NewEditIndex;
//数据绑定到GridView控件。
BindData();
} 保护无效TaskGridView_RowCancelingEdit(对象发件人,GridViewCancelEditEventArgs E)
{
//重置编辑索引。
TaskGridView.EditIndex = -1;
//数据绑定到GridView控件。
BindData();
} 保护无效TaskGridView_RowUpdating(对象发件人,GridViewUpdateEventArgs E)
{
//从session对象检索表。
DataTable的DT =(数据表)会议[TaskTable]; //更新值。
GridViewRow行= TaskGridView.Rows [e.RowIndex]
dt.Rows [row.DataItemIndex] [ID] =((文本框)(row.Cells [1] .Controls [0]))文本。
dt.Rows [row.DataItemIndex] [说明] =((文本框)(row.Cells [2] .Controls [0]))文本。
dt.Rows [row.DataItemIndex] [IsComplete] =((复选框)(row.Cells [3] .Controls [0]))选中。 //重置编辑索引。
TaskGridView.EditIndex = -1; //数据绑定到GridView控件。
BindData();
} 私人无效BindData()
{
TaskGridView.DataSource =会话[TaskTable];
TaskGridView.DataBind();
}
< / SCRIPT> < HTML的xmlns =http://www.w3.org/1999/xhtml>
<头ID =头像1=服务器>
<标题> GridView的例子< /标题>
< /头>
<身体GT;
<表ID =form1的=服务器>
< DIV> < ASP:GridView控件ID =TaskGridView=服务器
AutoGenerateEditButton属性=真
AllowPaging =真
OnRowEditing =TaskGridView_RowEditing
OnRowCancelingEdit =TaskGridView_RowCancelingEdit
OnRowUpdating =TaskGridView_RowUpdating
OnPageIndexChanging =TaskGridView_PageIndexChanging
的AutoGenerateColumns =FALSE>
<柱体和GT;
< ASP:的TemplateField>
<&EditItemTemplate的GT;
< ASP:标签ID =uxTest=服务器文本=TEST>< / ASP:标签>
< / EditItemTemplate中>
< / ASP:的TemplateField>
< /专栏>
< / ASP:GridView的> < / DIV>
< /表及GT;
< /身体GT;
< / HTML>
code的解决方案:
如果(e.Row.RowType == DataControlRowType.DataRow&放大器;&安培;
(e.Row.RowState&安培; DataControlRowState.Edit)== DataControlRowState.Edit){
标签(DL)=(标签)e.Row.FindControl(uxLblTest); // Retrive控制在这里
}
为什么你需要去改变它,你的ItemTemplate和EditItemTemplate里可以有完全不同的数据/控制。下面,我表明2个不同的标签(见的ID)为项目/编辑模板。你可以任何你想要的改变,但是这是有不同的看法/编辑模式的基本方法。
< ASP:的TemplateField的HeaderText =测试列>
<&ItemTemplate中GT;
< ASP:标签ID =uxTest=服务器文本='<%#DataBinder.Eval的(集装箱,DataItem.TestColumn)%>'>
< / ASP:标签>
< / ItemTemplate中>
<&EditItemTemplate的GT;
< ASP:标签ID =uxTestEditMode=服务器文本='<%#DataBinder.Eval的(集装箱,DataItem.TestColumn)%>'>
< / ASP:文本框>
< / EditItemTemplate中>
< / ASP:的TemplateField>
I posted similar question before, but I still have some problem with it. I use asp.net 4 and c#.
Please help me to understand I need to change the TEXT for the Label uxTest when a user click the EDIT button for a single row.
Any idea? thanks guys once again and sorry if looks like a duplicates question.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// Create a new table.
DataTable taskTable = new DataTable("TaskList");
// Create the columns.
taskTable.Columns.Add("Id", typeof(int));
taskTable.Columns.Add("Description", typeof(string));
taskTable.Columns.Add("IsComplete", typeof(bool) );
//Add data to the new table.
for (int i = 0; i < 20; i++)
{
DataRow tableRow = taskTable.NewRow();
tableRow["Id"] = i;
tableRow["Description"] = "Task " + i.ToString();
tableRow["IsComplete"] = false;
taskTable.Rows.Add(tableRow);
}
//Persist the table in the Session object.
Session["TaskTable"] = taskTable;
//Bind data to the GridView control.
BindData();
}
}
protected void TaskGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
TaskGridView.PageIndex = e.NewPageIndex;
//Bind data to the GridView control.
BindData();
}
protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
TaskGridView.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
BindData();
}
protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//Reset the edit index.
TaskGridView.EditIndex = -1;
//Bind data to the GridView control.
BindData();
}
protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Retrieve the table from the session object.
DataTable dt = (DataTable)Session["TaskTable"];
//Update the values.
GridViewRow row = TaskGridView.Rows[e.RowIndex];
dt.Rows[row.DataItemIndex]["Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;
//Reset the edit index.
TaskGridView.EditIndex = -1;
//Bind data to the GridView control.
BindData();
}
private void BindData()
{
TaskGridView.DataSource = Session["TaskTable"];
TaskGridView.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>GridView example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="TaskGridView" runat="server"
AutoGenerateEditButton="True"
AllowPaging="True"
OnRowEditing="TaskGridView_RowEditing"
OnRowCancelingEdit="TaskGridView_RowCancelingEdit"
OnRowUpdating="TaskGridView_RowUpdating"
OnPageIndexChanging="TaskGridView_PageIndexChanging"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:Label ID="uxTest" runat="server" Text="TEST"></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Code solutions:
if (e.Row.RowType == DataControlRowType.DataRow &&
(e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit){
Label dl = (Label)e.Row.FindControl("uxLblTest");// Retrive control here
}
Why do you need to change it, your ItemTemplate and EditItemTemplate can have completely different data/controls. Below I'm show 2 different labels (see the ID's) for the Item/Edit templates. You can change anyway you want, but this is the basic approach to have different view/edit modes.
<asp:TemplateField HeaderText="Test Column">
<ItemTemplate>
<asp:Label ID="uxTest" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.TestColumn") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="uxTestEditMode" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.TestColumn") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
这篇关于GridView中排在编辑模式下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!