简体   繁体   English

从C#代码获取HTML按钮/下拉文本

[英]Get html button/drop down text from c# code

I am wondering how I can grab the text from my HTML button/dropdown menu in c#? 我想知道如何从c#的HTML按钮/下拉菜单中提取文本? With the asp.net dropdownlist I was able to do string text = dropdownlist.Value; 使用asp.net dropdownlist,我能够执行字符串text = dropdownlist.Value;。 But it doesnt seem to be as simple, or maybe it is, with a different type of dropdown. 但这似乎不是那么简单,或者下拉菜单类型不同。 I couldnt use the asp drop down list because it didnt act as a button and a drop down menu. 我无法使用asp下拉列表,因为它没有充当按钮和下拉菜单。 The functions below are for changing the text from the default "Advanced" to the selected item text by the user. 以下功能用于由用户将文本从默认的“高级”更改为所选的项目文本。 After the user changes the buttons text, I want to grab that text and use it as a condition for a sqlbuild.where clause. 用户更改按钮文本之后,我想获取该文本并将其用作sqlbuild.where子句的条件。 Also the text of the updated button should remain after the search button is clicked. 单击搜索按钮后,更新后的按钮的文本也应保留。 With the asp drop down list the value remained after the refresh but for some reason when I search when the updated drop down menu, the change is wiped after selecting submit 使用asp下拉列表,刷新后仍保留该值,但是由于某些原因,当我搜索更新的下拉菜单时,选择“提交”后将擦除更改

Something like this is what i had in mind to do:
// get advancedsearch value
string selectedTxt = AdvancedSearch.value;
//filter on selected value
if (selectedTxt == "item1")
{
    sqlbuilder.where = ("input1@");
}
else if (selectedTxt == "item2")
{
   sqlbuild.where = ("input2@");
etc

        // container for button/dropdown menu
        <div class="container">
                <div class="row buffer">
                    <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 ">
                            <div class="input-group" role="group" >
                                <div class="input-group-btn" role="group">
                                <asp:Button runat="server" class="btn btn-secondary dropdown-toggle" id="AdvancedSearch" data-toggle="dropdown" OnClientClick="AdvancedSubmit();" style="background-color:#718CA1;color:#FFF;" Text="Advanced:" />
                            <ul class="dropdown-menu" runat="server" id="dropdown" style="background-color:#718CA1;color:#FFF;">
                                <li><a class="dropdown-item" href="#" data-value="asset">item1</a></li>
                                <li><a class="dropdown-item" href="#" data-value="building">item2</a></li>
                                <li><a class="dropdown-item" href="#" data-value="astmgrbems">item3</a></li>
                            </ul>
                            </div>
                            </div>
                    </div>
                    <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6 text-center">
                            <div class="input-group" role="group">
                                <input type="text" id="InquiryInput2" onblur="javascript:removeSpaces()" style="display:none" runat="server" class="form-control" Width="280px" />
                            </div>
                    </div>
                </div> 
                </div>

EDIT - Realize I forgot to add functions if that helps 编辑 -意识到我忘了添加功能是否有帮助

function AdvancedSubmit() { var obj = document.getElementById('<%=InquiryInput2.ClientID%>'); 函数AdvancedSubmit(){var obj = document.getElementById('<%= InquiryInput2.ClientID%>');

         if (obj.style.display == 'none') {
             obj.style.display = 'block';
         }
     };
     $(function () {
         $(".dropdown-menu li a").click(function () {
             var selText = $(this).text();
             /* $(".btn btn-secondary").html(selText);*/
             alert(selText);
             document.getElementById('<%=AdvancedSearch.ClientID%>').value = selText;
         })
     });

Just add an ASP hidden field. 只需添加一个ASP隐藏字段。 Then in the javascript set that field's value as well as the button's value. 然后在javascript中设置该字段的值以及按钮的值。 That way it'll be posted back to the server when the form is submitted. 这样,将在提交表单后将其发布回服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM