服务器端的 GetElementByID,asp.net?
我有类似的事情:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是出于学术目的吗?您可以使用
asp:DropDownList
编写具有较少标记的相同代码。如果您特别喜欢使用 ListView,请在服务器
runat="server"
上运行您的 HTML 控件Is this for academic purpose? You could write the same code with lesser markup using an
asp:DropDownList
If you are particular about using ListView do run your HTML Control at server
runat="server"
您使用
ListView
来填充 HTMLselect
而不仅仅是使用DropDownList
是否有原因?您可以将整个
ListView
替换为DropDownList
,如下所示:然后,在后面的代码中,您可以像这样绑定
DropDownList
:这将自动为您填充
DropDownList
。指定DataValueField
将自动填充DropDownList
的所有选项中的Value
属性。同样,DataTextField
将填充Text
属性。还需要注意的是,我在上面的示例中添加了
AppendDataBoundItems="true"
- 您需要添加它,以便“选择一个”的默认选项不会被数据替换绑定到控件 - 相反,绑定数据附加在现有选项之后。如果您使用
DropDownList
,则只需直接引用SampleDdl
即可访问代码隐藏中的控件。Is there a reason you are using a
ListView
to fill a HTMLselect
rather than just using aDropDownList
?You can just replace the entire
ListView
with aDropDownList
like so:Then, in your code behind, you can just bind the
DropDownList
like so:This will automatically populate the
DropDownList
for you. Specifying theDataValueField
will automatically populate theValue
attributes in all the options of theDropDownList
. Similarly, theDataTextField
will populate theText
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 toSampleDdl
.为了使控件具有其自身的服务器表示形式,您必须使用属性 runat="server" 来声明它
,然后尝试
使用
FindControl(“选择列表”)
In order for a control to have a server representation of itself you have to declare it with the attribute runat="server"
Try
and then try accessing using
FindControl("select_list")
您尝试访问的控件是客户端控件。如果您想在服务器端访问它,请尝试添加类似 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
您应该将其 runat 属性设置为“server”并使用 ListView 的 LayoutTemplate 属性来获取它。
You should set its runat attribute to "server" and use the ListView's LayoutTemplate property to obtain it.