用户控件中向前台javascript函数进行传值?????
首先创建了一个用户控件如下:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<script language="javascript" type="text/javascript">
function value(s)
{
var selectObj=document.getElementById(s);
var selectOptionText= selectObj.options[selectObj.selectedIndex].innerText;
alert(selectOptionText);
}
</script>
<div id="div1">
<asp:DropDownList ID="abc" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<br />
</div>
后台代码为:
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
this.Parent.Page.
ClientScript.RegisterStartupScript(this.GetType(), "abc", "value(<%=abc.ClientID%>)", true);
}
}
创建了一个WEB页面.想通过点击Button1来显示从下拉列表中选择的文本内容.但在执行的时间出现"运行期间出现错误"是不在后台代码中出错的啊.高手帮助啊.
------解决方案--------------------
刚才错了,这样就可以了
C# code
protected void Button1_Click(object sender, EventArgs e)
{
this.Parent.Page.
ClientScript.RegisterStartupScript(this.GetType(), "abc", "value('"+abc.ClientID+"')", true);
}