你要用什么控件做,是datagird还是datalist控件呀?我这里只有datagird做的例子。

解决方案 »

  1.   

    <%@ import namespace="system.data"%>
    <%@ import namespace="system.data.oledb"%>
    <%@ Page Language="VB" ContentType="text/html" ResponseEncoding="gb2312" %>
    <%
    if session("user")<>"admin" then
    response.Redirect("login.aspx")
    end if
    %>
    <script language="vb" runat="server">
    sub page_load(source as object,e as eventargs)
    if not page.ispostback then
    bindgrid()
    end if
    end sub
    sub changepage(sender as object,e as datagridpagechangedeventargs)
    dgsort.currentpageindex=e.newpageindex
    pagenum.text="第" & dgsort.currentpageindex+1 & "页"
    bindgrid
    end sub
    sub bindgrid()
    dim oleconn as new oledbconnection("provider=microsoft.jet.oledb.4.0;data source=" & server.mappath("newsmdb/news.mdb"))
    oleconn.open()
    dim oleds as new dataset
    dim oleda as new oledbdataadapter("select * from newsort order by id desc",oleconn)
    oleda.fill(oleds,"tbsort")
    dim total as integer
    total=oleds.tables("tbsort").rows.count
    totalnum.text="共" & cint(total/dgsort.pagesize)+1 & "页"
    if total=0 then
    response.write ("没有记录")
    exit sub
    end if
    dgsort.datasource=oleds.tables("tbsort").defaultview
    dgsort.databind
    oleconn.close
    oleconn=nothing
    end sub
    sub gridedit(sender as object,e as datagridcommandeventargs)
    dgsort.edititemindex=e.item.itemindex
    bindgrid
    end sub
    sub gridcancel(sender as object,e as datagridcommandeventargs)
    dgsort.edititemindex=-1
    bindgrid
    end sub
    sub gridupdate(sender as object,ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
    dim strsql,sort as string
    dim strid,introw as integer
    dim obj as textbox
    introw=e.item.itemindex
    obj=e.item.cells(0).controls(0)
    sort=obj.text
    strid=dgsort.datakeys.item(introw)
    strsql="update newsort set sort='" & sort & "' where id=" & strid
    dim oleconn as new oledbconnection("provider=microsoft.jet.oledb.4.0;data source=" & server.mappath("newsmdb/news.mdb"))
    oleconn.open
    dim olecmd as new oledbcommand
    olecmd.connection=oleconn
    olecmd.commandtext=strsql
    olecmd.executenonquery
    oleconn.close
    oleconn=nothing
    olecmd=nothing
    dgsort.edititemindex=-1
    bindgrid
    end sub
    </script>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>修改新闻类别</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <style type="text/css">
    td{font-size:13px;color:#ffffff}
    th{font-size:13px}
    </style>
    </head>

    <body bgcolor="#0082cd">
    <div align="center">
    <form runat="server">
    <asp:DataGrid Runat="server" BorderWidth="1" CellPadding="6" BorderStyle=Dashed DataKeyField="id" ID="dgsort" OnEditCommand="gridedit" OnCancelCommand="gridcancel" OnUpdateCommand="gridupdate" AutoGenerateColumns="False" AllowPaging="True" PageSize="10" Width="500" OnPageIndexChanged="changepage" PagerStyle-NextPageText="下一页" PagerStyle-PrevPageText="上一页">
    <Columns>
    <asp:BoundColumn HeaderText="新闻类别" HeaderStyle-ForeColor="#FF0000" HeaderStyle-BackColor="#3EB7FF" dataField="sort"></asp:BoundColumn>
    <asp:EditCommandColumn HeaderText="操作" HeaderStyle-ForeColor="#FF0000" HeaderStyle-BackColor="#3EB7FF" EditText="修改" UpdateText="保存" CancelText="取消"></asp:EditCommandColumn>
    </Columns>
    </asp:DataGrid>
    <asp:Label ID="pagenum" ForeColor="#FFFFFF" Runat="server"></asp:Label>
    <asp:Label ID="totalnum" ForeColor="#FFFFFF" Runat="server"></asp:Label>
    </form>
    </div>
    </body>
    </html>
      

  2.   

    哈.我是用TextBox做的,上面要求我一定要用TextBox做,不过其它控件可以实现这种效果也好,总之就是要竖着排列就是了
      

  3.   

    楼上的老大,非常感谢,不过用DataGrid做我也会做,但好像实现不好我图所示的排列.难就难在用TextBox做的,我也都实现了,可以在更新数据时会有点问题出现,更新不了,也不报错.唉.你们看到的就是我做完的结果http://www.kylixinfo.com/temp/qiu.gif
      

  4.   

    有点难度,要考虑到移动记录的问题,因为没有向ADO以前的rs.MoveNext.啊.就要自己写了
      

  5.   

    id你也要手工填写吗?
    把id设成只读的,然后移动记录根据id来就是了数据更新时出什么错,不就是取出textbox的内容然后写回数据库吗
      

  6.   

    没什么难度的挖,移动记录你取个结果集出来根据ID排序,翻页就翻结果集的下一条记录再重新赋值给textbox就行了挖更新时有什么问题?根据ID,update那条记录(用textbox里的text),然后再select这条记录(根据ID),再重新赋值给textbox就行了挖。你更新时数据库里的数据更新了吗?没报错那故居更新了,只是没有显示到textbox上吗?那得重新绑定这些textbox的挖
      

  7.   

    DataGrid + 自动分页功能
      

  8.   

    严重同意BenZ004(仲陵)
    没有显示到textbox上
    那得重新绑定这些textbox
      

  9.   

    唉,你们想到的我也都试过了.就是不行.现在贴上代码,看一下吧,你们: private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if (!IsPostBack)
    {
    DataSet ds = new DataSet(); String sConn = "server=localhost;UID=sa;PWD=sa;Initial Catalog=temp;";
    String sText = "SELECT * FROM WJPZ";
    SqlDataAdapter cmd = new SqlDataAdapter(sText, sConn); cmd.Fill(ds, "EmpTable");

    Session["MyData"] = ds;
    Session["CurrentRecord"] = 0;
    Session["RecordCount"] = ds.Tables["EmpTable"].Rows.Count-1;
    }

    Refresh();
    DisEnable();
    }
    public String GetBoundData(String fieldName)
    {
    DataSet ds = (DataSet) Session["MyData"];
    DataTable dt = ds.Tables["EmpTable"];
    int nRowPos = (int) Session["CurrentRecord"]; String buf = dt.Rows[nRowPos][fieldName].ToString();
    return buf;
    } public void Forward(Object sender, EventArgs e)
    {
    int nRowPos = (int) Session["CurrentRecord"];
    int nCount = (int) Session["RecordCount"];
    if (nRowPos < nCount)
    {
    nRowPos++; 
    Session["CurrentRecord"] = nRowPos;
    Refresh();
    } return;
    } public void Back(Object sender, EventArgs e)
    {
    int nRowPos = (int) Session["CurrentRecord"];
    if (nRowPos >0)
    {
    nRowPos--; 
    Session["CurrentRecord"] = nRowPos;
    Refresh();
    } return;
    }

    //编辑按钮事件
    public void DataEdit(Object sender,EventArgs e)
    {
    Enable();  //激活文本框
    lnkUpdate.Visible=true;
    lnkCancel.Visible=true;
    lnkDel.Visible=false;
    Linkbutton1.Enabled=false;
    Linkbutton2.Enabled=false;
    }

    //删除按钮事件
    public void DataDelete(Object sender,EventArgs e)
    {
    String sConn = "server=localhost;UID=sa;PWD=sa;Initial Catalog=temp;";
    SqlConnection conn=new SqlConnection(sConn);
    string sText="delete from WJPZ where ID=" + txtID.Text; SqlCommand cmd=new SqlCommand(sText,conn);
    conn.Open();
    try
    {
    cmd.ExecuteNonQuery();
    }
    catch
    {
    throw;
    }
    finally
    {
    conn.Close();
    }
    Refresh();

    } //更新按钮事件
    public void DataUpdate(Object sender,EventArgs e)
    {

    int ids=Int32.Parse(txtID.Text);//取得要修改的行的id值,并转换成int类型
    string name=txtTableName.Text; up_date(ids,name);
    /*String sText = "update WJPZ set 类型='"+txtType.Text+"',子标记='"+txtSub.Text+"',类型说明='"+txtTypeDescrip.Text+"',文件路径='"+txtFilePath.Text+"' where ID=11";
    //sText+="文件扩展名='{4}', 数据表名='{5}',后台表='{6}',可选处理='{7}',处理标志='{8}',对帐合并='{9}', ";
    //sText+="备份文件='{10}',金额缩倍='{11}',文件日期型='{12}',添加日期='{13}',结算日期='{14}',日期范围='{15}', ";
    //sText+="字段分隔符='{16}',字段日期型='{17}',类型序号='{18}' where ID=1"; //string.Format(sText,txtType.Text,txtSub.Text,txtTypeDescrip.Text,txtFilePath.Text);
    //txtFileExt.Text,txtTableName.Text,txtBackName.Text,txtOptionHand.Text,txtSign.Text,txtMager.Text,
    //txtBake.Text,txtMoney.Text,txtDate.Text,txtAddDate.Text,txtEndDate.Text,txtDateFan.Text,
    //txtSep.Text,txtDateZIDU.Text,txtTypeSign.Text);
    SqlConnection conn=new SqlConnection(sConn);
    SqlCommand cmd=new SqlCommand(sText,conn);
    conn.Open(); try
    {
    cmd.ExecuteNonQuery();
    }
    catch
    {
    throw;
    }
    finally
    {
    conn.Close();
    }*/
    //将更改提交回数据库之前,需要设置 InsertCommand、UpdateCommand 和 DeleteCommand 
    //来协调对数据库做出的更改。对于有限的方案,可使用 SqlCommandBuilder 自动生成这些命令。
    //SqlCommandBuilder sqlcommandbuilder=new SqlCommandBuilder(cmd);

    /*
     * 添加新数据
     * 
    datarow=ds.Tables["EmpTable"].NewRow();
    datarow["name"]="my";
    datarow["title"]="fool";
    datarow["text"]="dlfajd";
    dataset.Tables["ly"].Rows.Add(datarow);
    sqlDataAdapter1.Update(dataset,"ly");
    */ //刷新数据
    //Refresh(); DisEnable();
    lnkUpdate.Visible=false;
    lnkCancel.Visible=false;
    lnkDel.Visible=true;
    Linkbutton1.Enabled=true;
    Linkbutton2.Enabled=true; } //定义处理更新的方法
    //因为是简单示例,所以这个程序已经简化,直接进行更新
    //这里有两个参数,第一个用来查找需要更新的行,第二个是要修改的值
    private void up_date(int ids,string names)
    {
    //连接字符串,不明白的可以看本专题第二篇文章
    string connstr="server=localhost;UID=sa;PWD=sa;Initial Catalog=temp;";
    SqlConnection conn=new SqlConnection(connstr);
    string sql="select * from WJPZ";
    SqlDataAdapter ada=new SqlDataAdapter(sql,conn);//建立一个DataAdapter对象
    //这里的CommandBuilder对象一定不要忘了,一般就是写在DataAdapter定义的后面
    SqlCommandBuilder cb=new SqlCommandBuilder(ada);
    DataSet ds=new DataSet();//建立DataSet对象
    conn.Open();//打开连接 ada.Fill(ds,"mems");//填充DataSet

    //foreach(DataRow dr in ds conn.Close();//注意及时关闭连接

    DataTable dt=ds.Tables["mems"];//建立一个DataTable对象,方便操作
    dt.PrimaryKey=new DataColumn[]{dt.Columns["ID"]};//建立一个主键

    DataRow dr=dt.Rows.Find(ids);//根据参数查找到需要修改的行
    dr["数据表名"]=names;//对需要修改的记录赋新值
    ada.Update(ds,"mems");//用DataAdapter的Update()方法进行数据库的更新

    } //取消更新按钮事件
    public void DataCancel(Object sender,EventArgs e)
    {
    DisEnable();
    lnkUpdate.Visible=false;
    lnkCancel.Visible=false;
    lnkDel.Visible=true;
    Linkbutton1.Enabled=true;
    Linkbutton2.Enabled=true; } public void Refresh()
    {
    foreach (Control c in this.Controls[1].Controls)
    {
    if (c is System.Web.UI.WebControls.TextBox)
    {
    TextBox tb = (TextBox)c;
    //tb.Text="aaron";
    tb.DataBind();

    }
    }
    }
    public void DisEnable()
    {
    foreach (Control c in this.Controls[1].Controls)
    {
    if (c is System.Web.UI.WebControls.TextBox)
    {
    TextBox tb = (TextBox)c;
    //tb.Text="aaron";
    tb.Enabled=false;

    }
    }
    }

    public void Enable()
    {
    foreach (Control c in this.Controls[1].Controls)
    {
    if (c is System.Web.UI.WebControls.TextBox)
    {
    TextBox tb = (TextBox)c;
    //tb.Text="aaron";
    tb.Enabled=true;

    }
    }
    }
      

  10.   

    你updata更新的那一段为什么要传2个值?一个ID不就可以了吗?
    即使你需要NAME那么SQL语句中WHERE字缎为什么没有NAME?