你可以在客户端判断后,将结果写入cookie,
再在服务器程序里判断要不要启动某程序。

解决方案 »

  1.   

    window.event.returnValue=true;
    改成类似于:__doPostBack('ListBox1','');
      

  2.   

    ListBox1.Attributes.Add("onchange","select(this.selectedIndex)");===========>Page.RegisterOnSubmitStatement("myscript","return select(document.all."+ListBox1.ClientID+".selectedIndex)");
      

  3.   

    对不起,上面的代码最后丢了一个分号,正确的应该是这样:Page.RegisterOnSubmitStatement("myscript","return select(document.all."+ListBox1.ClientID+".selectedIndex);");
      

  4.   

    其实就是改成这样就行:function select(index){
       if(index==0)
             {
                return false;      //如果是選擇了第一項就不用到服務器端去處理
             }
            else
               { __doPostBack('ListBox1','');}
        }
      

  5.   

    我是樓主﹐謝謝各位
    雖然用__doPostBack('ListBox1','');可以提交到服務器上﹐
    但是并不會執行ListBox1_SelectedIndexChanged方法
    我用逐步調試工具﹐發現它只執行完Page_Load函數后就結束了
    請各位不吝賜教
      

  6.   

    不会呀!这个我是测试过的!如果你的程序不執行ListBox1_SelectedIndexChanged方法,那不是__doPostBack('ListBox1','');这儿的问题,可能是其它原因。
      

  7.   

    private void Page_Load(object sender, System.EventArgs e)
    {
    ListBox1.Attributes.Add("onchange","select(this.selectedIndex)");
    string[] str = {"1","2","3"};
    ListBox1.DataSource = str;
    ListBox1.DataBind();
    Page.DataBind();
    }
    private void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    Response.Write("<script language='javascript'>alert('server select')</script>"); }
    }=============================================================================
        function select(index)
        {
             if(index==0)
             {
                alert(0);
                return false;      //如果是選擇了第一項就不用到服務器端去處理
             }
             else
             {
    __doPostBack('ListBox1','');
    //window.event.returnValue=true;   //這一句也沒有效果
    return true;
             }我测试了没错阿
      

  8.   

    作为服务器端的 ListBox1 既要处理客户端的 onchange() 事件,又要处理服务器端的SelectedIndexChanged() 事件,引起了冲突!两者的实质都是生产客户端的HTML:
    ListBox1.Attributes.Add("onchange","select(this.selectedIndex)");产生的HTML为:
    <select name="ListBox1" id="ListBox1" onchange="select(this.selectedIndex)">
    ListBox1 的AutoPostBack="True"后产生的HTML为:
    <select name="ListBox1" onchange="__doPostBack('ListBox1','')">如果你让他们共存,产生的HTML为:
    <select name="ListBox1" onchange="select(this.selectedIndex);__doPostBack('ListBox1','')">
    你应该会看到状态栏里脚本错误提示!
    这样无论客户端或服务器端的onchange()事件都不能执行,因为脚本出错啦!
      

  9.   

    在Page_Load里把
    ListBox1.Attributes.Add("onchange","select(this.selectedIndex)");
    改为:
    ListBox1.Attributes.Add("onmousedown","select(this.selectedIndex);");
    就OK了!
      

  10.   

    shitingzhao兄,你说的好像不对,我测试过,没有错误。可以实现搂主的想法
      

  11.   

    to 北狼
    我觉得shitingzhao(淡淡一笑) 说的有道理
    你的方法也对,你们两个的方法不矛盾,是解决问题的两个方法
      

  12.   

    freecs(北狼) ( ) 
    你的代码我怎么运行不谈出对话矿啊
      

  13.   

    这是发到客户断的
    <select name="ListBox1" size="4" onchange="select(this.selectedIndex)__doPostBack('ListBox1','')" language="javascript" id="ListBox1" style="Z-INDEX: 101; LEFT: 152px; POSITION: absolute; TOP: 80px">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option></select>
    onchange="select(this.selectedIndex)__doPostBack('ListBox1','')" 有问题吧???
      

  14.   

    我是樓主﹐謝謝大家的關注
    freecs(北狼)的方法可以引發客戶端和服務器端的事件
    但是我現在用了一個TreeView控件
    我想根據節點的NodeData去判斷是否要展開(到資料庫中抓取數據后填充此節點)﹐抓過第一次后
    第二次展開時就不用提交到服務器端去﹐但是好像不行﹐我把原碼貼出﹐希望大家幫我看看;----------------------AllocateRights.ascx----------------
    <%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %>
    <%@ Control Language="c#" AutoEventWireup="false" Codebehind="AllocateRights.ascx.cs" Inherits="AllocateRights.AllocateRights" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
    <script language="javascript">
    //這里是客戶端代碼﹐即要判斷是否要到服務器上處理的javascript函數
             //這是TreeView控件的Expand(展開)方法(這個不行﹗﹗﹗)
    function chkinput2(tree){
    treenode=tree.getTreeNode(tree.clickedNodeIndex);
    nodedata=treenode.getAttribute("nodedata");
    if(nodedata=="0"){
    alert("不去服務器處理");
    }
    else{
    alert("去服務器端處理");
    //window.event.returnValue=true;
    __doPostBack('TreeView1','');
    }
    }//這是ListBox的SelectedIndexChange事件(這個可以執行)
    function chkinput(selectedIndex){
    alert("client manage \n" + selectedIndex);
    if(selectedIndex==0){
    alert("不去服務器處理");
    }
    else{
    alert("去服務器處理");
    __doPostBack('ListBox1','');
    }
    }</script>
    <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" border="1">
    <TR>
    <TD style="HEIGHT: 124px" vAlign="top"><FONT face="新細明體"><asp:listbox AutoPostBack="True" id="ListBox1" runat="server">
    <asp:ListItem Value="1">1</asp:ListItem>
    <asp:ListItem Value="2">2</asp:ListItem>
    <asp:ListItem Value="3">3</asp:ListItem>
    </asp:listbox></FONT></TD>
    <TD style="HEIGHT: 124px" vAlign="top"><FONT face="新細明體"><iewc:treeview id="TreeView1" runat="server" AutoPostBack="false" SystemImagesPath="\CustomTreeTest\images\treeimages/">
    <iewc:TreeNode NodeData="0" Text="***系統權限分配" Expanded="True">
    <iewc:TreeNode NodeData="5" Text="群組分配">
    <iewc:TreeNode Text="正在載入資源..."></iewc:TreeNode>
    </iewc:TreeNode>
    <iewc:TreeNode NodeData="1" Text="廠別分配">
    <iewc:TreeNode Text="正在載入資源..."></iewc:TreeNode>
    </iewc:TreeNode>
    <iewc:TreeNode NodeData="2" Text="部門分配">
    <iewc:TreeNode Text="正在載入資源..."></iewc:TreeNode>
    </iewc:TreeNode>
    <iewc:TreeNode NodeData="3" Text="公司分配">
    <iewc:TreeNode Text="正在載入資源..."></iewc:TreeNode>
    </iewc:TreeNode>
    <iewc:TreeNode NodeData="4" Text="倉庫分配">
    <iewc:TreeNode Text="正在載入資源..."></iewc:TreeNode>
    </iewc:TreeNode>
    </iewc:TreeNode>
    </iewc:treeview>
    </FONT>
    </TD>
    </TR>
    <TR>
    </TR>
    </TABLE>--------------------AllocateRights.ascx.cs----------------------------
    namespace AllocateRights
    {
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls; using Microsoft.Web.UI.WebControls; /// <summary>
    /// 權限分配模塊(web user control)
    /// </summary>
    public abstract class AllocateRights : ModuleBase
    {
    protected System.Web.UI.WebControls.ListBox ListBox1;
    protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.Button Button2; private void Page_Load(object sender, System.EventArgs e)
    {
    //這里是加入兩個客戶端處理事件
    TreeView1.Attributes.Add("onexpand","chkinput2(this)");
    ListBox1.Attributes.Add("onchange","chkinput(this.selectedIndex)"); } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 此呼叫為 ASP.NET Web Form 設計工具的必要項。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 此為設計工具支援所必需的方法 - 請勿使用程式碼編輯器修改
    /// 這個方法的內容。
    /// </summary>
    private void InitializeComponent()
    {
    this.ListBox1.SelectedIndexChanged += new System.EventHandler(this.ListBox1_SelectedIndexChanged);
    this.TreeView1.Expand += new Microsoft.Web.UI.WebControls.ClickEventHandler(this.TreeView1_Expand);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion /// <summary>
    /// 單擊TreeView節點的展開動作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    //這個不會執行
    protected void TreeView1_Expand(object sender, Microsoft.Web.UI.WebControls.TreeViewClickEventArgs e)
    {
    Response.Write("<script language='javascript'>alert('hello')</script>");
    }//這個會執行
    protected void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    int ss=((ListBox)sender).SelectedIndex;
    Response.Write("<script language='javascript'>alert('server manage "+ ss + "')</script>");
    } }
    }-----------------------WebForm1.aspx-----------------------------
    <%@ Register TagPrefix="uc1" TagName="AllocateRights" Src="AllocateRights.ascx" %>
    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="AllocateRights.WebForm1" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <FONT face="新細明體">
    <uc1:AllocateRights id="AllocateRights1" runat="server"></uc1:AllocateRights></FONT>
    </form>
    </body>
    </HTML>
    ------------------------------------------------
    請大家看看這段代碼有什么問題
    為什么ListBox可以先根據客戶端數據﹐再執行服務器端的事件處理程式
    而TreeView的Expand方法卻在執行服務器端時執行完Page_Load后就結束了(不再執行ListBox1_SelectedIndexChanged方法)
      

  15.   

    freecs(北狼) ( 
    你的代码我怎么同不过。不执行提交服务器的代码。autopost设成假不行,成真就说网页有错误
      

  16.   

    ListBox1.Attributes.Add("onchange","select(this.selectedIndex);")没有;
    客户段代码不执行!!!!
      

  17.   

    我是樓主
    大家好
    有沒有TreeView的高手
      

  18.   

    to:wyfwyf2000(求知)
       只要解決﹐我可以把100分全給你﹐
     其他兄弟我也可以另開帖給分﹐大家幫忙
     在TreeView中為什么在客戶端使用了onexpand事件后﹐再用__doPostBack回傳到服務器端時﹐
    只執行到Page_Load就完了,而不繼續執行TreeView1_Expand事件了(而像ListBox的onchange事件執行后﹐再調用__doPostBack后就可以到服務器端執行 ListBox1_SelectedIndexChanged事件)  
      我很急﹐大家幫幫忙﹐分不夠可以加
      

  19.   

    代碼見十九樓
    第一個是一個自定義控件﹐包括一個ListBox和一個TreeView(在這里實現這兩個控件的客戶端方法)
    第二個是自定義控件的code behind(在這里面實現這兩個控件的報務器方法)
    第三個是加入這個自定義控件的WebForm(用這個測試)