 |
|
|
 |
 |
|
คุณจะกำหนด Slected Value ให้ DropDownList8 ใช่ไหมครับ?
|
 |
 |
 |
 |
Date :
2010-02-16 16:18:39 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
DropDownList8.Text = Session["Nationality1"].ToString();
dropdownlist คุณ ใส่ text เข้าไปไม่ได้ครับ คุณต้องใส่เป็น Item เข้าไป
DropDownList8.Items.Add(Session["Nationality1"].ToString());
ทำนองนี้ครับ
|
 |
 |
 |
 |
Date :
2010-02-16 16:41:09 |
By :
numenoy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
dropdownlist 8 เชือมต่อกับ datasorce อยู่อะคับ
ลอง ทำ DropDownList8.Items.Add(Session["Nationality1"].ToString());
แล้วครับ ไม่ error แต่ไม่ค่าเข้า dropdownlist8 คับ
|
 |
 |
 |
 |
Date :
2010-02-17 14:11:19 |
By :
kiko |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เงียบจัง ....
|
 |
 |
 |
 |
Date :
2010-02-17 22:50:11 |
By :
kiko |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณคับ แต่ยังไม่ได้เลบ - -"
|
 |
 |
 |
 |
Date :
2010-02-18 10:03:23 |
By :
kiko |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เดาให้
Code (C#)
foreach (ListItem item in DropDownList8.Items)
{
if (item.Text == Session["Nationality1"].ToString())
{
item.Selected = true;
}
}
ที่หลังคำว่าคับเขาเขียนแบบนี้ --> ครับ
เขียนภาษาไทยแบบถูกต้องผื่นไม่ขึ้นหรอกเชื่อดิ
|
 |
 |
 |
 |
Date :
2010-02-18 10:39:43 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ครับ
ยังไม่ได้เลย อ่ะ
มีวิธีอื่นไหมครับ
|
 |
 |
 |
 |
Date :
2010-02-18 12:58:35 |
By :
kiko |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ได้อะแปลกแล้ว ผมเองใช้วิธีนี้อยู่ก็ใช้ได้นี่ ต้องลองตรวจสอบค่าระหว่าง Session["Nationality1"].ToString() กับ item.Value หรือ item.Text แล้วล่ะแบบนี้ว่าทำไมถึงไม่ตรง
ลองให้มันแสดงค่าออกมาดู
|
 |
 |
 |
 |
Date :
2010-02-18 13:15:25 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = Session["UserNameCustomer1"].ToString();
TextBox2.Text = Session["Password1"].ToString();
TextBox3.Text = Session["FirstName1"].ToString();
TextBox4.Text = Session["LastName1"].ToString();
foreach ListItem item in DropDownList8.Items
{
if (item.Value == Session["Nationality1"].ToString())
{
item.Selected = true;
}
}
ลองดูให้หน่อยครับ
ใช้แล้ว มัน error ตรง บรรทัด foreach ListItem item in DropDownList8.Items ครับ
|
 |
 |
 |
 |
Date :
2010-02-18 13:44:47 |
By :
kiko |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
'page 1
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Session.Contents("TEST") = TextBox1.Text
Response.Redirect("Default2.aspx")
End Sub
'page 2
Dim a As String = Session.Contents("TEST")
dl1.Items.Clear()
dl1.Items.Add(a)
dl1.SelectedIndex = 0
ลองเปลี่ยนเป็น c# ดูนะคะ
|
 |
 |
 |
 |
Date :
2010-02-18 14:01:26 |
By :
njnight |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าจะไม่ให้ error ตรงทำคล้ายๆ แบบนี้
Code (C#)
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = Session["UserNameCustomer1"].ToString();
TextBox2.Text = Session["Password1"].ToString();
TextBox3.Text = Session["FirstName1"].ToString();
TextBox4.Text = Session["LastName1"].ToString();
DropDownList8.DataSource = ?????; //<-- อะไรที่คุณว่าเป็น datasource ของ dropdownlist นั่นน่ะ
DropDownList8.DataTextField = "?????";
DropDownList8.DataValueField = "?????";
DropDownList8.DataBind();
foreach (ListItem item in DropDownList8.Items)
{
if (item.Value == Session["Nationality1"].ToString())
{
item.Selected = true;
}
}
}
|
 |
 |
 |
 |
Date :
2010-02-18 14:19:54 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
for (int i = 0; i < DropDownList8.Items.Count; i++ )
{
if (DropDownList8.Items[i].ToString() == Session["Nationality1"].ToString())
{
DropDownList8.SelectedItem = DropDownList8.Items[i];
}
}
ผมว่า ListItem คุณไม่มีครับ เพราะ class นี้ ไม่มีใน default ของ .net ต้องมีการสร้างไว้เอง ถ้ามีจริงๆ ผมขอ class นี้บ้างนะครับ กำลังอยากศึกษา
|
 |
 |
 |
 |
Date :
2010-02-18 14:34:29 |
By :
numenoy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เชื่อม ต่อกับ datasource ตรงหน้า interface ที่มี click choose datasource อะคับ
|
 |
 |
 |
 |
Date :
2010-02-18 14:46:22 |
By :
kiko |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ คุณ tungman ผมไม่ทันเห็นการใช้ datasource แบบนี้ ปกติจะ coding เองหมด ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2010-02-18 15:56:13 |
By :
numenoy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอโทษเช่นกันครับ ลืมไปว่ามัน bind ได้ที่หน้า design ด้วย เพราะปกติจะกำหนด property
ให้ controls ต่างๆ ด้วย dynamic code ส่วนหน้า design ใช้จัด layout อย่างเดียว
|
 |
 |
 |
 |
Date :
2010-02-18 16:11:20 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตกลง ยังไงอะครับ
ผม งงๆ
พี่ช่วย พิพใหม่หน่อยครับ
|
 |
 |
 |
 |
Date :
2010-02-19 10:35:20 |
By :
kiko |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังไม่ได้อีกเหรอ นี่ง่ายๆ เลยนะ
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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//สร้าง SqlDataSource เพื่อ Bind ให้ DropDownList1
string SqlConnectionString = "Your connection string.";
string SqlCommandString = "Select [ClassID], [ClassName] From [Global_UserClass]";
SqlDataSource DataSource = new SqlDataSource(SqlConnectionString, SqlCommandString);
//Bind data ให้ DropDownList1
DropDownList1.DataSource = DataSource;
DropDownList1.DataTextField = "ClassName";
DropDownList1.DataValueField = "ClassID";
DropDownList1.DataBind();
//ให้ ListItem ที่มี Value = 5 เป็น Selected Item
foreach (ListItem item in DropDownList1.Items)
{
if (item.Value == "5")
item.Selected = true;
}
}
}
|
 |
 |
 |
 |
Date :
2010-02-19 11:41:41 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณ คุณ tungman มากๆเลยนะครับ
ตอบให้ผมเยอะมากเลย ^^
|
 |
 |
 |
 |
Date :
2010-02-19 20:19:14 |
By :
kiko |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|