Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,030

HOME > .NET Framework > Forum > มาลองใช้ codebehide สร้าง page ของ asp.net กัน asp.net เป็น oop controls ต่างๆ เป็น object หมด



 

มาลองใช้ codebehide สร้าง page ของ asp.net กัน asp.net เป็น oop controls ต่างๆ เป็น object หมด

 



Topic : 040987

Guest




asp.net เป็น oop controls ต่างๆ เป็น object หมด ดังนั้นเราจึงสามารถอ้างอิง

หรือสร้างขึ้นมาใช้งานจาก server script ได้ ไม่เว้นแม้กระทั้ง tag html ต่างๆ ด้วย

Default.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>
    </form>
</body>
</html>


จริงๆ สามรถลบ tag html head body ทิ้งทั้งหมดแล้วสร้างใน code behide ก็ได้แต่ไหนๆ

มัน gen ให้อยู่แล้วก็ปล่อยเลยตามเลยแล้วกัน

Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Net;
using System.Text;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    private const int MaxEmoticon = 30;

    protected void Page_Load(object sender, EventArgs e)
    {
        CreateHeaderElement();

        CreateJavaScript();

        CreateBodyElement();
    }

    protected void CreateHeaderElement()
    {
        Page.Title = "ThaiCreate Old Emoticon";

        Page.Header.Controls.Add(new LiteralControl(Environment.NewLine));

        HtmlMeta charSet = new HtmlMeta();
        charSet.HttpEquiv = "Content-Type";
        charSet.Content = "text/html; charset=utf-8";
        Page.Header.Controls.Add(charSet);

        Page.Header.Controls.Add(new LiteralControl(Environment.NewLine));

        HtmlLink cssStyle = new HtmlLink();
        cssStyle.Attributes.Add("type", "text/css");
        cssStyle.Attributes.Add("rel", "stylesheet");
        cssStyle.Href = "https://www.thaicreate.com/style/Style.css";
        Page.Header.Controls.Add(cssStyle);

        Page.Header.Controls.Add(new LiteralControl(Environment.NewLine));

        HtmlGenericControl script = new HtmlGenericControl();
        script.TagName = "script";
        script.Attributes.Add("type", "text/javascript");
        script.Attributes.Add("src", "jsfile.js");
        Page.Header.Controls.Add(script);

        Page.Header.Controls.Add(new LiteralControl(Environment.NewLine));    
    }

    protected void CreateBodyElement()
    {
        Table Table1 = new Table();
        Table1.BorderStyle = BorderStyle.Solid;
        Table1.BorderWidth = 1;
        Table1.BorderColor = System.Drawing.Color.Black;
        Table1.GridLines = GridLines.Both;
        Table1.CellPadding = 2;
        Table1.CellSpacing = 0;
        Page.Form.Controls.Add(Table1);

        TableHeaderRow HeaderRow = new TableHeaderRow();
        Table1.Rows.Add(HeaderRow);

        TableHeaderCell HeaderCell1 = new TableHeaderCell();
        HeaderCell1.Text = "ID";
        HeaderCell1.HorizontalAlign = HorizontalAlign.Center;
        HeaderRow.Cells.Add(HeaderCell1);

        TableHeaderCell HeaderCell2 = new TableHeaderCell();
        HeaderCell2.Text = "Pic";
        HeaderCell2.HorizontalAlign = HorizontalAlign.Center;
        HeaderRow.Cells.Add(HeaderCell2);

        TableHeaderCell HeaderCell3 = new TableHeaderCell();
        HeaderCell3.Text = "Image URL";
        HeaderCell3.HorizontalAlign = HorizontalAlign.Center;
        HeaderRow.Cells.Add(HeaderCell3);

        TableHeaderCell HeaderCell4 = new TableHeaderCell();
        HeaderCell4.Text = "Button";
        HeaderCell4.HorizontalAlign = HorizontalAlign.Center;
        HeaderRow.Cells.Add(HeaderCell4);

        for (int i = 0; i < MaxEmoticon; i++)
        {
            TableRow tableRow = new TableRow();
            Table1.Rows.Add(tableRow);

            TableCell TableCell1 = new TableCell();
            TableCell1.HorizontalAlign = HorizontalAlign.Center;
            tableRow.Cells.Add(TableCell1);

            TableCell TableCell2 = new TableCell();
            TableCell2.HorizontalAlign = HorizontalAlign.Center;
            tableRow.Cells.Add(TableCell2);

            TableCell TableCell3 = new TableCell();
            tableRow.Cells.Add(TableCell3);

            TableCell TableCell4 = new TableCell();
            TableCell4.HorizontalAlign = HorizontalAlign.Center;
            tableRow.Cells.Add(TableCell4);

            Label Label1 = new Label();
            Label1.Text = Convert.ToString(i + 1);
            TableCell1.Controls.Add(Label1);

            Image Image1 = new Image();
            TableCell2.Controls.Add(Image1);

            TextBox TextBox1 = new TextBox();
            TextBox1.ReadOnly = true;
            TextBox1.Text = "https://www.thaicreate.com/images/old_resource/" + ImageFile(i + 1) + ".gif";
            TextBox1.Width = 350;
            TableCell3.Controls.Add(TextBox1);

            Button Button1 = new Button();
            Button1.Text = "Select";
            Button1.Attributes.Add("onclick", "javascript:SelectText('" + TextBox1.ClientID + "'); return false;");
            TableCell4.Controls.Add(Button1);

            if (FileExist("https://www.thaicreate.com/images/old_resource/" + ImageFile(i + 1) + ".gif"))
            {
                Image1.ImageUrl = "https://www.thaicreate.com/images/old_resource/" + ImageFile(i + 1) + ".gif";
            }
            else
            {
                Image1.ImageUrl = "http://forums.regonline.com/forums/images/DeleteIcon.gif";
                TableCell4.Controls.Remove(Button1);
                TableCell4.Text = "&nbsp;";
            }
        }
    }

    protected void CreateJavaScript()
    {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.Append("function SelectText(txtBoxName)\n");
        stringBuilder.Append("{\n");
        stringBuilder.Append("\tvar txtBox = document.getElementById(txtBoxName);\n\n");

        stringBuilder.Append("\ttxtBox.focus();\n");
        stringBuilder.Append("\ttxtBox.select();\n");
        stringBuilder.Append("}\n");

        ClientScriptManager ClientScript = Page.ClientScript;

        if (!ClientScript.IsClientScriptBlockRegistered(this.GetType(), "SelectText"))
            ClientScript.RegisterClientScriptBlock(this.GetType(), "SelectText", stringBuilder.ToString(), true);
    }

    protected string ImageFile(int Index)
    {
        string txt = string.Empty;

        txt = Index.ToString().Length == 1 ? txt = "o0" + Index.ToString() : "o" + Index.ToString();

        return txt;
    }

    protected bool FileExist(string url)
    {
        HttpWebRequest request = null;
        HttpWebResponse response = null;
        bool exist = false;

        try
        {
            request = (HttpWebRequest)WebRequest.Create(url);
            request.KeepAlive = true;
            response = (HttpWebResponse)request.GetResponse();
            exist = true;
        }
        catch (WebException)
        {
        }
        finally
        {
            if (response != null)
                response.Close();
        }

        return exist;
    }
}


เขียนเสร็จลองรัน แล้ว view source ดู



Tag : - - - -







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2010-03-29 15:13:23 By : tungman View : 2385 Reply : 12
 

 

No. 1



โพสกระทู้ ( 11,835 )
บทความ ( 10 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


จะบวกให้กำลังก็ไม่ได้ ดันเป็น Guest






Date : 2010-03-29 18:19:33 By : plakrim
 


 

No. 2



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

เขียน .NET ยิ่งเขียนยิ่งสนุกครับ เพราะมีอะไรมากมายให้เราศึกษา
Date : 2010-03-29 18:31:02 By : webmaster
 

 

No. 3



โพสกระทู้ ( 1,603 )
บทความ ( 1 )



สถานะออฟไลน์


เทพค่ะพี่ตึ๋งศรีอย่างนี้เทพแล้ว ^ ^
Date : 2010-03-29 20:50:10 By : blurEye
 


 

No. 4



โพสกระทู้ ( 11,835 )
บทความ ( 10 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


ภาษาวัยรุ่น เขาเรียก เมพขิงๆ ใช่ไหมครับ
Date : 2010-03-29 22:20:23 By : plakrim
 


 

No. 5

Guest


อิอิ บ้ายอเหมือนกันวุ้ยเรานี่ แต่ .net มันยังมีอะไรให้เรียนรู้อีกเยอะเลย ตอนนี้ก็กำลังดู silverlight อยู่

เอาที่เหลือมารวมไว้เผื่อใครเอาไปใช้

แบบไม่มีอะไรเลย มีแบบบอกว่าใช้ code behide ไหน
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="html.aspx.cs" Inherits="html" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


สร้าง html tag, head tag และ body กับ form
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

public partial class html : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlGenericControl html = new HtmlGenericControl();
        html.TagName = "html";
        html.Attributes.Add("xmlns", "http://www.w3.org/1999/xhtml");
        Page.Controls.Add(html);

        HtmlHead header = new HtmlHead();
        html.Controls.Add(header);

        HtmlGenericControl body = new HtmlGenericControl();
        body.TagName = "body";
        html.Controls.Add(body);

        HtmlForm form1 = new HtmlForm();
        form1.ID = "form1";
        form1.EnableViewState = true;
        body.Controls.Add(form1);
    }
}

Date : 2010-03-29 22:59:42 By : tungman
 


 

No. 6

Guest


Code (C#)
<%@ 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>
    </form>
</body>
</html>


จากเจ้านี้เราสามารถอ้างอิง tag ต่างๆ ของ html ได้เช่น head กับ form ด้วย property ของ class page

เช่น ต้องการ add control ลง head ก็ใช้

Code (C#)
Page.Header.Controls.Add(control);


ต้องการ add control ลง form ก็ใช้

Code (C#)
Page.Form.Controls.Add(control);


A: แต่สมมติว่าถ้าเราอยากจะ add control หรือเพิ่ม attibute ลงใน tag body ล่ะจะอ้างอิงอย่างไร ?
Q: ต้องใช้ class HtmlControls

Code (C#)
HtmlControl body = (HtmlControl)Page.FindControl("Body");
body.Attributes.Add("onload", "alert('hello world');");


ซึ่งสามารถใช้ได้กับทุกๆ tag html เลย สามารถเอา tag ที่ต้องการจับมาเป็น object ใช้ในการอ้างอิงได้เลย
Date : 2010-03-30 08:26:21 By : tungman
 


 

No. 7

Guest


การเขียนใน codebehide มันมักจะยาวกว่าเขียนใน design ซึ่งถ้าลำบากนักก็เขียนที่ design

อย่างเดิมเถอะ แต่ถ้าจำเป็นต้องเขียนแบบ dynamic

asp.net ก็สามารถใช้ความสามารถของ oop สร้างหรืออ้างอิง control ทั้งหลายใน page ได้เช่นกัน
Date : 2010-03-30 08:32:23 By : tungman
 


 

No. 8



โพสกระทู้ ( 373 )
บทความ ( 0 )



สถานะออฟไลน์


+1 จัดไป ขอบคุณครับ ปกติผมก็เขียนแบบ codebehide เพราะผมไม่ได้มาทางสาย html มาทางสาย .net เลย พวกแท็ค html ไม่ค่อยมั่นใจ
Date : 2010-03-30 08:39:50 By : numenoy
 


 

No. 9



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

Go to : (C#) ASP.NET System.Data.OracleClient - DataTable() Part 2

ตัวอย่างการสร้าง Table ใน code behide
Date : 2010-03-30 08:51:39 By : webmaster
 


 

No. 10



โพสกระทู้ ( 34 )
บทความ ( 0 )



สถานะออฟไลน์


ยอดเลยครับพี่ มีทักษะอะไรดีๆ เอามาลงสอนน้องๆ อีกนะครับ
silverlight นิก็ได้นะครับมาลงบทความสอนหน่อย อิอิๆ ชอบ ๆ Microsoft
Date : 2010-03-31 02:42:04 By : chakrit021
 


 

No. 11

Guest


ผมก็ไม่ได้คลั่ง ms มากมักหรอกครับ แต่อะไรที่มันดีก็อยากศึกษา

เจ้า silverlight เนี่ยผมไม่ได้มองเรื่องการใช้งาน multimadia เลย แต่มองเกี่ยวกับ

การสร้าง user interface ของ app บนเว็บ คือมันเป็นโปรแกรมจริงๆ

แต่รันบนเว็บได้ ซึ่งแต่ต่างจาก flash ที่เป็นแค่ action script ก็เลยลองเล่นดู

ตอนนี้ยังไม่พร้อมจะแนะครับ ยังต้องศึกษาอีกน้อย
Date : 2010-03-31 11:15:00 By : tungman
 


 

No. 12



โพสกระทู้ ( 321 )
บทความ ( 5 )



สถานะออฟไลน์
Twitter Facebook

กำลังรอสูบความรู้จากคุณ tungman อยู่ครับ อิอิอิ
Date : 2010-03-31 19:14:54 By : kenessar
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : มาลองใช้ codebehide สร้าง page ของ asp.net กัน asp.net เป็น oop controls ต่างๆ เป็น object หมด
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่