Subscribe to RSS Subscribe to Comments

HONgHong,珍惜生命,远离鼠标…

尝试.NET2.0客户端调用服务器端方法,但只能传字符串

aspx页面:

<%@ Page Language=”C#” AutoEventWireup=”true”  CodeBehind=”Test.aspx.cs” Inherits=”AjaxTest.Test” %>
<%@ Implements Interface=”System.Web.UI.ICallbackEventHandler” %>

<!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>Untitled Page</title>
    <style type=”text/css”>
        .dg
        {
            color:blue;
        }
    </style>
</head>
<body>
    <form id=”form1″ runat=”server”>
        <asp:GridView ID=”GridView1″ runat=”server”>
            <Columns>
                <asp:CommandField ShowEditButton=”True” />
            </Columns>
        </asp:GridView>
    <div>
    Select a category:
        <asp:DropDownList ID=”ddlCategory” runat=”server” DataSourceID=”SqlDataSource1″
            DataTextField=”Name” DataValueField=”CategoryId” onChange=”GetSelectedProducts(this.value)” >
        </asp:DropDownList><asp:SqlDataSource ID=”SqlDataSource1″ runat=”server” ConnectionString=”<%$ ConnectionStrings:MSPetShop4ConnectionString %>”
            SelectCommand=”SELECT [Name], [CategoryId] FROM [Category]”></asp:SqlDataSource>
       
    </div>
        <asp:Label ID=”lblMsg” runat=”server” ></asp:Label>
    <div>
</div>
Select a product:<br />
<div id=”MyDiv”></div>

<script language=”javascript” type=”text/javascript”>

function GetSelectedProducts(selectedValue)

{

    CallServer(selectedValue,”);

}

function RecieveServerData(rValue)

{

    document.getElementById(”MyDiv”).innerHTML = rValue;

}

</script>

    </form>
</body>
</html>
———————————-

.CS页面:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;

namespace AjaxTest
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sbReference = ClientScript.GetCallbackEventReference(this, “arg”, “RecieveServerData”, “context”);

            string cbScript = String.Empty;

            // check if the script is already registered or not

            if (!ClientScript.IsClientScriptBlockRegistered(”CallServer”))
            {

                cbScript = @” function CallServer(arg,context) { ” + sbReference + “}”;

                ClientScript.RegisterClientScriptBlock(this.GetType(), “CallServer”, cbScript, true);

            }

        }

        // protected variable
        protected string returnValue;

        public void RaiseCallbackEvent(string eventArgument)
        {

            if (!String.IsNullOrEmpty(eventArgument))
            {

                returnValue = eventArgument;

            }

            else
            {

                // Set a flag so that the GetCallbackResult is not called.
                lblMsg.Text = “Faild.Because ” + eventArgument;

            }

        }

        public string GetCallbackResult()
        {

            return GetProductsByCategoryID(returnValue);

        }

        private string GetProductsByCategoryID(string categoryID)
        {

            string connectionString = @”Data Source=GGZZHS-D030146;Initial Catalog=MSPetShop4;Integrated Security=True”;

            SqlConnection myConnection = new SqlConnection(connectionString);

            SqlCommand myCommand = new SqlCommand(”SELECT * FROM Product WHERE CategoryId = @CategoryId”, myConnection);

            myCommand.Parameters.AddWithValue(”@CategoryId”, categoryID);

            SqlDataAdapter ad = new SqlDataAdapter(myCommand);

            DataSet ds = new DataSet();

            ad.Fill(ds);

            DataGrid dg = new DataGrid();
            dg.ID = “dgProducts”;
            dg.DataSource = ds;
            dg.DataKeyField = “ProductId”;
            dg.CssClass = “dg”;
           
           
            dg.DataBind();

            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            dg.RenderControl(htw);
            return sw.ToString();

            //DropDownList ddlProducts = new DropDownList();

            //ddlCategory.ID = “ddlProducts”;

            //ddlProducts.DataSource = ds;

            ////ddlProducts.Attributes.Add(”onChange”, “PopSelectedProductID(this.value)”);

            //ddlProducts.DataTextField = “Name”;

            //ddlProducts.DataValueField = “ProductId”;

            //ddlProducts.DataBind();

            //StringWriter sw = new StringWriter();

            //HtmlTextWriter htw = new HtmlTextWriter(sw);

            //ddlProducts.RenderControl(htw);

            //return sw.ToString();

        }

    }
}

No comments yet. Be the first.

Leave a reply

提示:如果你刚刚提交过评论,但是还没有被显示出来,请点击这里刷新一下: 刷新评论

Based on FluidityUpload by AK47 | ITech . Theme Redesigned by Kaushal Sheth Sponsored by Send Flowers