asp教程.net gridview数据绑定与xml绑定
当gridview编辑状态获取新值时,往往获取的是修改前的值。
我的分析:
很多时候我把数据绑定函数调用直接放在page_load()函数里,当gridview编辑状态获取新值时,往往获取的是修改前的值,这是因为页面加载先执行page_load()函数,数据绑定函数就会再次执行,这样gridview里的值就会又变成修改前的值,获取值时就是更改前的值。
解决办法:
把数据绑定函数放在page_load()函数的if(!this.page.ispostback)里,这样获取的值就是更改后的值。
获取gridview单元格值办法:
在rowupdating事件里获取第一个单元格内容
((textbox)(gridview.rows[e.rowindex].cells[1].controls[0])).text
数据绑定到
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="basicgridview" %>
<!doctype html public "-//w3c//dtd xhtml 1.1//en" "http://www.w3.org/tr/xhtml11/dtd/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>untitled page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:gridview id="gridview1" runat="server">
</asp:gridview>
</div>
</form>
</body>
</html>file: default.aspx.cs
using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.web.configuration;
using system.data.sqlclient;public partial class basicgridview : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
if (!this.ispostback)
{
string connectionstring = webconfigurationmanager.connectionstrings["northwind"].connectionstring;
string selectsql = "select productid, productname, unitprice from products";
sqlconnection con = new sqlconnection(connectionstring);
sqlcommand cmd = new sqlcommand(selectsql, con);
sqldataadapter adapter = new sqldataadapter(cmd);
dataset ds = new dataset();adapter.fill(ds, "products");
gridview1.datasource = ds;
gridview1.databind();
}
}
}
file: web.config
<?xml version="1.0"?>
<configuration>
<connectionstrings>
<add name="northwind" connectionstring="data source=localhostsqlexpress;initial catalog=northwind;integrated security=sspi"/>
</connectionstrings>
</configuration>
xml数据绑定到asp gridview
<%@ page language="c#" %>
<%@ import namespace="system.configuration"%>
<%@ import namespace="system.data"%><script runat="server">
void page_load(object sender, eventargs e)
{
dataset authorsdataset;
string filepath = server.mappath("authors.xml");
authorsdataset = new dataset();
//read the contents of the xml file into the dataset
authorsdataset.readxml(filepath);
authorsgird.datasource = authorsdataset.tables[0].defaultview;
authorsgird.databind();
}
</script><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>reading xml data into a dataset object </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:gridview id="authorsgird" runat="server"
autogeneratecolumns="false" cellpadding="4" headerstyle-backcolor="blue" headerstyle-forecolor="white"
headerstyle-horizontalalign="center" headerstyle-font-bold="true">
<columns>
<asp:boundfield headertext="last name" datafield="lastname" />
<asp:boundfield headertext="first name"
datafield="firstname" itemstyle-horizontalalign="right" />
</columns>
</asp:gridview>
</div>
</form>
</body>
</html>