Olen tehnyt VS2005 C#:lla ASP.Net ohjelman jossa näytöllä on useita syöttökenttiä. Edelliseen kenttään annettu arvo pitäisi vaikuttaa seuraavan combobox:n sisältöön. Olen asettanut syöttökentälle autopostpack:n päälle. Mutta miten saan päivitettyä datan näkymään combobox:iin? Vai onnistuuko tämä ilman erillistä painiketta näytöllä?
Moi atl!
javascriptillä onnistuu
<%@ 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>
<script language="JavaScript">
document.onkeyup = function ()
{
var text = document.activeElement.value;
var dropDownBox;
if (document.activeElement.name == "TextBox1")
{
dropDownBox = document.getElementById('DropDownList1');
switch (text)
{
case "teksti1": //esim.
dropDownBox.selectedIndex = 1;
break;
case "teksti2":
dropDownBox.selectedIndex = 2;
break;
case "teksti3":
dropDownBox.selectedIndex = 3;
break;
case "teksti4":
dropDownBox.selectedIndex = 4;
break;
//case jne...
default:
dropDownBox.selectedIndex = 0;
break;
}
}
/*if (document.activeElement.name == "TextBox2")
{
dropDownBox = document.getElementById('DropDownList2');
switch (text)
{
case jne...
}
}*/
//jne.
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<p>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br /><br />
<asp:DropDownList ID="DropDownList1" runat="server" Height="26px" Width="129px">
<asp:ListItem></asp:ListItem>
<asp:ListItem>valinta1</asp:ListItem>
<asp:ListItem>valinta2</asp:ListItem>
<asp:ListItem>valinta3</asp:ListItem>
<asp:ListItem>valinta4</asp:ListItem>
</asp:DropDownList>
</p>
</form>
</body>
</html>Moi taas atl!
mikäli haluat 'piilottaa' scriptisi niin...
Default.aspx:
<%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" %> "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> "http://www.w3.org/1999/xhtml"> "server"><form id="form1" runat="server"> <p> <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox> <br /><br /> <asp:DropDownList ID="DropDownList1" runat="server" Height="26px" Width="129px"> <asp:ListItem></asp:ListItem> <asp:ListItem>valinta1</asp:ListItem> <asp:ListItem>valinta2</asp:ListItem> <asp:ListItem>valinta3</asp:ListItem> <asp:ListItem>valinta4</asp:ListItem> </asp:DropDownList> </p> </form>
Default.aspx.cs
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (!ClientScript.IsStartupScriptRegistered("theScript"))
{
FileStream fs = File.OpenRead(
Server.MapPath("Bin/source.txt"));
byte[] buffer = new byte[1025];
string myscript = String.Empty;
UTF8Encoding enc = new UTF8Encoding(false);
while (fs.Read(buffer, 0, buffer.Length) > 0)
{
myscript += enc.GetString(buffer);
}
fs.Close();
ClientScript.RegisterClientScriptBlock(
typeof(string), "theScript", myscript);
}
}
}
}source.txt:
<script language="javascript">
document.onkeyup = function ()
{
var text = document.activeElement.value;
var dropDownBox;
if (document.activeElement.name == "TextBox1")
{
dropDownBox = document.getElementById('DropDownList1');
switch (text)
{
case "teksti1": //esim.
dropDownBox.selectedIndex = 1;
break;
case "teksti2":
dropDownBox.selectedIndex = 2;
break;
case "teksti3":
dropDownBox.selectedIndex = 3;
break;
case "teksti4":
dropDownBox.selectedIndex = 4;
break;
//case jne.
default:
dropDownBox.selectedIndex = 0;
break;
}
}
}
</script>Kiitos Nea!
Java scriptillä taitaa onnistua moni muukin asia! Täytyypä alkaa opiskelemaan...
-atl-
Aihe on jo aika vanha, joten et voi enää vastata siihen.