GetElementByID at server side, asp.net?

2.3k Views Asked by At

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.

5

There are 5 best solutions below

0
On BEST ANSWER

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"

0
On

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

0
On

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

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
On

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.