GridView行编辑和数据绑定 [英] GridView RowEditing and Databinding
问题描述
亲爱的专业人士:
请帮助我解决此问题.
问题出在
我有两个asp.net网页,一个(GridCreateUser.aspx)具有gridview,另一个(CreateUser.aspx)具有文本框之类的控件
我正在使用vb.net作为背后的代码.
现在我想当我在gridcreateuser.aspx页面上的gridview中单击编辑链接时,适当的数据将绑定到我的createuser.aspx页面上.
我知道在桌面应用程序中我可以通过
做到这一点
createuser.textbox1.text =修剪(ds.Tables("Employee1").Rows(0)(0))
但是如何在基于浏览器的asp.net应用程序中呢?
在此先感谢!!!!!!!!!!!!!!!!!! 1
Dear Professional Peoples:
Plz Help Me To Resolve This Problem.
The Problem Is THat
i have two asp.net web pages one(GridCreateUser.aspx) has gridview and other(CreateUser.aspx) has controls like textboxes
i am using vb.net as code behind.
now i want when i click on edit link in my gridview on page gridcreateuser.aspx the appropriate data will bind to my createuser.aspx page???????????how i can do this.
I know in desktop application i can do this by
createuser.textbox1.text=Trim(ds.Tables("Employee1").Rows(0)(0))
but how in Browser based asp.net application????????????????
thanks in advance!!!!!!!!!!!!!!!1
推荐答案
不是世界上最好的代码,但是,您可以看一下以下简单实现并遵循类似的方法:
注意:请原谅C#代码.我不知道VB.NET,您必须将这些代码转换为VB.NET才能运行
Aspx:
Not the best piece of code in the world, but, you can have a look at the following simple implementation and follow a similar approach:
Note : Pardon the C# code. I don''t know VB.NET and you have to convert these codes to VB.NET to run
Aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="215px" OnRowCommand="GridView1_RowCommand"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
</form>
</body>
</html>
而且,CodeBehind(由于我不知道VB.net,所以用C#编写,请转换为VB.NET)
And, CodeBehind (Written in C# as I do not know VB.net, please convert into VB.NET)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
//A simple Person class
public class Person
{
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
public Person(int Id, string Name)
{
this.Id = Id;
this.Name = Name;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Populating some dummy data in the GridView. In reality, you should populate these from the database
IList items = new ArrayList();
items.Add(new Person(1, "Peter"));
items.Add(new Person(2, "John"));
items.Add(new Person(3, "Shubho"));
GridView1.DataSource = items;
GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
//User clicked Edit link
int userId = Convert.ToInt32(e.CommandArgument);
//Redirect to your create page here. In the CreateUser.aspx, load the user with the supplied userId from the database and populate user data in the text box
Response.Redirect(string.Format("CreateUser.aspx?Id={0}",userId));
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton link = e.Row.FindControl("LinkButton1") as LinkButton;
if (link != null)
{
//Set the CommandArgument to Person''s Id for the Link buttons so that, when user clicks the Edit link, the Id value can be retrieved from the CommandArgument
link.CommandArgument = ((Person)e.Row.DataItem).Id.ToString();
}
}
}
}
我为您提供了代码,因为某种程度上我觉得您是Asp.net的新手,并认为这会为您提供帮助.
I provided you the codes because somehow I feel you are kind of new in Asp.net and thought this will help you.
这篇关于GridView行编辑和数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!