1) 改变控件的位置:Button1.Style.Add("LEFT","100px");
                   Button1.Style.Add("TOP","100px");2) Button tmpBtn = new tmpBtn;
   Page.Controls[1].Controls.Add(tmpBtn);

解决方案 »

  1.   

    我能不能象在VS.NET编辑状态下一样的用鼠标拖动的方法改变这个Button的位置或大小。如果能,请详告之,小弟不胜感激。
    -------------------------------------------------------------------------------
    可以但是需要用前台脚本定义鼠标的位置!最后问,页面有没有OnClick事件,比如我点空白页面的一个地方,那里就出现一个动态生成的Button.
    ------------------------------------------------------------
    在需要生成button的地方画个panel先,它有click事件,还可以根据panel.width和panel.hight确定相对位置!具体代码真的没时间写,不好意思!
      

  2.   

    3)给你个能编译运行的例子
    <%@ Page language="c#" Codebehind="draw2.aspx.cs" AutoEventWireup="false" Inherits="CTest.draw2" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>draw</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    <script language="javascript" src="jsgraphics.js" type="text/javascript"></script>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:datalist id="DataList1" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 392px"
    runat="server" Width="80%">
    <ItemTemplate>
    <asp:TextBox id="TextSelItem" runat="server" Width="70%"></asp:TextBox>
    <DIV   id="SelItemCoordinate" runat="server"><FONT size="+0">坐标:(</FONT>
    <asp:TextBox id="TextSelItemX" runat="server" Width="30px">0</asp:TextBox><FONT size="+0">,</FONT>
    <asp:TextBox id="TextSelItemY" runat="server" Width="30px">0</asp:TextBox><FONT size="+0">)</FONT></DIV>
    </ItemTemplate>
    </asp:datalist>
    <DIV id="parentdiv" style="Z-INDEX: 102; LEFT: 16px; WIDTH: 640px; POSITION: absolute; TOP: 40px; HEIGHT: 292px; BACKGROUND-COLOR: aliceblue"
    runat="server" ms_positioning="GridLayout"></DIV>
    <asp:textbox  id="TextOper" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 8px" runat="server"
    Width="24px">1</asp:textbox>&nbsp;&nbsp;<asp:radiobuttonlist id="RadioButtonList1" style="Z-INDEX: 108; LEFT: 16px; POSITION: absolute; TOP: 352px"
    runat="server" Width="448px" RepeatDirection="Horizontal" AutoPostBack="True">
    <asp:ListItem Value="0" Selected="True">定义节点</asp:ListItem>
    <asp:ListItem Value="1">节点位置排放</asp:ListItem>
    <asp:ListItem Value="2">答案</asp:ListItem>
    </asp:radiobuttonlist><asp:button id="Button2" style="Z-INDEX: 109; LEFT: 544px; POSITION: absolute; TOP: 8px" runat="server"
    Text="JustRefresh"></asp:button>
    <asp:TextBox id="TextAnswer" style="Z-INDEX: 110; LEFT: 48px; POSITION: absolute; TOP: 8px" runat="server"
    Width="480px"></asp:TextBox></form>
    <script>
                //被拖动的对象
                var Obj=''
                
                var startObj=""; 
                
                //被拖动对象的最初坐标位置
                var pLeft,pTop;

    //起点坐标,终点坐标
    var startLeft,startTop,endLeft,EndTop;

                var jg = new jsGraphics("parentdiv");
                
                //按下鼠标时
                function MDown(Object)
                {
    Obj=Object.id
                    //捕获对象
                    document.all(Obj).setCapture()
                    
                    //获得对象的最初坐标位置
    pLeft = document.all(Obj).style.pixelLeft;
    pTop = document.all(Obj).style.pixelTop;

    if (document.all("TextOper").value == 2)
                    {
    startLeft = document.all(Obj).style.pixelLeft + 10;
    startTop = document.all(Obj).style.pixelTop + 10;
    document.all("TextOper").value = 3;
    startObj = Obj;
                    }
                    else if (document.all("TextOper").value == 3)
                    {
    if(Obj != startObj)
    {
    endLeft = document.all(Obj).style.pixelLeft + 10;
    endTop = document.all(Obj).style.pixelTop + 10;
    document.all("TextOper").value = 2;

    jg.setColor("blue");
    jg.drawLine(startLeft,startTop,endLeft,endTop);
    jg.paint("parentdiv");
    document.all("TextAnswer").value = document.all("TextAnswer").value + startObj + "--" + Obj + " | ";
    startObj = "";
                    }
                    else
                    {
    document.all("TextOper").value = 3;
                    }
                    }
                    
                    pX=event.x-document.all(Obj).style.pixelLeft;
                    pY=event.y-document.all(Obj).style.pixelTop;   
                    
                }            function MMove()
                {
                    if (event.button == 1)
                    {
                        //对象的移动位置
                        if(Obj!='')
                        {
                            if ((document.all(Obj).style.pixelLeft + document.all(Obj).style.pixelWidth)< (document.all("parentdiv").style.pixelLeft + document.all("parentdiv").style.pixelWidth))
                            {
                            document.all(Obj).style.left=event.x-pX;                    
                        }
                        document.all(Obj).style.top=event.y-pY;
                        
                      
                        //document.all.TextBox1.value = document.all(Obj).style.pixelLeft + "," + (document.all("parentdiv").style.pixelLeft + document.all("parentdiv").style.pixelWidth) ;
                    }
                }
                
                }            function MUp()
                {
                    if(Obj!='')
                    {
    var index = Obj.replace("MoveDiv","");
    var txtXid = "DataList1__ctl" + index + "_TextSelItemX";
    var txtYid = "DataList1__ctl" + index + "_TextSelItemY";
    document.all[txtXid].value = document.all(Obj).style.pixelLeft;
    document.all[txtYid].value = document.all(Obj).style.pixelTop;
                        //释放对象
                 
                    document.all(Obj).releaseCapture();
                    if (document.all("TextOper").value != 1)
                    {
                        //还原到最初按下鼠标时的位置
                        document.all(Obj).style.pixelLeft = pLeft;
                        document.all(Obj).style.pixelTop = pTop;
                        }
                    Obj='';
                }            }
                            
                document.onmouseup=MUp;
                document.onmousemove=MMove;
                
                function CancelContextMenu()
                {
                    event.cancelBubble = true;
                    event.returnValue = false;
                }
                document.oncontextmenu = CancelContextMenu;
    </script>
    </body>
    </HTML>
      

  3.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace CTest
    {
    /// <summary>
    /// draw 的摘要说明。
    /// </summary>
    public class draw2 : System.Web.UI.Page
    {
    protected System.Web.UI.HtmlControls.HtmlGenericControl parentdiv;
    protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
    protected System.Web.UI.WebControls.Button Button2;
    protected System.Web.UI.WebControls.TextBox TextOper;
    protected System.Web.UI.WebControls.TextBox TextAnswer;
            protected System.Web.UI.WebControls.DataList DataList1;
        
    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
                if (!IsPostBack)
                {
                    this.AddDiv(6);

                }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e); }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.DataList1.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(this.DataList1_ItemDataBound);
    this.RadioButtonList1.SelectedIndexChanged += new System.EventHandler(this.RadioButtonList1_SelectedIndexChanged);
    this.Button2.Click += new System.EventHandler(this.Button2_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion        private void AddDiv(int n)
            {
                string [] strAry = new string[n];
                DataList1.DataSource = strAry;
                DataList1.DataBind();
            }
    private void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
    TextBox txt = new TextBox();
    HtmlGenericControl div = new HtmlGenericControl(); txt=(TextBox)e.Item.Controls[2].Controls[1];
    txt.Text = "10"; txt=(TextBox)e.Item.Controls[2].Controls[3];
    txt.Text = (10 + 30*e.Item.ItemIndex).ToString(); div = (HtmlGenericControl)e.Item.Controls[2];
    div.Style.Add("display","none");
    }
    private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
    {

    switch(RadioButtonList1.SelectedIndex)
    {
    case 0:
    this.DataList1.Style.Add("display","inline");
    this.TextOper.Text = "1";
    break;
    case 1:
    this.DataList1.Style.Add("display","inline");
    this.TextOper.Text = "1";
    break;
    case 2:
    this.DataList1.Style.Add("display","none");
    this.TextOper.Text = "2";
    break;
    }
    showSelItems();
    }

    private void showSelItems()
    {
    string html = "";
    string txtValue ="";
    string txtX = "";
    string txtY = "";
    TextBox txt = new TextBox();
    HtmlGenericControl div = new HtmlGenericControl(); for(int i=0;i<DataList1.Items.Count;i++)
    {
    txt=(TextBox)DataList1.Items[i].Controls[2].Controls[1];
    txtX = txt.Text.Trim(); txt=(TextBox)DataList1.Items[i].Controls[2].Controls[3];
    txtY = txt.Text.Trim(); txt=(TextBox)DataList1.Items[i].Controls[1];
    txtValue = txt.Text.TrimStart().TrimEnd();

    if(txtValue.Length>0)
    html += "<DIV onmousedown='MDown(this)' id='MoveDiv"+ i.ToString() +"' style='Z-INDEX: 105; LEFT: " + txtX + "px; CURSOR: hand; POSITION: absolute; TOP: "+ txtY +"px; BORDER-RIGHT: darkgreen 3px double; BORDER-TOP: darkgreen 3px double; BORDER-LEFT: darkgreen 3px double; BORDER-BOTTOM: darkgreen 3px double; BACKGROUND-COLOR: FloralWhite' ms_positioning='FlowLayout' >&nbsp;"+txtValue+"&nbsp;</DIV>";
    else if(this.RadioButtonList1.SelectedIndex>0)
    txt.Visible = false;
    else
    txt.Visible = true;

    div = (HtmlGenericControl)DataList1.Items[i].Controls[2];
    if(this.RadioButtonList1.SelectedIndex>0 && txtValue.Length>0)
    div.Style.Add("display","inline");
    else
    div.Style.Add("display","none");
    }
    this.parentdiv.InnerHtml =html;
    } private void Button2_Click(object sender, System.EventArgs e)
    {

    }
    }
    }
      

  4.   

    3)给你个能编译运行的例子(刚才那个搞错了、不行sorry)
    <%@ Page language="c#" Codebehind="draw3.aspx.cs" AutoEventWireup="false" Inherits="CTest.draw3" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>draw</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    <script language="javascript" src="jsgraphics.js" type="text/javascript"></script>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:datalist id="DataList1" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 392px"
    runat="server" Width="80%">
    <ItemTemplate>
    <asp:TextBox id="TextSelItem" runat="server" Width="70%"></asp:TextBox><DIV id="SelItemCoordinate" runat="server"><FONT>坐标:(</FONT>
    <asp:TextBox id="TextSelItemX" runat="server" Width="30px">0</asp:TextBox><FONT>,</FONT>
    <asp:TextBox id="TextSelItemY" runat="server" Width="30px">0</asp:TextBox><FONT>)</FONT></DIV>
    </ItemTemplate>
    </asp:datalist>
    <DIV id="parentdiv" style="Z-INDEX: 102; LEFT: 16px; WIDTH: 640px; POSITION: absolute; TOP: 40px; HEIGHT: 300px; BACKGROUND-COLOR: aliceblue"
    runat="server" ms_positioning="GridLayout" noWrap></DIV>
    <asp:textbox id="TextOper" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 8px" runat="server"
    Width="24px">1</asp:textbox>&nbsp;&nbsp;<asp:radiobuttonlist id="RadioButtonList1" style="Z-INDEX: 104; LEFT: 16px; POSITION: absolute; TOP: 352px"
    runat="server" Width="448px" RepeatDirection="Horizontal" AutoPostBack="True">
    <asp:ListItem Value="0" Selected="True">定义节点</asp:ListItem>
    <asp:ListItem Value="1">节点位置排放</asp:ListItem>
    <asp:ListItem Value="2">答案</asp:ListItem>
    </asp:radiobuttonlist><asp:button id="Button2" style="Z-INDEX: 105; LEFT: 544px; POSITION: absolute; TOP: 8px" runat="server"
    Text="JustRefresh"></asp:button>
    <asp:TextBox id="TextAnswer" style="Z-INDEX: 106; LEFT: 48px; POSITION: absolute; TOP: 8px" runat="server"
    Width="480px"></asp:TextBox><INPUT id="hidTag" style="Z-INDEX: 107; LEFT: 480px; POSITION: absolute; TOP: 352px" type="hidden"
    name="Hidden1" runat="server">
    <asp:TextBox id="TextBox1" style="Z-INDEX: 108; LEFT: 700px; POSITION: absolute; TOP: 104px"
    runat="server"></asp:TextBox></form>
    <script>
                //被拖动的对象
                var Obj=''
                
                var startObj=""; 
                
                //被拖动对象的最初坐标位置
                var pLeft,pTop;

    //起点坐标,终点坐标
    var startLeft,startTop,endLeft,EndTop;

                var jg = new jsGraphics("parentdiv");
                
                var clientWidth = document.all("parentdiv").clientWidth;
                var clientHeight = document.all("parentdiv").clientHeight;
                var clientLeft = document.all("parentdiv").style.pixelLeft;
                var clientTop = document.all("parentdiv").style.pixelTop;
                
                //document.all("TextBox1").value = clientWidth + "," + clientHeight + "," + clientLeft + "," + clientTop;
                //按下鼠标时
                function MDown(Object)
                {
    Obj=Object.id
                    //捕获对象
                    document.all(Obj).setCapture();
                    
                    //获得对象的最初坐标位置
    pLeft = document.all(Obj).style.pixelLeft;
    pTop = document.all(Obj).style.pixelTop;

    if (document.all("TextOper").value == 2)
                    {
    startLeft = document.all(Obj).style.pixelLeft + 10;
    startTop = document.all(Obj).style.pixelTop + 10;
    document.all("TextOper").value = 3;
    startObj = Obj;
                    }
                    else if (document.all("TextOper").value == 3)
                    {
    if(Obj != startObj)
    {
    endLeft = document.all(Obj).style.pixelLeft + 10;
    endTop = document.all(Obj).style.pixelTop + 10;
    document.all("TextOper").value = 2;

    jg.setColor("blue");
    jg.drawLine(startLeft,startTop,endLeft,endTop);
    jg.paint("parentdiv");
    document.all("TextAnswer").value = document.all("TextAnswer").value + startObj + "--" + Obj + " | ";
    startObj = "";
                    }
                    else
                    {
    document.all("TextOper").value = 3;
                    }
                    }
                    
                    pX=event.x-document.all(Obj).style.pixelLeft;
                    pY=event.y-document.all(Obj).style.pixelTop;   
                    
                }
                
                function MMove()
                {
                    if (event.button == 1 && document.all("hidTag").value != "2")
                    {
                        //对象的移动位置
                        if(Obj!='')
                        {
                            if ((event.x)< (clientWidth + clientLeft - 20) && (event.x) > (clientLeft + 20))
                            {
                            document.all(Obj).style.left=event.x-pX;                    
                        }
                        
                        if ((event.y) < (clientHeight + clientTop - 10) && (event.y) > (clientTop + 20))
                        {
                            document.all(Obj).style.top=event.y-pY;
                        }
                    }
                }
                
                }            function MUp()
                {
                    if(Obj!='')
                    {
    var index = Obj.replace("MoveDiv","");
    var txtXid = "DataList1__ctl" + index + "_TextSelItemX";
    var txtYid = "DataList1__ctl" + index + "_TextSelItemY";
    document.all[txtXid].value = document.all(Obj).style.pixelLeft;
    document.all[txtYid].value = document.all(Obj).style.pixelTop;
                        //释放对象
                 
                    document.all(Obj).releaseCapture();
                    if (document.all("TextOper").value != 1)
                    {
                        //还原到最初按下鼠标时的位置
                        document.all(Obj).style.pixelLeft = pLeft;
                        document.all(Obj).style.pixelTop = pTop;
                        }
                    Obj='';
                }            }
                            
                document.onmouseup=MUp;
                document.onmousemove=MMove;
                
                function CancelContextMenu()
                {
                    event.cancelBubble = true;
                    event.returnValue = false;
                }
                document.oncontextmenu = CancelContextMenu;
    </script>
    </body>
    </HTML>
      

  5.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace CTest
    {
    /// <summary>
    /// draw 的摘要说明。
    /// </summary>
    public class draw3 : System.Web.UI.Page
    {
    protected System.Web.UI.HtmlControls.HtmlGenericControl parentdiv;
    protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
    protected System.Web.UI.WebControls.Button Button2;
    protected System.Web.UI.WebControls.TextBox TextOper;
    protected System.Web.UI.WebControls.TextBox TextAnswer;
            protected System.Web.UI.HtmlControls.HtmlInputHidden hidTag;
            protected System.Web.UI.WebControls.TextBox TextBox1;
            protected System.Web.UI.WebControls.DataList DataList1;
        
    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
                if (!IsPostBack)
                {
                    this.AddDiv(6);

                }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e); }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.DataList1.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(this.DataList1_ItemDataBound);
    this.RadioButtonList1.SelectedIndexChanged += new System.EventHandler(this.RadioButtonList1_SelectedIndexChanged);
    this.Button2.Click += new System.EventHandler(this.Button2_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion        private void AddDiv(int n)
            {
                string [] strAry = new string[n];
                DataList1.DataSource = strAry;
                DataList1.DataBind();
            }
    private void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
    TextBox txt = new TextBox();
    HtmlGenericControl div = new HtmlGenericControl(); txt=(TextBox)e.Item.Controls[2].Controls[1];
    txt.Text = "10"; txt=(TextBox)e.Item.Controls[2].Controls[3];
    txt.Text = (10 + 30*e.Item.ItemIndex).ToString(); div = (HtmlGenericControl)e.Item.Controls[2];
    div.Style.Add("display","none");
    }
    private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
    {

    switch(RadioButtonList1.SelectedIndex)
    {
    case 0:
    this.DataList1.Style.Add("display","inline");
    this.TextOper.Text = "1";
    break;
    case 1:
    this.DataList1.Style.Add("display","inline");
    this.TextOper.Text = "1";
                        this.hidTag.Value = "1";
    break;
    case 2:
    this.DataList1.Style.Add("display","none");
    this.TextOper.Text = "2";
                        this.hidTag.Value = "2";
    break;
    }
    showSelItems();
    }

    private void showSelItems()
    {
    string html = "";
    string txtValue ="";
    string txtX = "";
    string txtY = "";
    TextBox txt = new TextBox();
    HtmlGenericControl div = new HtmlGenericControl(); for(int i=0;i<DataList1.Items.Count;i++)
    {
    txt=(TextBox)DataList1.Items[i].Controls[2].Controls[1];
    txtX = txt.Text.Trim(); txt=(TextBox)DataList1.Items[i].Controls[2].Controls[3];
    txtY = txt.Text.Trim(); txt=(TextBox)DataList1.Items[i].Controls[1];
    txtValue = txt.Text.TrimStart().TrimEnd();

    if(txtValue.Length>0)
    html += "<DIV onmousedown='MDown(this)' id='MoveDiv"+ i.ToString() +"' style='Z-INDEX: 105; LEFT: " + txtX + "px; CURSOR: hand; POSITION: absolute; TOP: "+ txtY +"px; BORDER-RIGHT: darkgreen 3px double; BORDER-TOP: darkgreen 3px double; BORDER-LEFT: darkgreen 3px double; BORDER-BOTTOM: darkgreen 3px double; BACKGROUND-COLOR: FloralWhite' ms_positioning='FlowLayout' >&nbsp;"+txtValue+"&nbsp;</DIV>";
    else if(this.RadioButtonList1.SelectedIndex>0)
    txt.Visible = false;
    else
    txt.Visible = true;

    div = (HtmlGenericControl)DataList1.Items[i].Controls[2];
    if(this.RadioButtonList1.SelectedIndex>0 && txtValue.Length>0)
    div.Style.Add("display","inline");
    else
    div.Style.Add("display","none");
    }
    this.parentdiv.InnerHtml =html;
    } private void Button2_Click(object sender, System.EventArgs e)
    {

    }
    }
    }
      

  6.   

    如果在服务端执行的话更改Button的Top和Left属性就行了,如果在客户端的话只能用楼上几位的JavaScript