locked
get elementbyid by C# RRS feed

  • Question

  • User1741069310 posted

    Hi,

    Is there a way to get select object by C#?

    <select id="LoESelect" name="LoESelect">
    <option></option>
    </select><br/>

    Tuesday, June 5, 2018 6:21 AM

Answers

  • User-369506445 posted

    hi

    if you use web from , you can define runat tag for it below like :

    <form id="form1" runat="server">
            <select id="LoESelect" name="LoESelect" runat="server">
                <option>test1</option>
                <option>test2</option>
            </select><br />
            
            
            <asp:Button runat="server" Text="submit" OnClick="Submit_Click"  />
        </form>

    and in code behind

     protected void Submit_Click(object sender, EventArgs e)
            {
                var index = LoESelect.SelectedIndex;
    
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 5, 2018 6:42 AM
  • User61956409 posted

    Hi fsze88,

    Firstly, as vahid bakkhi mentioned, you can set <select> DropDownList with runat=”server” property, and get selected text from its Items collection via the SelectedIndex property.

    sval = LoESelect.Items[LoESelect.SelectedIndex].Text;

    Secondly, you can also use Request Form collection and name property to access the HTML Select DropDownList selected Text from code behind.

    sval  = Request.Form["LoESelect"];

    With Regards, 

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 6, 2018 9:42 AM

All replies

  • User-369506445 posted

    hi

    if you use web from , you can define runat tag for it below like :

    <form id="form1" runat="server">
            <select id="LoESelect" name="LoESelect" runat="server">
                <option>test1</option>
                <option>test2</option>
            </select><br />
            
            
            <asp:Button runat="server" Text="submit" OnClick="Submit_Click"  />
        </form>

    and in code behind

     protected void Submit_Click(object sender, EventArgs e)
            {
                var index = LoESelect.SelectedIndex;
    
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 5, 2018 6:42 AM
  • User61956409 posted

    Hi fsze88,

    Firstly, as vahid bakkhi mentioned, you can set <select> DropDownList with runat=”server” property, and get selected text from its Items collection via the SelectedIndex property.

    sval = LoESelect.Items[LoESelect.SelectedIndex].Text;

    Secondly, you can also use Request Form collection and name property to access the HTML Select DropDownList selected Text from code behind.

    sval  = Request.Form["LoESelect"];

    With Regards, 

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 6, 2018 9:42 AM