ASP.Net Gridview,如何激活基于ID的编辑模式(DataKey)
发布时间:2020-05-25 16:13:25 所属栏目:asp.Net 来源:互联网
导读:我有一个页面,我们称之为SourceTypes.aspx,它有一个显示源类型列表的GridView. GridView的一部分是DataKey,SourceTypeID.如果源TypeID通过查询sting传递给页面,如何根据SourceTypeID将Gridview置于编辑模式以获取相应的行? GridView绑定到SQlDataSource对象.
|
我有一个页面,我们称之为SourceTypes.aspx,它有一个显示源类型列表的GridView. GridView的一部分是DataKey,SourceTypeID.如果源TypeID通过查询sting传递给页面,如何根据SourceTypeID将Gridview置于编辑模式以获取相应的行? GridView绑定到SQlDataSource对象. 我有一种感觉,当答案出现时我会踢自己! 我已经看了Putting a gridview row in edit mode programmatically,但它有些缺乏具体细节 解决方法当你想根据数据将其置于编辑模式时,这有点棘手.您告诉datagrid哪些显示的行是可编辑的,而不是您想要编辑哪些数据,因此您需要遍历网格中的每一行,查看它是否与id匹配,并将EditItemIndex设置为适当的价值和重新绑定.您可以在绑定之前查看源数据并从中获取行号,但是您可能遇到分页,排序等问题. 重新绑定网格有点麻烦,但我想不出更好的方法. public partial class _Default : System.Web.UI.Page
{
private DataTable GetData()
{
DataTable tTable = new DataTable();
tTable.Columns.Add(new DataColumn("Column1",typeof(int)));
tTable.Columns.Add(new DataColumn("Column2",typeof(string)));
DataRow tRow = tTable.NewRow();
tRow["Column1"] = 1;
tRow["Column2"] = "Test1";
tTable.Rows.Add(tRow);
tRow = tTable.NewRow();
tRow["Column1"] = 2;
tRow["Column2"] = "Test2";
tTable.Rows.Add(tRow);
tRow = tTable.NewRow();
tRow["Column1"] = 3;
tRow["Column2"] = "Test3";
tTable.Rows.Add(tRow);
tRow = tTable.NewRow();
tRow["Column1"] = 4;
tRow["Column2"] = "Test4";
tTable.Rows.Add(tRow);
tRow = tTable.NewRow();
tRow["Column1"] = 5;
tRow["Column2"] = "Test5";
tTable.Rows.Add(tRow);
return tTable;
}
private void BindData()
{
DataTable tTable = GetData();
TestGrid.DataSource = tTable;
TestGrid.DataBind();
if (!String.IsNullOrEmpty(Request.QueryString["edit"]))
{
foreach (DataGridItem tRow in TestGrid.Items)
{
if (tRow.Cells[0].Text == Request.QueryString["edit"])
TestGrid.EditItemIndex = tRow.ItemIndex;
}
TestGrid.DataBind();
}
}
protected void Page_Load(object sender,EventArgs e)
{
if (!Page.IsPostBack)
BindData();
}
}
您应该能够启动它(显然将数据网格添加到ASPX中)然后在URL的末尾放置?edit =以使其在编辑模式下打开相关条目. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-core-2.1 – 如何在asp.net Core 2.1.1中为Identit
- 启动ASP.NET窗体身份验证
- asp.net-mvc – 404 Asp.Net中的Http错误处理程序MVC(RC 5)
- ASP.NET – 在屏幕底部显示应用程序生成日期/信息
- asp.net-mvc – 在ASP.Net MVC 2中为非归因模型验证提供本地
- asp.net-mvc – ASP.Net MVC自定义模型绑定说明
- ASP.Net自定义客户端验证
- asp.net-mvc – 如何找出在MVC3中调用我的视图的控制器动作
- asp.net-mvc-3 – MVC直接在单个对象上调用模型绑定器
- 使用什么方法将ASP.Net应用程序部署到野外?
推荐文章
站长推荐
- ASP.NET C#静态变量是全局的?
- asp.net – 通过在弹性beanstalk的负载均衡器中的
- ASP.NET Webdeploy失败;项目中不存在AddSchedule
- 我可以创建一个不回发的ASP.NET ImageButton吗?
- asp.net-mvc-routing – ASP.NET Web Api路由(II
- asp.net-mvc – 在Razor中输出单引号生成的JavaS
- asp.net’记住我’不再使用表单身份验证了
- asp.net-mvc-3 – 带有EF 4.1和EntityState.Modi
- asp.net-mvc – ASP.NET MVC快速教程
- asp.net-mvc – 如何为mvc应用程序中的所有控制器
热点阅读
