首页 > 编程 > .NET > 正文

ASP.NET技巧:使Div内内容可编辑

2024-07-10 13:10:17
字体:
来源:转载
供稿:网友

呵呵,仅ie有效:)
前台代码:
<%@ 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 id="div1">
        <asp:gridview id="gridview1" runat="server" height="300px" width="200px">
        </asp:gridview>
        &nbsp;</div>
    <script language="javascript" type="text/javascript">
       document.getelementbyid("div1").contenteditable = true;
       document.execcommand('2d-position', true, true);
    </script>
    </form>
</body>
</html>后台代码:
using system;
using system.data;
using system.configuration;
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;

public partial class _default : system.web.ui.page
{
    protected void page_load(object sender, eventargs e)
    {
        if (!ispostback)
        {
            gridview1.style.add("position", "absolute");
            gridview1.datasource = getdataset();
            gridview1.datamember = "testtable";
            gridview1.databind();
        }
    }

    private dataset getdataset()
    {
        datatable dt = new datatable("testtable");
        dt.columns.add("col1", typeof(int));
        dt.columns.add("col2", typeof(string));
        dt.columns.add("col3", typeof(string));
        dt.columns.add("col4", typeof(string));

        datarow dr;

        for (int i = 0; i < 10; i++)
        {
            dr = dt.newrow();
            dr[0] = i;
            dr[1] = "val" + i.tostring();
            dr[2] = "val" + i.tostring();
            dr[3] = "val" + i.tostring();
            dt.rows.add(dr);
        }

        dataset ds = new dataset();
        ds.tables.add(dt);
        return ds;
    }
}



收集最实用的网页特效代码!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表