Nir
Nir

Reputation: 2629

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

Upvotes: 1

Views: 2445

Answers (5)

Leah
Leah

Reputation: 2607

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.

Upvotes: 1

codeandcloud
codeandcloud

Reputation: 55200

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"

Upvotes: 2

platon
platon

Reputation: 5340

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>

Upvotes: 0

biluriuday
biluriuday

Reputation: 428

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

Upvotes: 0

Dustin Hodges
Dustin Hodges

Reputation: 4195

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

Upvotes: 0

Related Questions