服务器端的GetElementByID,asp.net?
我有这样的想法:服务器端的GetElementByID,asp.net?,asp.net,client-side-data,server-side-controls,Asp.net,Client Side Data,Server Side Controls,我有这样的想法: <asp:ListView ID="lvList" runat="server"> <LayoutTemplate> <select id="select_list"> <option value="-1"> select one </option> <asp:PlaceHolder ID="itemPlaceHolder
<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\u list
。。
我尝试了FindControl(“选择列表”)
,lvList.FindControl(“选择列表”)
,Request.Form[“选择列表”]
-他们都没有把控制权还给我
是否有某种方法可以通过id获取控件,就像JSgetElementByID
谢谢。为了使控件具有自身的服务器表示,您必须使用属性runat=“server”来声明它 试一试
选择一个
然后尝试使用
FindControl(“选择列表”)您试图访问的控件是客户端控件。如果您想在服务器端访问它,请尝试添加类似runat=“server”的标记。差不多
<select id="..." runat="server">
您应该将其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>
选择一个
这是为了学术目的吗?您可以使用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,请在服务器上运行HTML控件
runat=“server”
是否有理由使用ListView
来填充HTMLselect
,而不只是使用DropDownList
您只需将整个列表视图
替换为下拉列表
,如下所示:
<asp:DropDownList ID="SampleDdl" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="Select one" Value="-1" />
</asp:DropDownList>
SampleDdl.DataSource = DataSet
SampleDdl.DataValueField = "Code"
SampleDdl.DataTextField = "Name"
SampleDdl.DataBind()
这将自动为您填充DropDownList
。指定数据值字段
将自动填充下拉列表
所有选项中的值
属性。类似地,DataTextField
将填充Text
属性
还需要注意的是,我已经在上面的示例中添加了AppendDataBoundItems=“true”
——您需要添加它,以便“选择一个”的默认选项不会被绑定到控件的数据替换,而是将绑定的数据追加到现有选项之后
如果使用DropDownList
,则可以直接参考SampleDdl
访问代码隐藏中的控件
SampleDdl.DataSource = DataSet
SampleDdl.DataValueField = "Code"
SampleDdl.DataTextField = "Name"
SampleDdl.DataBind()