如何将JavaScript添加到Gridview的ASP.NET下拉列表(模板已归档)?

时间:2012-06-18 09:56:30

标签: javascript asp.net javascript-events

如何在ASP.NET下拉列表中添加JavaScript,将该下拉列表ID传递给相同的JavaScript函数。如何对Dropdownlist执行相同操作,这是Gridview的模板字段。

2 个答案:

答案 0 :(得分:1)

如果您不需要来自数据库/服务器端的任何内容,可以在下拉列表中添加onchange事件

<ItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" onchange="BindEng(this);" Height="16px" Width="179px"> <ItemTemplate>

将RowDataBound事件添加到gridview并访问每行的下拉列表并为其分配javascript,如果javascript函数需要,您可以在此处访问数据源的数据。

protected void GrdViewUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList ddl = e.Row.FindControl("YourDropDownListID") as DropDownList;
        //The statment below will pass the client id of drop down to function in javascript
        ddl.Attributes.Add("onchange", "YourjavascriptMethodCall('" + ddl.ClientID + "'");
    }
}

答案 1 :(得分:0)

将javacript添加到下拉列表并传递所选值 - &gt; ddl.Attributes.Add(“onchange”,“BindEng('”+ ddl.SelectedValue +“');”);