asp.net – 使用codebehind显示隐藏div

我有一个DropDownList,我试图显示一个div OnSelectedindexChanged,但它表示需要OBJECT。

我绑定在该div中的DataList:

ASPX:

<asp:DropDownList runat="server" ID="lstFilePrefix1" AutopostBack="True" 
                  OnSelectedindexChanged="DropDownList1_SelectedindexChanged" >
  <asp:ListItem Text="Prefix1" Value="Prefix1" />
  <asp:ListItem Text="Prefix2" Value="Prefix2" />
  <asp:ListItem Text="Prefix3" Value="Prefix3" />
  <asp:ListItem Text="Prefix1 and Prefix2" Value="Prefix1 and Prefix2" />
  <asp:ListItem Text="Prefix2 and Prefix3" Value="Prefix2 and Prefix3" />
</asp:DropDownList>

<asp:DataList ID="DataList1" runat="server" RepeatColumns="4"  
              CssClass="datalist1"  OnItemDataBound="SOMENAMEItemBound"
              CellSpacing="6" onselectedindexchanged="DataList1_SelectedindexChanged" 
              HorizontalAlign="Center" Width="500px">

代码背后:

protected void DropDownList1_SelectedindexChanged(object sender,EventArgs e)
{
    if (lstFilePrefix1.SelectedItem.Text=="Prefix2")
    {
        int TotalRows = this.BindList(1);
        this.Prepare_Pager(TotalRows);
        Page.ClientScript.RegisterClientScriptBlock(GetType(),"JScript1","ShowDiv('data');",true);
    }
}

JavaScript的:

function ShowDiv(obj)
{
     var dataDiv = document.getElementById(obj);
     dataDiv.style.display = "block";
}

我究竟做错了什么?

解决方法

做div
runat="server"

和做

if (lstFilePrefix1.SelectedValue=="Prefix2")
{
    int TotalRows = this.BindList(1);
    this.Prepare_Pager(TotalRows);
    data.Style["display"] = "block";
}

您的方法不工作,因为在渲染div之前,JavaScript正在渲染在body标签的顶部。您必须包含代码来告诉JavaScript等待DOM完全准备好接受您的请求,这可能是最简单的jQuery。

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...