2016-08-05 51 views
0

我想知道如何我可以從我的HTML按鈕/下拉菜單在C#中的文本?通過asp.net dropdownlist,我能夠做到string text = dropdownlist.Value;但它看起來並不那麼簡單,或者它可能是,具有不同類型的下拉菜單。我不能使用asp下拉列表,因爲它沒有充當按鈕和下拉菜單。以下功能用於將文本從默認的「高級」更改爲用戶選定的項目文本。在用戶更改按鈕文本後,我想抓住該文本並將其用作sqlbuild.where子句的條件。此外,更新後的按鈕的文本應保持在單擊搜索按鈕後。與ASP下拉列表中的值保持在刷新後,但由於某種原因,當我搜索時更新的下拉菜單,改變擦拭選擇後提交獲取HTML按鈕/下拉文本從C#代碼

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 = ("[email protected]"); 
} 
else if (selectedTxt == "item2") 
{ 
    sqlbuild.where = ("[email protected]"); 
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> 

編輯 - 意識到我忘了補充功能如果有幫助

function 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; 
     }) 
    }); 
+0

看起來你正在使用'Text'屬性;它會不會是'button.Text'? – Whothehellisthat

+0

頁面上的按鈕文字是否真的發生變化?如果是這樣,則不會將其與表單一起發佈。也許你可以爲此添加一個隱藏字段,並在按鈕的值的同時更改該值? – Whothehellisthat

+0

@Whathehellisthat是按鈕文本實際上是通過我剛剛粘貼的功能在頁面上改變的。對不起,忘了添加它,雖然我提到它。所以你說我可以在前端添加一個隱藏的字段來捕獲html selText的變化,然後用它作爲c#端的過濾器? – ryan45366

回答

1

只需添加一個ASP隱藏字段。然後在javascript中設置該字段的值以及按鈕的值。這樣,當表單被提交時,它將被髮回服務器。