asp:ListBox SelectedValue未设置(动态或单击时)

时间:2012-08-31 19:06:42

标签: asp.net vb.net list

我有一个asp:ListBox,它是根据另一个asp:List框的选定值从js动态填充的。问题是第二个列表框总是返回SelectedValue为“”,无论我是否设置lstBox.selectedIndex = 0或实际选择列表中的项目。

.js添加到列表然后设置默认选定项

var Option = document.createElement("option");
lstId = document.getElementById(lstId);
Option.text = lstItem;
lstId.add(Option);    
lstId.selectedIndex = 0;

.vb获取选定值

Dim selSchedule As String = lstCRMSched.SelectedValue

现在由于这个列表是由javascript填充的,我必须设置我的@page EnableEventValidation =“false”否则后来发布的回发会失败。

旁注:我注意到当你使用隐藏的div作为基于菜单选择取消隐藏的叠加时,asp.net不喜欢它,因为它所做的一切都需要回发,擦除超出其他div的状态。我应该只为每个div分配10个.aspx文件,只使用会话切换代码隐藏的位置,以传输选定值和将在另一个div中显示的数据等内容吗?

2 个答案:

答案 0 :(得分:2)

您可以通过SelectedValue对象访问下拉列表的Request,因为表单中的每个元素都已在请求中提交。

您只需要这样做:

Dim selSchedule As String = Request[lstCRMSched.UniqueID]

现在,这只会因为您在页面上禁用了EventValidation而起作用。你得到的错误是完全正常的。 ASP.NET只是确保没有人发送最初未由服务器呈现的数据以防止攻击。如果您要在页面上启用EventValidation,则需要通过ClientScriptManager.RegisterForEventValidation

注册验证列表

答案 1 :(得分:1)

如果您添加商品到客户的下拉列表,这些商品不会在服务器上保留

但您可以尝试保存动态添加(文本值对) 隐藏的输入字段并在服务器上解析它们。有关工作示例,请参阅此link。对于您的示例,您还必须在另一个隐藏字段保存您的 selectedIndex ,以便能够在服务器上访问它。

修改

<强> demo.aspx

<script type="text/javascript">
function saveValue() {
    var hiddenField1 = document.getElementById("hiddenField1");
    hiddenField1.value = "hello world";
}
</script>
<form id="Form1" method="post" runat="server">
    <input type="hidden" id="hiddenField1" name="hiddenField1" value="" />
    <asp:Button ID="btnPostBack" runat="server" Text="PostBack" 
        OnClientClick="saveValue()" onclick="btnPostBack_Click" />
</form>

<强> demo.aspx.cs

protected void btnPostBack_Click(object sender, EventArgs e)
{
    Debug.WriteLine("Value of hiddenField1: " + Request["hiddenField1"]);
    Debugger.Break();
}

这个对我有用。我在服务器上得到"hello world"

编辑2

Icarus通过引用Request对象指出你总能访问任何提交的元素到服务器,当然他绝对正确!根据您的问题,我认为您希望访问 所有动态创建的项目 - 这就是 - 使用下面显示的解决方案 - 不可能

<强> aspxPage

<script type="text/javascript">
    function saveToList() {
        var ListBox1 = document.getElementById("ListBox1");
        var ListBox2 = document.getElementById("ListBox2");
        var Option = document.createElement("option");
        Option.text = ListBox1.options[ListBox1.selectedIndex].text;
        ListBox2.add(Option);
        ListBox2.selectedIndex = 0;
    }
</script>
<form id="Form1" method="post" runat="server">
<asp:ListBox ID="ListBox1" runat="server">
    <asp:ListItem Text="entry1" Value="1" />
    <asp:ListItem Text="entry2" Value="2" />
    <asp:ListItem Text="entry3" Value="3" />
    <asp:ListItem Text="entry4" Value="4" />
</asp:ListBox>
<asp:ListBox ID="ListBox2" runat="server" Width="100"></asp:ListBox>
<input id="Button1" type="button" value="Save to List" onclick="saveToList()" />

<asp:Button ID="btnPostBack" runat="server" Text="PostBack" OnClick="btnPostBack_Click" />

<强>代码隐藏

protected void btnPostBack_Click(object sender, EventArgs e)
{
    Debug.WriteLine("SelectedValue of ListBox2: " + Request["ListBox2"]);
    // no access to all other clientSide created items in ListBox2
    Debugger.Break();
}