开发者

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>
        <as开发者_开发问答p: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.


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"


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.


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜