嗨我想在我的asp代碼中使用更多的java腳本,jQuery,ajax。所以對於一個項目,我使用java-script構建了這個字段選擇器。 使用ViewState與JavaScript更改控制
<asp:ListBox ID="lb1" runat="server" Height="400px" Width="170px"></asp:ListBox>
<input type="button" id="_b_Add" value="Add >" onclick="add();" style="padding:5px; margin-left:5px; margin-top:15px; width: 131px;" />
<input type="button" id="_b_Rem" value="< Remove" onclick="remove();" style="padding:5px; margin-top: -25px; margin-left:5px; width: 131px;" />
<input type="button" value="Up" onclick="moveUp(document.getElementById('lb2'));" style="padding:5px; margin-left:5px; width: 131px;" />
<input type="button" value="Down" onclick="moveDown();" style="padding:5px; margin-left:5px; margin-top: -25px; width: 131px;" />
<asp:Button ID="_b_Reset" runat="server" Text="Reset" Width="131px"
style="padding:5px; margin-left:5px; margin-top:40px;"
onclick="_b_Reset_Click" />
<asp:Button ID="_b_Run" runat="server" Text="Run" Width="131px"
style="padding:5px; margin-left:5px; margin-top:10px;"
onclick="_b_Run_Click" />
<asp:ListBox ID="lb2" runat="server" Height="400px" Width="170px" AppendDataBoundItems="True" Enabled="False"></asp:ListBox>
,這裏是重要的javascript:
function prom() { { $.prompt('Maximum Number of Custom Types is 14 '); } }
function add() {
var opt = document.createElement("option");
var sel = document.getElementById("lb1").selectedIndex;
if ((document.getElementById("lb1").selectedIndex) != -1 && document.getElementById("lb2").length < 14) {
document.getElementById("lb2").options.add(opt);
opt.text = (document.getElementById("lb1").options[document.getElementById("lb1").selectedIndex].text);
opt.value = (document.getElementById("lb1").value);
document.getElementById("lb1").options.remove((document.getElementById("lb1").selectedIndex));
document.getElementById("lb1").options.SelectedIndex = sel;
}
else {
prom();
}
}
function remove() {
var opt = document.createElement("option");
var sel = document.getElementById("lb2").selectedIndex;
if ((document.getElementById("lb2").selectedIndex) != -1) {
document.getElementById("lb1").options.add(opt);
opt.text = (document.getElementById("lb2").options[document.getElementById("lb2").selectedIndex].text);
opt.value = (document.getElementById("lb2").value);
}
document.getElementById("lb2").options.remove((document.getElementById("lb2").selectedIndex));
document.getElementById("lb2").options.selectedIndex = sel;
}
編輯***
在LB2
foreach (var item in lb2.Items.Cast<ListItem>())
{
TextBox1.Text += (item.Value).ToString();
i++;
}
測試值也能正常工作的LB1,但不當項目已被添加客戶端它不好。
基本上現在運行lb2的值應該被迭代並饋入我的報告類。不過,我很快意識到,在觸發「_b_Run_Click」事件時,Java腳本對lb2所做的更改不會反映在視圖狀態中。所以經過深入挖掘,我似乎覺得解決方案是使用隱藏字段......我的問題是(作爲js noob)你究竟做了什麼,是否有更優雅的選擇?
感謝
你的問題無關,與C# – jeroenh
的ViewState應該捕捉到控制方面取得了使用JavaScript的任何變化,回發過程的一部分保存控制狀態。 –
那麼,當第二個列表框填充項目,然後我嘗試循環在文本框中的項目沒有項目? http://www.aspanswers.com/general/Q_25539345-Javascript-changes-to-Listbox-not-working-on-Postback.jsp –