<html>
<head>
<script language="VB" runat="server">
public Sub Page_Load(Sender As Object,e As EventArgs)
Dim scriptString As String = "<script language=JavaScript> function DoClick(n) {"
scriptString += "alert(n);"
scriptString += "span1.innerHTML=Item +n;"
scriptString += "document.all(""DropDown1"").value = document.all(""span1"").innerText;"
scriptString += "alert(span1.innerHTML);}<"
scriptString += "/"
scriptString += "script>"
If(Not Me.IsStartupScriptRegistered("Startup")) Then
Me.RegisterStartupScript("Startup", scriptString)
End If
Bind()
End Sub
Sub SubmitBtn_Click(sender As Object, e As EventArgs)
Label1.Text="You chose: " + DropDown1.SelectedItem.Text
End Sub
Sub Bind()
Response.Write(DropDown1.SelectedItem.Text)
End Sub
Sub OnIDChanged(sender As Object, e As EventArgs)
Response.Write(DropDown1.SelectedItem.Text)
End Sub
</script>
</head>
<body>
<h3><font face="Verdana">DropDownList Example</font></h3>
<input type="text" id="txt" value="0" runat="server"/>
<form runat=server>
<asp:DropDownList id=DropDown1 runat="server" AutoPostBack ="True" OnSelectedIndexChanged="OnIDChanged">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</asp:DropDownList>
<asp:button text="Submit" OnClick="SubmitBtn_Click" runat=server/>
<p>
<asp:Label id=Label1 font-name="Verdana" font-size="10pt" runat="server">
Select a value from the list
</asp:Label>
<br>
<%
dim i
for i=1 to 6%>
<input type="button" value="Item <%=i%>" onclick="DoClick(<%=i%>)">
<%next%>
<br>
<span id="span1" runat="server">0</span>
</form>
</body>
</html>
上面的程序怎样才能作到点击button 将<span>上的值传给dropdownlist的SelectedIndex,现在<span>可以正确显示值,但是不能传给dropdownlist的SelectedIndex,可以用js传value值,但是SelectedIndexChanged事件用不了, 为什么
Test it:
<html>
<head>
<script language="VB" runat="server">
public Sub Page_Load(Sender As Object,e As EventArgs)
Dim scriptString As String = "<script language=JavaScript> function DoClick(n) {"
scriptString += "alert(n);"
scriptString += "span1.innerHTML=Item +n;"
scriptString += "document.all(""DropDown1"").value = document.all(""span1"").innerText;"
scriptString += "alert(span1.innerHTML);}<"
scriptString += "/"
scriptString += "script>"
If(Not Me.IsStartupScriptRegistered("Startup")) Then
Me.RegisterStartupScript("Startup", scriptString)
End If
if not me.ispostback() then
Bind()
end if
End Sub
Sub SubmitBtn_Click(sender As Object, e As EventArgs)
Label1.Text="You chose: " + DropDown1.SelectedItem.Text
End Sub
Sub Bind()
Response.Write(DropDown1.SelectedItem.Text)
End Sub
Sub OnIDChanged(sender As Object, e As EventArgs)
Response.Write(DropDown1.SelectedItem.Text)
End Sub
</script>
</head>
<body>
<h3><font face="Verdana">DropDownList Example</font></h3>
<input type="text" id="txt" value="0" runat="server" NAME="txt" />
<form runat="server" ID="Form1">
<asp:DropDownList id="DropDown1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="OnIDChanged">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</asp:DropDownList>
<asp:button text="Submit" OnClick="SubmitBtn_Click" runat="server" ID="Button1" />
<p>
<asp:Label id="Label1" font-name="Verdana" font-size="10pt" runat="server">
Select a value from the list
</asp:Label>
<br>
<%
dim i
for i=1 to 6%>
<input type="button" value="Item <%=i%>" onclick="DoClick(<%=i%>)">
<%next%>
<br>
<span id="span1" runat="server">0</span>
</form>
</body>
</html>
the SelectedIndexChanged is triggered if you click on Submit button, if you want to trigger it right away, according to the documentation:
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/events/onchange.asp
"
....
The onchange event does not fire when the selected option of the select object is changed programatically
..."
so after you change the value, you should call onchange() or call form.submit() yourself, for example
scriptString += "alert(span1.innerHTML);"
scriptString +="document.all(""DropDown1"").onchange();"
scriptString += "}</"
scriptString += "script>"