請問在jQuery post之後如何才能正確的執行user control(WebUserControl1)內的setMyName function呢?
由下面的程式Label1.Text都只能設為"Mary"
雖然if判斷可以進入,但都無法變成"John"小弟我的目的是要在jQuery post mode="jq"之後撈取DB
然後再將資料設給user control的function
但不知jQuery callback function要如何寫?
也就可以達成jQuery post之後調用WebUserControl1的setMyName給定"John"名稱
請高手指導~
謝謝!Default.aspx<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>    <script src="jquery-1.4.2.js" type="text/javascript"></script>
    
       <script type="text/javascript">
           $(function() {               function loadData() {
                   $.post("Default.aspx", { mode: "jq" }, bindList, "json");
               }               function bindList(data) {               }               loadData();
           })
       </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="divUc" runat="server">        <uc1:WebUserControl ID="WebUserControl1" runat="server" />    </div>
    </form>
</body>
</html>
Default.aspx.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        WebUserControl1.setMyName("Mary");        if (Request["mode"] == "jq") {            WebUserControl1.setMyName("John");
           
        }
    }}WebUserControl.ascx<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<table style="width:100%;">
    <tr>
        <td>
            My name is :
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </td>
    </tr>
</table>
WebUserControl.ascx.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class WebUserControl : System.Web.UI.UserControl
{    public void setMyName(string myname) {
        Label1.Text = myname;
    }
}

解决方案 »

  1.   

    function bindList(data) {               }
    =>function bindList(data) {
                     eval(data);
                   }
    -----------------------------------public void setMyName(string myname) {
            Label1.Text = myname;
        }=>public string setMyName(string myname) {
            Label1.Text = myname;
            return string.Format("document.getElementById('{0}').innerText",Label1.ClientID);
        }------------------------------------------------------if (Request["mode"] == "jq") {            WebUserControl1.setMyName("John");
               
            }
    =>
    if (Request.Form["mode"]!=null&&Request.Form["mode"] == "jq") {            Response.Write(WebUserControl1.setMyName("John"));
                Response.End();
            }
      

  2.   

    謝謝Sandy945
    不過改成這樣出來結果還是顯示"Mary"
    是否哪裡需要修改呢?
      

  3.   

    function bindList(data) {
      eval(data);
      }
    =>function bindList(data) {
      eval(data);
    alert(data); //看下是什么
      }
      

  4.   

    return string.Format("document.getElementById('{0}').innerText",Label1.ClientID);=》return string.Format("document.getElementById('{0}').innerText='{1}'",Label1.ClientID,myname);
    这回应该可以了
      

  5.   

    太感謝了!
    不過不知為什麼無法進到我call back function bindList內?
    後來加入了     
           
    System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
    Response.Write(jss.Serialize(WebUserControl1.setMyName("John")));就可以進入bindList並將Mary改成John了
     if (Request.Form["mode"] != null && Request.Form["mode"] == "jq")
     {
        System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
        Response.Write(jss.Serialize(WebUserControl1.setMyName("John")));
        Response.End();
     }
      

  6.   

    $.post("Default.aspx", { mode: "jq" }, bindList, "json");
    是因为 "json" 的缘故,指定了类型为 json