从 javascript 调用代码隐藏
本文关键字:隐藏 代码 调用 javascript | 更新日期: 2023-09-27 18:37:15
我想解决一个问题,
试试这个
<script type="text/javascript">
function txtOnKeyPress(txt1) {
if (txt1 != 'undefined') {
var txt2 = document.getElemejavantById('<%=TxtArama.ClientID %>');
txt2.value = txt1.value;
<% Session["Sercert"] = TxtArama.Text;%>
//alert(txt2.value);
var DTT =<%= GetSearcher("") %>;
alert(DTT);
}
}
<%--function CallCodeBehindMethod() {
var txt2 = document.getElementById('<%=TxtArama.ClientID %>');
alert(window.PageMethods.GetName(txt2));
//this.GetName(txt2);
}--%>
</script>
JS 是客户端文本框按键事件
和
事件隐藏代码是
protected string GetSearcher(String KeyValue)
{
KeyValue = Session["Sercert"].ToString();
String aa = "";
if (KeyValue.Length > 0)
{
DataTable DT = new DataTable();
DT = DbClass.GetDataTable("SELECT * FROM Products WHERE ProductName LIKE '%" + KeyValue + "%' LIMIT 10", "MySql");
for (int i = 0; i < DT.Rows.Count; i++)
{
aa += "<a href='"" +ReWriterPath(DT.Rows[i]["ProductId"].ToString(), DT.Rows[i]["ProductName"].ToString()) +"'">" +DT.Rows[i]["ProductName"] +"</a><br />";
}
RptSearcher.DataSource = DT;
RptSearcher.DataBind();
UPLSearcher.Update();
}
return aa;
}
我尝试在中继器中填写数据
<asp:UpdatePanel runat="server" ID="UPLSearcher" UpdateMode="Conditional">
<ContentTemplate>
<asp:Literal runat="server" ID="LtrSearcher"></asp:Literal>
<asp:Repeater runat="server" ID="RptSearcher">
<ItemTemplate>
<a href="<%# ReWriterPath(Eval("ProductId").ToString(), Eval("ProductName").ToString()) %>"><%# Eval("ProductName") %></a><br />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
我想在代码隐藏方法上的搜索按键事件中更新我的中继器。并重新更新我的更新面板和结果列表。但是js代码和方法只运行页面加载。并且密钥前提不在代码后面的代码上运行。但是这个//alert(txt2.value);每次按键时运行。
请帮忙。
您必须在代码隐藏中创建 [WebMethod]。 而不是你可以从JavaScript调用该方法。
<script type="text/javascript">
function txtOnKeyPress(txt1) {
$.ajax({
type: "POST",
url: "/yourpage.aspx/GetSearcher",
data: '', // put your data here that you want to pass in server method
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
//write your code what you want to display on success
}
</script>
创建代码隐藏方法
[WebMethod]
[ScriptMethod()]
public static string GetSearcher(String KeyValue)
{ //Your code goes here }