简体   繁体   English

GetElementByID 在服务器端,asp.net?

[英]GetElementByID at server side, asp.net?

I have something like that:我有类似的东西:

  <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>

And I want to access select_list at server side, after a button get submitted.. I tried FindControl("select_list") , lvList.FindControl("select_list") , Request.Form["select_list"] - none of them gave my the control back..而且我想在提交按钮后访问服务器端的select_list 。我尝试了FindControl("select_list")lvList.FindControl("select_list")Request.Form["select_list"] - 他们都没有给我控制回来。。

Is there some way to get the control by its id, just like JS getElementByID ?有没有办法通过它的 id 来获取控件,就像 JS getElementByID一样?

Thanks.谢谢。

Is this for academic purpose?这是为了学术目的吗? You could write the same code with lesser markup using an asp:DropDownList您可以使用asp:DropDownList编写具有较少标记的相同代码

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

If you are particular about using ListView do run your HTML Control at server runat="server"如果您特别喜欢使用 ListView,请在服务器runat="server"上运行您的 HTML Control

Is there a reason you are using a ListView to fill a HTML select rather than just using a DropDownList ?您是否有理由使用ListView来填充 HTML select而不仅仅是使用DropDownList

You can just replace the entire ListView with a DropDownList like so:您可以像这样用DropDownList替换整个ListView

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

Then, in your code behind, you can just bind the DropDownList like so:然后,在你后面的代码中,你可以像这样绑定DropDownList

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

This will automatically populate the DropDownList for you.这将自动为您填充DropDownList Specifying the DataValueField will automatically populate the Value attributes in all the options of the DropDownList .指定DataValueField将自动填充DropDownList的所有选项中的Value属性。 Similarly, the DataTextField will populate the Text attributes.同样, DataTextField将填充Text属性。

It's also important to note that I've added AppendDataBoundItems="true" in my sample above - you will need to add that so that the default option of "Select one" isn't replaced by the data that is bound to the control - instead the bound data is appended after the existing option.同样重要的是要注意我在上面的示例中添加了AppendDataBoundItems="true" - 您需要添加它,以便“选择一个”的默认选项不会被绑定到控件的数据替换 -而是将绑定数据附加在现有选项之后。

If you use a DropDownList , you can then just access the control in your code-behind by directly referring to SampleDdl .如果您使用DropDownList ,则可以通过直接引用SampleDdl来访问代码隐藏中的控件。

In order for a control to have a server representation of itself you have to declare it with the attribute runat="server"为了使控件具有自身的服务器表示,您必须使用属性 runat="server" 声明它

Try尝试

<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>

and then try accessing using FindControl("select_list")然后尝试使用 FindControl("select_list") 访问

the control you are trying to access is client side control.您尝试访问的控件是客户端控件。 If you want to access it server side try adding a tag like runat="server".如果您想在服务器端访问它,请尝试添加类似 runat="server" 的标签。 Something like就像是

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

You should set its runat attribute to "server" and use the ListView's LayoutTemplate property to obtain it.您应该将其 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM