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,027

HOME > .NET Framework > Forum > ต้องการแปล php เป็น asp.net c# ครับ รบกวนช่วยหน่อยครับ



 

ต้องการแปล php เป็น asp.net c# ครับ รบกวนช่วยหน่อยครับ

 



Topic : 088232



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



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




ผมต้องการใช้ autocomplete textbox แล้วเชื่อมต่อกับ SQL server

ผมเลยไปโหลดตัวอย่างนี้มาใช้ครับ
https://www.thaicreate.com/community/ajax-php-mysql-autocomplete-textbox.html
แต่ผมไม่เข้าใจมันเป็น php ครับ อยากเอามาใช้กับ asp.net

Code (PHP)
<?php
header("Content-type:text/html; charset=UTF-8"); 
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false); 
// เชื่อมต่อฐานข้อมูล
$link=mysql_connect("localhost","root","รหัสผ่าน") or die("error".mysql_error());
mysql_select_db("project",$link);
mysql_query("SET NAMES UTF8");
mysql_query("SET character_set_results=UTF8");
mysql_query("SET character_set_client=UTF8");
mysql_query("SET character_set_connection=UTF8");
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');
mb_language('uni');
mb_regex_encoding('UTF-8');
ob_start('mb_output_handler');
setlocale(LC_ALL, 'th_TH');

$q = urldecode($_GET["q"]);
//$q= iconv('utf-8', 'tis-620', $_GET['test']);
$pagesize = 50; // จำนวนรายการที่ต้องการแสดง
$table_db=" tb_db_type_job"; // ตารางที่ต้องการค้นหา
$find_field="type_job_name"; // ฟิลที่ต้องการค้นหา
$sql = "select * from $table_db where locate('$q', $find_field) > 0 order by locate('$q', $find_field), $find_field limit $pagesize";
$results = mysql_query($sql);
while ($row = mysql_fetch_array( $results )) {
$id = $row["type_job_name"]; // ฟิลที่ต้องการส่งค่ากลับ
$name =$row["type_job_name"]; // ฟิลที่ต้องการแสดงค่า
// ป้องกันเครื่องหมาย '
$name = str_replace("'", "'", $name);
// กำหนดตัวหนาให้กับคำที่มีการพิมพ์
$display_name = preg_replace("/(" . $q . ")/i", "<b>$1</b>", $name);
echo "<li onselect=\"this.setText('$name').setValue('$id');\">$display_name</li>";
}
mysql_close();
?>



ช่วยหน่อยครับ ขอบคุณ



Tag : .NET, Web (ASP.NET), C#







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2012-12-19 08:43:51 By : ERO-TIC View : 3086 Reply : 8
 

 

No. 1

Guest


หาโปเกม่อนสิ

https://www.thaicreate.com/dotnet/forum/044317.html






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-12-19 08:46:25 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 2



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



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


หาโปรเกมอนยิ่งงงเลย แล้วเว็บโปเกมอนเข้าไม่ได้แล้วครับ ไฟล์อะไรเยอะเยะไปหมด
ผมเห็นโค้ดนี้มันสั้นแล้วใช้ง่ายดี ผมลองกับ php แล้วใช้ได้
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-12-19 08:56:36 By : ERO-TIC
 

 

No. 3

Guest


ก็ไปใช้ php สิจ๊ะ

ของ asp.net ใช้ ajax toolkits - autocomplete จ้า

เอาตัวอย่างง่ายๆ ก็ได้ แต่ยังก็ต้องโหลด ajax toolkits มาติดตั้งก่อนอยู่ดี

ที่สำคัญๆ ต้องมี 2 ส่วนคือ ajax toolkits กลับ web service

aspx
        <ajax:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnableScriptGlobalization="true" EnableScriptLocalization="true">
        </ajax:ToolkitScriptManager>

                                        <asp:TextBox ID="TextBoxSearch" runat="server" autocomplete="off"></asp:TextBox>
                                        <ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" BehaviorID="AutoCompleteEx"
                                            runat="server" TargetControlID="TextBoxSearch" ServicePath="~/AutoComplete.asmx"
                                            ServiceMethod="GetAutoCompleteList" CompletionInterval="100" CompletionSetCount="12"
                                            DelimiterCharacters="" Enabled="true" EnableCaching="true" MinimumPrefixLength="1"
                                            CompletionListCssClass="autocomplete_completionListElement" CompletionListItemCssClass="autocomplete_listItem"
                                            CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem" OnClientPopulated="autocomplete_Populated"
                                            OnClientItemSelected="autocomplete_Selected">
                                        </ajaxToolkit:AutoCompleteExtender>


สุดท้ายก็โค้ด webservice คือใช้สร้าง web service ชื่อ AutoComplete.asmx แล้วไปแก้ AutoComplete.cs ใน app_code

AutoComplete.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;

using System.Web.SessionState;

/// <summary>
/// Summary description for AutoComplete
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService {

    public AutoComplete ()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string[] GetAutoCompleteList(string prefixText, int count)
    {
        // ต้องนี้เป็น linq จะเปลี่ยนเป็น sql command ก็ได้ แก้ได้ตามสะดวก
        SqlServerDataContext sqlServer = new SqlServerDataContext();

        string keyWord = prefixText.Trim().Replace(" ", string.Empty);
        List<string> result = new List<string>();

        (from d in sqlServer.Data
         where d.Name.Contains(keyWord)
         orderby s.Name ascending
         select new
         {
             Name = d.Name
         }).ToList().ForEach(d => result.Add(d.Name));

        return result.Take(count).ToArray();
    }
}






ส่วนนี่เพื่อความสวยงาม และเพิ่มความสามารถให้กรณีเลือก autocomplete แล้วให้ postback

javascript เพื่อความสวยงาม
    <script type="text/javascript">
        function autocomplete_Populated(sender, e) {
            if (sender._currentPrefix != null) {

                var list = sender.get_completionList();
                var search = sender._currentPrefix.replace(" ", "").toLowerCase();

                for (var i = 0; i < list.childNodes.length; i++) {
                    var text = list.childNodes[i].innerHTML;
                    var index = text.toLowerCase().indexOf(search);

                    if (index != -1) {
                        var value = text.substring(0, index);
                        value += '<span style="color: black;">';
                        value += text.substr(index, search.length);
                        value += '</span>';
                        value += text.substring(index + search.length);

                        list.childNodes[i].innerHTML = value;
                    }
                }
            }
        }

        function autocomplete_Selected(sender, e) {

            var SelectedText;
            // Check for IE - should really check for the availabbility of the property... if(e._item.innerText)
            if (document.all)
                SelectedText = e._item.innerText;
            else
                SelectedText = e._item.textContent;
            // Set the value of the textbox
            sender.get_element().value = SelectedText;

            // Get the value selected
            SelectedValue = e.get_value();
        }   
    </script>


css เพื่อความสวยงาม
/*AutoComplete flyout */

.autocomplete_completionListElement 
{  
	margin : 0px!important;
	background-color : inherit;
	color : windowtext;
	border : buttonshadow;
	border-width : 1px;
	border-style : solid;
	border-top: none;
	cursor : default;
	overflow : auto;
    text-align : left; 
    list-style-type : none;
    padding-left: 0px;
}

/* AutoComplete highlighted item */

.autocomplete_highlightedListItem
{
	background-color: #8dcff4;
	color: #808080;
	padding: 1px;
}

/* AutoComplete item */

.autocomplete_listItem 
{
	background-color : window;
	color: #808080;
	padding : 1px;
}
        
.ajax__tab_header 
{
    font-size: 12px;
    font-family: Tahoma, Sans-Serif;
}
        
/* Pager */
        
.pager:hover
{
    border: solid 2px #87CEEB;
    /* background-color: #FFFACD; */
}
        
.pager
{
    border: solid 2px #f0f0f0;            
}

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-12-19 09:25:16 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 4



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



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


ผมไปโหลด ajax toolkits - autocomplete มาติดตั้งแล้ว
ผมลองทำตามแล้วยังไม่ได้เลยครับ ช่วยดูหน่อยนะครับ

พิมพ์ไปแล้วไม่ขึ้นอะไรเลย
testauto

ไฟล์ทั้งหมด
autocomplete

Default.aspx
Code (ASP)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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>
    <link rel="stylesheet" type="text/css" href="http://localhost:3899/CSharp/StyleSheet.css" />
    <script type="text/javascript">
        function autocomplete_Populated(sender, e) {
            if (sender._currentPrefix != null) {

                var list = sender.get_completionList();
                var search = sender._currentPrefix.replace(" ", "").toLowerCase();

                for (var i = 0; i < list.childNodes.length; i++) {
                    var text = list.childNodes[i].innerHTML;
                    var index = text.toLowerCase().indexOf(search);

                    if (index != -1) {
                        var value = text.substring(0, index);
                        value += '<span style="color: black;">';
                        value += text.substr(index, search.length);
                        value += '</span>';
                        value += text.substring(index + search.length);

                        list.childNodes[i].innerHTML = value;
                    }
                }
            }
        }

        function autocomplete_Selected(sender, e) {

            var SelectedText;
            // Check for IE - should really check for the availabbility of the property... if(e._item.innerText)
            if (document.all)
                SelectedText = e._item.innerText;
            else
                SelectedText = e._item.textContent;
            // Set the value of the textbox
            sender.get_element().value = SelectedText;

            // Get the value selected
            SelectedValue = e.get_value();
        }   
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnableScriptGlobalization="true"
            EnableScriptLocalization="true">
        </asp:ToolkitScriptManager>
        <asp:TextBox ID="TextBoxSearch" runat="server" autocomplete="off"></asp:TextBox>
        <asp:AutoCompleteExtender ID="AutoCompleteExtender1" BehaviorID="AutoCompleteEx"
            runat="server" TargetControlID="TextBoxSearch" ServicePath="AutoComplete.asmx"
            ServiceMethod="GetAutoCompleteList" CompletionInterval="100" CompletionSetCount="12"
            DelimiterCharacters="" Enabled="true" EnableCaching="true" MinimumPrefixLength="1"
            CompletionListCssClass="autocomplete_completionListElement" CompletionListItemCssClass="autocomplete_listItem"
            CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem" OnClientPopulated="autocomplete_Populated"
            OnClientItemSelected="autocomplete_Selected">
        </asp:AutoCompleteExtender>
    </div>
    </form>
</body>
</html>


AutoComplete.cs
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Web.SessionState;

/// <summary>
/// Summary description for AutoComplete
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService {

    public AutoComplete () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string[] GetAutoCompleteList(string prefixText, int count)
    {
        // ต้องนี้เป็น linq จะเปลี่ยนเป็น sql command ก็ได้ แก้ได้ตามสะดวก
        dbsampleDataContext db = new dbsampleDataContext();

        string keyWord = prefixText.Trim().Replace(" ", string.Empty);
        List<string> result = new List<string>();

        (from d in db.Customers
         where d.CustomersName.Contains(keyWord)
         orderby d.CustomersName ascending
         select new
         {
             CustomersName = d.CustomersName
         }).ToList().ForEach(d => result.Add(d.CustomersName));

        return result.Take(count).ToArray();
    }    
}

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-12-20 09:54:39 By : ERO-TIC
 


 

No. 5

Guest


ตรง AutoComplete.cs

บรรทัดที่ 15 น่ะ

เอา comment ออก เพราะมันต้องใช้

[System.Web.Script.Services.ScriptService]
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-12-20 10:06:24 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 6



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



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


เอาบรรทัดที่ 15 ออก
พี่ครับ ขอบคุณมากๆครับ ผมทำได้แล้ว
ขอถามเพิ่มหน่อยครับ ถ้าผมมี textbox มากกว่า 1 ผมต้องเพิ่มตรงไหนบ้างครับ

<asp:TextBox ID="TextBoxSearch" runat="server" autocomplete="off"></asp:TextBox>
<asp:TextBox ID="TextBoxSearch1" runat="server" autocomplete="off"></asp:TextBox>
<asp:TextBox ID="TextBoxSearch"2 runat="server" autocomplete="off"></asp:TextBox>
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-12-20 10:29:43 By : ERO-TIC
 


 

No. 7

Guest


มี textbox ที่จะทำ autocomplete กี่อันคือต้องมี autocomplete extender เท่าจำนวนของ textbox

ส่วนจะเอา data ตรงไหนไปเป็น autocomplete ก็เปลี่ยนตรง ServiceMethod="GetAutoCompleteList"

แล้วไปเพิ่ม method ใน autocomplete.cs เอา

ส่วนอย่างอื่นเหมือนเดิม
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-12-20 10:44:41 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 8



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



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


ช่วยหน่อยครับ ผมต้องการส่งค่า ID จากฐานข้อมูลครับ
ต้องเขียนโค้ดที่ไฟล์ไหนครับ แล้วเขียนยังไงครับ

datac
เช่น ในรูปจะเป็นฐานข้อมูลใช่ไหมครับ
เมื่อผมพิมพ์มันจะโชว์ค่า ant bird ภาษาไทย อะไรพวกนี้ แต่ที่ผมต้องการคือ
ตอนกดส่งให้มันส่ง CID ไปแทนที่จะเป็นข้อความ

default.aspx
protected void Button1_Click(object sender, EventArgs e)
{
//ต้องเขีนรโค้ดยังไงครับ แล้วต้องไปเพิ่มโค้ดที่ไฟล์ AutoComplete.cs ยังไงครับ
var name = Convert.ToInt32(TextBoxSearch.Text);
//Label.text =
}

มันไม่ได้ไม่มีค่าอะไรส่งมา ผมจะลองกับเอาค่าไป ID ไปโชว์ที่ Label ก่อน
จุดประสงค์หลักของผมคือต้องการบันทึกค่า ID ลงในฐานข้อมูล ไม่เก็บเป็นข้อความ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-12-26 10:32:56 By : ERO-TIC
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ต้องการแปล php เป็น asp.net c# ครับ รบกวนช่วยหน่อยครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 03
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 อัตราราคา คลิกที่นี่