ASP.NET中前台javascript与后台c#函数相互调用问题(1)http://hi.baidu.com/xuexiang516168/blog/item/90f2bc4bc316372908f7eff4.html

ASP.NET中前台javascript与后台c#函数相互调用问题
2010-04-11 13:41

C#代码与javaScript函数的相互调用

问:
1.如何在JavaScript访问C#函数?
2.如何在JavaScript访问C#变量?
3.如何在C#中访问JavaScript的已有变量?
4.如何在C#中访问JavaScript函数?

问题1答案如下:
javaScript函数中执行C#代码中的函数:
方法一:1、首先建立一个按钮,在后台将调用或处理的内容写入button_click中;
          2、在前台写一个js函数,内容为document.getElementById("btn1").click();
          3、在前台或后台调用js函数,激发click事件,等于访问后台c#函数;

方法二:1、函数声明为public              
             后台代码(把public改成protected也可以)
             public string ss()
             {
                return("a");
             }
          2、在html里用<%=fucntion()%>可以调用
             前台脚本
             <script language=javascript>
             var a = "<%=ss()%>";
             alert(a);
             </script>
方法三:1、<script language="javascript">
             <!--
             function __doPostBack(eventTarget, eventArgument)
             {
                var theForm = document.Form1;       //指runat=server的form
                theForm.__EVENTTARGET.value = eventTarget;
                theFrom.__EVENTARGUMENT.value = eventArgument;
                theForm.submit();
             }
             -->
             </script>
             <input id="Button1" type="button" name="Button1" value="按钮" οnclick="javascript:__doPostBack('Button1','')">
         
方法四:<script language="javascript">
function SubmitKeyClick()
{
      if (event.keyCode == 13)
      {
          event.cancelBubble = true;
          event.returnValue = false;
          document.all.FunName.value="你要调用的函数名";
          document.form[0].submit();
      }
}
</script>

<INPUT οnkeypress="SubmitKeyClick()" id="aaa" type="text">
<input type="hidden" name="FunName"> 〈!--用来存储你要调用的函数 --〉

在.CS里有:
public Page_OnLoad()
{
if (!Page.IsPost())
{
string strFunName=Request.Form["FunName"]!=null?Request.Form["FunName"]:"";
//根据传回来的值决定调用哪个函数
switch(strFunName)
{
case "enter()":
enter() ; //调用该函数
break;
case "其他":
//调用其他函数
break;
default:
//调用默认函数
break;
}
}
}

public void enter()
{
//……比如计算某值
}

问题2.如何在JavaScript访问C#变量?
答案如下:
方法一:1、通过页面上隐藏域访问<input id="xx" type="hidden" runat="server">
方法二:1、如后台定义了PUBLIC STRING N;前台js中引用该变量的格式为'<%=n%>'或"+<%=n%>+"
方法三:1、或者你可以在服务器端变量赋值后在页面注册一段脚本
             "<script language='javascript'>var temp=" + tmp + "</script>"
             tmp是后台变量,然后js中可以直接访问temp获得值。


3.如何在C#中访问JavaScript的已有变量?

答案如下:

方法一:1、前台使用静态文本控件隐藏域,将js变量值写入其中;
          2、后台用request["id"]来获取值;

方法二:可以用cookie或session


4.如何在C#中访问JavaScript函数?
答案如下:
c#代码中执行javaScript函数:
方法一:1、Page.RegisterStartupScript("ggg","<script>SetVisible(1);</script>");
方法二:使用Literal类,然后
private void Button2_Click(object sender, System.EventArgs e)
{
string str;
str="<script language='javascript'>";
str+="selectRange()";
str+="</script>";
//Literal1.Visible=true;
Literal1.Text=str;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/listinfo" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/round_style" android:elevation="4dp" android:layout_margin="8dp" android:clickable="true" android:padding="10dp"> <com.xuexiang.xui.widget.textview.autofit.AutoFitTextView android:id="@+id/code" style="@style/item_show_title" android:layout_width="14dp" android:layout_height="16dp" android:paddingTop="2dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <com.xuexiang.xui.widget.textview.autofit.AutoFitTextView style="@style/item_show_title" android:id="@+id/name" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/code"/> <com.xuexiang.xui.widget.textview.autofit.AutoFitTextView style="@style/item_show_title" android:id="@+id/spec" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/name"/> <com.xuexiang.xui.widget.textview.autofit.AutoFitTextView style="@style/item_show_title" android:id="@+id/composition" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/spec"/> <com.xuexiang.xui.widget.textview.autofit.AutoFitTextView style="@style/item_show_title" android:id="@+id/weight" app:layout_constraintStart_toEndOf="@+id/composition" app:layout_constraintTop_toBottomOf="@+id/spec"/> <ImageView android:id="@+id/status" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/name" app:layout_constraintTop_toTopOf="parent" /> <View style="@style/item_show" android:id="@+id/view_task_list" android:layout_width="match_parent" android:layout_height="1dp" android:background="#cccccc" android:layout_marginTop="3dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/composition" app:layout_constraintStart_toStartOf="@+id/composition"/> </androidx.constraintlayout.widget.ConstraintLayout>点击表格,显示弹窗展示列内容
06-04
要在点击表格时显示弹窗展示列内容,可以通过以下步骤实现: 1. 在布局文件创建一个弹窗布局,例如: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Title" android:textSize="18sp" android:textStyle="bold" /> <TextView android:id="@+id/content" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Content" android:textSize="16sp" /> </LinearLayout> ``` 2. 在点击表格的事件,获取列内容并设置到弹窗布局,例如: ```java listinfo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 获取列内容 String code = ((TextView) v.findViewById(R.id.code)).getText().toString(); String name = ((TextView) v.findViewById(R.id.name)).getText().toString(); String spec = ((TextView) v.findViewById(R.id.spec)).getText().toString(); String composition = ((TextView) v.findViewById(R.id.composition)).getText().toString(); String weight = ((TextView) v.findViewById(R.id.weight)).getText().toString(); // 创建弹窗布局 View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null); // 获取弹窗布局的控件 TextView titleView = popupView.findViewById(R.id.title); TextView contentView = popupView.findViewById(R.id.content); // 设置弹窗标题和内容 titleView.setText(name); contentView.setText("Code: " + code + "\n" + "Spec: " + spec + "\n" + "Composition: " + composition + "\n" + "Weight: " + weight); // 创建弹窗 PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT); // 设置弹窗的背景和动画 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setAnimationStyle(R.style.PopupAnimation); // 显示弹窗 popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0); } }); ``` 其,R.layout.popup_layout 表示弹窗布局文件的名称,R.style.PopupAnimation 表示弹窗的动画样式。 3. 在 styles.xml 文件定义弹窗的动画样式,例如: ```xml <style name="PopupAnimation" parent="android:Animation"> <item name="android:windowEnterAnimation">@anim/popup_enter</item> <item name="android:windowExitAnimation">@anim/popup_exit</item> </style> ``` 其,@anim/popup_enter 和 @anim/popup_exit 表示进入和退出弹窗时的动画效果,可以根据需要自定义实现。 4. 在 res 目录下创建 anim 目录,并在该目录下创建 popup_enter.xml 和 popup_exit.xml 文件,实现弹窗进入和退出时的动画效果,例如: popup_enter.xml: ```xml <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:fromXScale="0.0" android:fromYScale="0.0" android:toXScale="1.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="300" /> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" /> </set> ``` popup_exit.xml: ```xml <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:fromXScale="1.0" android:fromYScale="1.0" android:toXScale="0.0" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:duration="300" /> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" /> </set> ``` 这样,当用户点击表格时,就会显示一个弹窗,展示列内容。需要注意的是,可以根据实际需求自定义弹窗布局和样式,以及实现更加复杂的动画效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值