質問者
JavaScriptで<OPTION>を追加したDLLのValue(及びにText)の取得について

質問
-
お世話になります。
只今、ASP.NET(C#)で以下の処理を実装しています。
1.Page_Load(Is Not PostBack)でDropDownListに基本値をバインドする。
2.クライアントスクリプト(JavaScript)でDropDownListにアイテム(<OPTION>)を追加する。
3.追加したアイテムを選択し、Buttonのクリックイベントで選択されたValueとTextを取得する。
結果は残念ながらクライアントスクリプトで追加したアイテムの内容は取得できずでした・・・未選択状態でした。
DropDownListのItemsコレクションには追加したアイテムが存在しませんでした。
※HtmlSelectコントロールを使用しても同様の結果でした。
初期にサーバーサイドで追加したアイテムを選択した場合は問題なく取得できます。
Request.Form["DropDownList名"]ではValueは取得できますがTextも取得したいので解決とは至りません。
HiddunとOnChangeなどを使用して実装する方法しか解決方法は無いのでしょうか?
ご教授のほどよろしくお願い致します。
以下のサイトの掲示板を参考にしましたが、解決には至っていないようでしたので書き込んだ次第です。
http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=15171&forum=7
すべての返信
-
返信ありがとう御座います。
Hidden + onChangeで対応し追加内容をサーバー側で反映することが出来ました。
また1つ似たような(別スレ?)質問なのですが・・・
DataGridのテンプレート列にHiddenコントロールを追加し、クライアントスクリプトでHidden値を変更してサーバー側でDataGridのHiddenコントロールの値を取得すると変更前の値が取得されてしまいます。
この現象も「クライアントスクリプトで追加したもの」にあたるのでしょうか?
それとも他に原因があるのでしょうか?
クライアントスクリプトは以下のように単純なものです。
function on_click(id){
document.forms[0](id).value = "1";
}
-
この手の処理はクライアント コールバック でやったほうがいいと思います。
参考までにサンプルを乗せますので、試してみてください。
-- 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>DropDownList Client CallBack サンプル</title>
<script language="javascript" type="text/javascript">
<!--
function addOption()
{
alert("コールバック");
var op = new Option(document.frmMain.tText.value, document.frmMain.tValue.value);
var len = document.frmMain.ddlClient.length;
document.frmMain.ddlClient[len] = op;
var data = document.frmMain.tText.value + "," + document.frmMain.tValue.value;
alert(data);
CallServer(data);
}
function ReciveData(args, context)
{
Results.innerText = args;
}
-->
</script>
</head>
<body>
<form id="frmMain" runat="server">
<div>
<span id="Results"></span>
<asp:DropDownList ID="ddlClient" runat="server" AppendDataBoundItems="True">
<asp:ListItem Value="0">へろー</asp:ListItem>
<asp:ListItem Value="1">もえぇ~</asp:ListItem>
</asp:DropDownList><input id="tValue" type="text" maxlength="5" size="5" /><input id="tText" type="text" maxlength="20" size="10" /><input
id="bAdd" type="button" value="追加" onclick=addOption();" />
</div>
</form>
</body>
</html>
-- Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
private string returnValue;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsCallback && !Page.IsPostBack)
{
// クライアント側コールバックスクリプト設定
ClientScriptManager clientScriptManager = (ClientScriptManager)Page.ClientScript;
string cbReference = clientScriptManager.GetCallbackEventReference(this, "arg", "ReciveData", "");
string callbackScript = "function CallServer(arg, context) {" + cbReference + "; }";
clientScriptManager.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true);
}
}
public string GetCallbackResult()
{
return this.returnValue;
}
public void RaiseCallbackEvent(string eventArgument)
{
string[] buffer = eventArgument.Split(',');
ListItem listItem = new ListItem(buffer[0], buffer[1]);
this.ddlClient.Items.Add(listItem);
this.ddlClient.DataBind();
this.returnValue = "更新";
}
}