服务器端的 GetElementByID,asp.net?

发布于 2024-11-19 06:11:39 字数 815 浏览 3 评论 0原文

我有类似的事情:

  <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 一样?

谢谢。

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

Is there some way to get the control by its id, just like JS getElementByID ?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淡写薰衣草的香 2024-11-26 06:11:39

这是出于学术目的吗?您可以使用 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 控件

Is this for academic purpose? You could write the same code with lesser markup using an 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"

残花月 2024-11-26 06:11:39

您使用 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 即可访问代码隐藏中的控件。

Is there a reason you are using a ListView to fill a HTML select rather than just using a DropDownList?

You can just replace the entire ListView with a DropDownList like so:

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

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

This will automatically populate the DropDownList for you. Specifying the DataValueField will automatically populate the Value attributes in all the options of the DropDownList. Similarly, the DataTextField will populate the Text attributes.

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.

If you use a DropDownList, you can then just access the control in your code-behind by directly referring to SampleDdl.

绝情姑娘 2024-11-26 06:11:39

为了使控件具有其自身的服务器表示形式,您必须使用属性 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(“选择列表”)

In order for a control to have a server representation of itself you have to declare it with the attribute 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")

淡淡の花香 2024-11-26 06:11:39

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

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

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". Something like

<select id="..." runat="server">
↘人皮目录ツ 2024-11-26 06:11:39

您应该将其 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>

You should set its runat attribute to "server" and use the ListView's LayoutTemplate property to obtain it.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文