从 javascript 调用代码隐藏
Call Codebehind From javascript
我想解决一个问题,
试试这个
<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 }
相关文章:
- 从var向代码隐藏函数传递值
- javascript函数和代码隐藏函数的执行顺序
- 显示生成的gif;base64图像使用C#代码隐藏在img中使用JavaScript
- 使用较少代码隐藏和显示选择菜单内容的更好方法是什么?javascript
- jQuery toogle-一个打开,另一个隐藏-在代码中的任何位置
- 如果javascript打开/关闭,则隐藏和显示html代码
- 使用C#将在JavaScript中创建的变量值传递给ASP.Net中的服务器端(代码隐藏)
- 使用ScriptManager.RegisterStartupScript从代码隐藏调用javascript方法
- 正在代码隐藏中检索选定ListBox项的属性数据
- 为什么需要从代码隐藏调用 JavaScript 函数
- 空引用错误 Eval(字符串) 将其传递给代码隐藏中的函数
- 要隐藏的JavaScript代码
- 从Java脚本调用C#代码隐藏代码
- 动态内容angular js的动态显示隐藏代码
- 如何在AngularJS中显示和隐藏代码块
- 如何隐藏/取消隐藏代码镜像
- 铁拳7网站html隐藏代码行
- jQuery准备在IDE中隐藏代码大纲
- 切换显示/隐藏代码在RMarkdown
- 如何删除Html <隐藏代码的子弹