服务器控件dropdowlist、gridview,然后用ajax 控制 onclick 事件,请问在用户控件中,两个dropdownlist 级联,用ajax实现了不刷新,但是这里我有个问题,就是我的用户控件中还有一个gridview控件,可是我怎么控制,当第二个dropdownlist 值选择改变时,传新值给一个方法,这个方法是用于重新绑定gridview
1、
AjaxMethod.cs  
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
namespace AjaxTest
{
/// <summary>
/// Summary description for AjaxMethod.
/// </summary>
public class AjaxMethod
{
        #region ddl_bind
        public static void ddl_bind(DropDownList ddl, string strsel, string textField, string vauleField, string strmotion)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "User ID=worklog_sa;pwd=liangHui2009;Data Source=oadb02.hollysys.net;Initial Catalog=worklog;connection timeout=60;Max Pool Size = 512;";            string strcomm = strsel;  //             SqlCommand comm = new SqlCommand(strcomm, conn);            SqlDataAdapter da = new SqlDataAdapter(comm);            DataSet ds = new DataSet();
            da.Fill(ds);
            //string strsel = "SELECT * FROM " + strTable + "";
            //DataSet ds = new DataSet();
            //ds = SqlHelper.ExecuteDataset(StrConn, CommandType.Text, strsel);
            ddl.DataSource = ds.Tables[0];
            ddl.DataTextField = textField;
            ddl.DataValueField = vauleField;
            ddl.DataBind();
            ddl.Items.Insert(0, "" + strmotion + "");
            ddl.Items[0].Value = "0";
        }
        #endregion
        #region GetUserList
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.Read)]
        public static DataSet GetUserList(string pno)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "User ID=worklog_sa;pwd=liangHui2009;Data Source=oadb02.hollysys.net;Initial Catalog=worklog;connection timeout=60;Max Pool Size = 512;";            string strcomm = "select distinct pu_name,pu_domainno from ut_projectusers where p_no='" + pno + "'";            SqlCommand comm = new SqlCommand(strcomm, conn);            SqlDataAdapter da = new SqlDataAdapter(comm);            DataSet ds = new DataSet();            da.Fill(ds);
            return ds;
        }
        #endregion
    }
} 2、ddlDisFreshAjax.ascx  
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ddlDisFreshAjax.ascx.cs" Inherits="userControl_ddlDisFreshAjax" %><SCRIPT language="javascript">            
            //项目------------------------------
            function proResult() 
            { 
                var pro=document.getElementById('<%=DropDownList1.ClientID %>');                var index=pro.selectedIndex;
                var Value=pro.options[index].value;
                document.getElementById('<%=TextBox1.ClientID %>').value=Value;
                AjaxTest.AjaxMethod.GetUserList(Value,get_user_Result_CallBack);  //
              
            }
            
            //绑定成员
            function get_user_Result_CallBack(response)
            {
                    
                if (response.value != null)
                {       
                       
                    //debugger;
                    document.getElementById('<%=DropDownList2.ClientID %>').length=0;
                    document.getElementById('<%=DropDownList2.ClientID %>').options.add(new Option("请选择组内成员","0"))                        var ds = response.value;
                    if(ds != null && typeof(ds) == "object" && ds.Tables != null)
                    {            
                        for(var i=0; i<ds.Tables[0].Rows.length; i++)
                    {
                        var name=ds.Tables[0].Rows[i].pu_name;
                      var id=ds.Tables[0].Rows[i].pu_domainno;
                      document.getElementById('<%=DropDownList2.ClientID %>').options.add(new Option(name,id));
                    }
                    
                    }
                }   
                            
                return
            }
            
             function writeTextBox()
            {
                var pro=document.getElementById('<%=DropDownList2.ClientID %>');                var index=pro.selectedIndex;
                var Value=pro.options[index].value;
                document.getElementById('<%=TextBox1.ClientID %>').value=Value;
                dateBind(Value);       //这里调用的后台dateBind(string user) 这个方法 
             }        </SCRIPT><table style="width: 100%">
    <tr>
        <td style="">
            项目:<asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList></td>
        <td style="">
            成员:<asp:DropDownList ID="DropDownList2" runat="server">
            </asp:DropDownList></td>
        <td style="width: 100px">
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td style="height: 20px;" colspan="3">
            <asp:GridView ID="GridView_userlog" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
                CellPadding="3" GridLines="Vertical" OnPageIndexChanging="GridView_userlog_PageIndexChanging"
                Width="100%">
                <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                <Columns>
                    <asp:BoundField DataField="姓名" HeaderText="姓名">
                        <ItemStyle HorizontalAlign="Center" Width="10%" />
                    </asp:BoundField>
                    <asp:BoundField DataField="域帐号" HeaderText="域帐号" Visible="False">
                        <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="项目名称" HeaderText="项目名称">
                        <ItemStyle Width="20%" />
                    </asp:BoundField>
                    <asp:BoundField DataField="版本名称" HeaderText="版本名称">
                        <ItemStyle Width="10%" />
                    </asp:BoundField>
                    <asp:BoundField DataField="阶段名称" HeaderText="阶段名称">
                        <ItemStyle Width="10%" />
                    </asp:BoundField>
                    <asp:BoundField DataField="填写日期" HeaderText="填写日期">
                        <ItemStyle HorizontalAlign="Center" Width="20%" />
                    </asp:BoundField>
                    <asp:BoundField DataField="工时" HeaderText="工时">
                        <ItemStyle HorizontalAlign="Center" Width="10%" />
                    </asp:BoundField>
                    <asp:BoundField DataField="日志内容" HeaderText="日志内容" />
                </Columns>
                <PagerStyle BackColor="Silver" ForeColor="Black" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#99CCFF" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="Gainsboro" />
            </asp:GridView>
        </td>
    </tr>
    <tr>
        <td style="width: 100px">
        </td>
        <td style="width: 100px">
        </td>
        <td style="width: 100px">
        </td>
    </tr>
</table>
大家能不能给些方案,就是说我就是想实现,dropdownlist 选择项改变时重新绑定 gridview ,但是要求就是局部刷新或不刷新,所以 dropdownlist 用了ajax,但是如何使 gridview 重新绑定,我没想到思咱,试了一下都不可行,象我上面的那个,点第二个dropdownlist2时,会提示“没有对象”,也就是 javascript 中 writeTextBox()方法没用执行 其中的一句"dateBind(Value);" 即没有执行 后台 dateBind(string user) 这个方法,先谢谢大家了,帮着给想想吧!

解决方案 »

  1.   

    刚才超长了,还差一个类文件,补上3、ddlDisFreshAjax.ascx.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.Data.SqlClient;    public partial class userControl_ddlDisFreshAjax : System.Web.UI.UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                AjaxPro.Utility.RegisterTypeForAjax(typeof(AjaxTest.AjaxMethod));
                if (!Page.IsPostBack)
                {
                    string username = HttpContext.Current.User.Identity.Name;
                    username = username.Substring(username.LastIndexOf("\\") + 1);                this.GridView_userlog.DataSource = dt_list(username.Trim());
                    this.GridView_userlog.DataBind();                string strsel = "select p_no,p_name from ut_projectbase where p_no in (select distinct p_no from ut_projectusers where pu_domainno='wangxin4095')";
                    AjaxTest.AjaxMethod.ddl_bind(DropDownList1, strsel, "p_name", "p_no", "请选择项目");                this.DropDownList1.Attributes.Add("onclick", "proResult();");
                    this.DropDownList2.Attributes.Add("onclick", "areaResult();");
                }
            }       
            private DataTable dt_list(string domainno)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("姓名");
                dt.Columns.Add("域帐号");
                dt.Columns.Add("项目名称");
                dt.Columns.Add("版本名称");
                dt.Columns.Add("阶段名称");
                dt.Columns.Add("填写日期");
                dt.Columns.Add("工时");
                dt.Columns.Add("日志内容");            SqlConnection conn = new SqlConnection();
                conn.ConnectionString = "User ID=worklog_sa;pwd=liangHui2009;Data Source=oadb02.hollysys.net;Initial Catalog=worklog;connection timeout=60;Max Pool Size = 512;";            //  string strcomm = "select (select DISTINCT pu_name from ut_projectusers where pu_domainno='" + domainno + "') as p_name,pu_domainno,lr_workdate,lr_time,lr_content from ut_logreport where pu_domainno='" + domainno + "'";            string p_no = "";
                string strcomm = "";
                if (DropDownList1.SelectedValue.Trim() != "0" && DropDownList1.SelectedValue.Trim() != string.Empty)
                {
                    p_no = DropDownList1.SelectedValue.Trim();
                    strcomm = "select (select DISTINCT pu_name from ut_projectusers where pu_domainno='" + domainno + "') as u_name,pu_domainno,";
                    strcomm += "(select p_name from ut_projectbase where p_no=ut_logreport.p_no) as p_name,";
                    strcomm += "(select pv_name from ut_projectversion where pv_no=ut_logreport.pv_no) as pv_name,";
                    strcomm += "(select ps_name from ut_projectstage where ps_no=ut_logreport.ps_no) as ps_name,";
                    strcomm += " lr_workdate,lr_time,lr_content ";
                    strcomm += "from ut_logreport where pu_domainno='" + domainno + "' and p_no='" + p_no + "'";
                }
                else
                {
                    strcomm = "select (select DISTINCT pu_name from ut_projectusers where pu_domainno='" + domainno + "') as u_name,pu_domainno,";
                    strcomm += "(select p_name from ut_projectbase where p_no=ut_logreport.p_no) as p_name,";
                    strcomm += "(select pv_name from ut_projectversion where pv_no=ut_logreport.pv_no) as pv_name,";
                    strcomm += "(select ps_name from ut_projectstage where ps_no=ut_logreport.ps_no) as ps_name,";
                    strcomm += " lr_workdate,lr_time,lr_content ";
                    strcomm += "from ut_logreport where pu_domainno='" + domainno + "' ";
                }            SqlCommand comm = new SqlCommand(strcomm, conn);            SqlDataAdapter da = new SqlDataAdapter(comm);            DataSet ds = new DataSet();            da.Fill(ds);
                foreach (DataRow dsdr in ds.Tables[0].Rows)
                {
                    DataRow dr = dt.NewRow();
                    dr["姓名"] = dsdr["u_name"].ToString().Trim();
                    dr["域帐号"] = dsdr["pu_domainno"].ToString().Trim();
                    dr["项目名称"] = dsdr["p_name"].ToString().Trim();
                    dr["版本名称"] = dsdr["pv_name"].ToString().Trim();
                    dr["阶段名称"] = dsdr["ps_name"].ToString().Trim();
                    dr["填写日期"] = dsdr["lr_workdate"].ToString().Trim();
                    dr["工时"] = dsdr["lr_time"].ToString().Trim();
                    dr["日志内容"] = dsdr["lr_content"].ToString().Trim();                dt.Rows.Add(dr);
                }            return dt;
            }
            private void data_bind()
            {            string pudomainno = "";
                if (DropDownList2.SelectedValue != "0" && DropDownList2.SelectedValue != string.Empty)
                {
                    pudomainno = DropDownList2.SelectedValue.Trim();
                }
                else
                {
                    string username = HttpContext.Current.User.Identity.Name;
                    username = username.Substring(username.LastIndexOf("\\") + 1);
                    pudomainno = username;
                }
                this.GridView_userlog.DataSource = dt_list(pudomainno);
                this.GridView_userlog.DataBind();
            }
            private void ddl_bind(DropDownList ddl, string strsel, string textField, string vauleField, string strmotion)
            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = "User ID=worklog_sa;pwd=liangHui2009;Data Source=oadb02.hollysys.net;Initial Catalog=worklog;connection timeout=60;Max Pool Size = 512;";            string strcomm = strsel;  //             SqlCommand comm = new SqlCommand(strcomm, conn);            SqlDataAdapter da = new SqlDataAdapter(comm);            DataSet ds = new DataSet();
                da.Fill(ds);
                //string strsel = "SELECT * FROM " + strTable + "";
                //DataSet ds = new DataSet();
                //ds = SqlHelper.ExecuteDataset(StrConn, CommandType.Text, strsel);
                ddl.DataSource = ds.Tables[0];
                ddl.DataTextField = textField;
                ddl.DataValueField = vauleField;
                ddl.DataBind();
                ddl.Items.Insert(0, "" + strmotion + "");
                ddl.Items[0].Value = "0";
            }        protected void DDL_project_SelectedIndexChanged(object sender, EventArgs e)
            {
                string strsel = "select distinct pu_name,pu_domainno from ut_projectusers where p_no='" + DropDownList1.SelectedValue.Trim() + "'";
                ddl_bind(DropDownList2, strsel, "pu_name", "pu_domainno", "请选择组内成员");
            }
            protected void DDL_proUser_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (DropDownList2.SelectedValue != "0")
                {
                    this.GridView_userlog.DataSource = dt_list(DropDownList2.SelectedValue.Trim());
                    this.GridView_userlog.DataBind();
                }
            }        /// <summary>
            ///这里根据dropdownlist2的选择值重表绑定gridview   
            /// </summary>
            /// <param name="user"></param>
            protected void dateBind(string user)
            {
                if (user != "0")
                {
                    this.GridView_userlog.DataSource = dt_list(user);
                    this.GridView_userlog.DataBind();
                }
            }        protected void GridView_userlog_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                GridView_userlog.PageIndex = e.NewPageIndex;
                //e.NewPageIndex = this.GridView_userlog.PageIndex;
                data_bind();        }    }