【问题标题】:GetElementByID at server side, asp.net?服务器端的GetElementByID,asp.net?
【发布时间】:2011-07-08 14:40:40
【问题描述】:

我有类似的东西:

  <asp:ListView ID="lvList" runat="server">
    <LayoutTemplate>
      <select id="select_list">
        <option value="-1">
          select one
        </option>
        <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
      </select>
    </LayoutTemplate>
    <ItemTemplate>
      <option value="<%# Eval("code") %>">
        <%# Eval("Name") %>
      </option>
    </ItemTemplate>
  </asp:ListView>

我想在提交按钮后在服务器端访问select_list.. 我试过FindControl("select_list")lvList.FindControl("select_list")Request.Form["select_list"] - 他们都没有把控制权交还给我..

有没有办法通过它的 id 来获取控件,就像 JS getElementByID 一样?

谢谢。

【问题讨论】:

    标签: asp.net client-side-data server-side-controls


    【解决方案1】:

    这是为了学术目的吗?您可以使用 asp:DropDownList 编写具有较少标记的相同代码

    <asp:DropDownList ID="select_list" runat="server"
                AppendDataBoundItems="true"
                DataTextField="Name"
                DataValueField="code">
        <asp:ListItem Text="select one" Value="-1" />
    </asp:DropDownList>
    

    如果您特别喜欢使用 ListView,请在服务器 runat="server" 上运行您的 HTML 控件

    【讨论】:

      【解决方案2】:

      您使用ListView 来填充HTML select 而不仅仅是使用DropDownList 是否有原因?

      您可以将整个 ListView 替换为 DropDownList,如下所示:

      <asp:DropDownList ID="SampleDdl" runat="server" AppendDataBoundItems="true">
          <asp:ListItem Text="Select one" Value="-1" />
      </asp:DropDownList>
      

      然后,在您后面的代码中,您可以像这样绑定DropDownList

      SampleDdl.DataSource = DataSet
      SampleDdl.DataValueField = "Code"
      SampleDdl.DataTextField = "Name"
      SampleDdl.DataBind()
      

      这将自动为您填充DropDownList。指定DataValueField 将自动填充DropDownList 的所有选项中的Value 属性。同样,DataTextField 将填充 Text 属性。

      还需要注意的是,我在上面的示例中添加了AppendDataBoundItems="true" - 您需要添加它,以便“选择一个”的默认选项不会被绑定到控件的数据替换- 而是将绑定数据附加在现有选项之后。

      如果您使用DropDownList,则可以通过直接引用SampleDdl 来访问代码隐藏中的控件。

      【讨论】:

        【解决方案3】:

        为了让控件拥有自己的服务器表示,您必须使用属性 runat="server" 来声明它

        试试

        <asp:ListView ID="lvList" runat="server">
        <LayoutTemplate>
          <select id="select_list" runat="server">
            <option value="-1">
              select one
            </option>
            <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
          </select>
        </LayoutTemplate>
        <ItemTemplate>
          <option value="<%# Eval("code") %>">
            <%# Eval("Name") %>
          </option>
        </ItemTemplate>
        

        然后尝试使用 FindControl("select_list")

        【讨论】:

          【解决方案4】:

          您尝试访问的控件是客户端控件。如果您想在服务器端访问它,请尝试添加类似 runat="server" 的标签。类似的东西

          <select id="..." runat="server">
          

          【讨论】:

            【解决方案5】:

            你应该将它的runat属性设置为“server”并使用ListView的LayoutTemplate属性来获取它。

            <asp:ListView ID="lvList" runat="server">
                <LayoutTemplate>
                  <select id="select_list" runat="server">
                    <option value="-1">
                      select one
                    </option>
                    <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
                  </select>
                </LayoutTemplate>
                <ItemTemplate>
                  <option value="<%# Eval("code") %>">
                    <%# Eval("Name") %>
                  </option>
                </ItemTemplate>
              </asp:ListView>
            

            【讨论】:

              猜你喜欢
              • 2011-05-16
              • 2013-10-10
              • 2011-01-01
              • 1970-01-01
              • 2012-12-19
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多