在我的 HTML 中:
<button type="submit"></button>
<asp:TextBox ID="_searchKeyword" runat="server" Width="450" Height="42"></asp:TextBox>
<div style="display:none">
<asp:LinkButton ID="_searchLinkButton" OnClick="SearchButton" runat="server">Search</asp:LinkButton>
</div>
当我单击 button
元素时,我希望回发到服务器并运行 SearchButton
方法。
在我的代码后面:
protected void SearchButton(Object sender, EventArgs e)
{
string typeValue = _hdDropDown.Value;
if (typeValue == "All")
{
typeValue = String.Empty;
}
}
我怎样才能做到这一点,我需要 JavaScript 吗?
最佳答案
有什么原因不能使用 Asp:Button 元素吗?
我的第一个建议是使用 Asp:Button 元素而不是您当前的设置,但如果出于某种原因您不能,那么我建议为按钮命名,然后使用 jQuery 您可以执行以下操作:
$('#submitbuttonid').click(function(){$('#_searchLinkButton').click();});
或者在普通 JavaScript 中:
var submitBtn = document.getElementById('submit buttonid');
submitBtn.addEventListener("click", function(){
var linkBtn = document.getElementById('_searchLinkButton');
linkBtn.onClick();
});
关于javascript - 如何在asp.net中使用javascript将按钮html回发到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29543495/