首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

DropDownList 二级联动 保留时 第二个DropDownList 被刷新

2012-12-14 
DropDownList 二级联动 保存时 第二个DropDownList被刷新自己想做个DropDownList 二级联动用户控件,但是在

DropDownList 二级联动 保存时 第二个DropDownList 被刷新
自己想做个DropDownList 二级联动用户控件,但是在保存数据时,第二个DropDownList被刷新,请各位帮忙指点下
代码如下:
编辑页面:
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeFile="UserOpt.aspx.cs" Inherits="IBP_UserOpt" Title="无标题页" EnableEventValidation="false" %>
<%@ Register TagPrefix="uc1" TagName="OrgDep" Src="~/UserControl/OrgDep.ascx" %>
<%@ Register TagPrefix="title" TagName="menuTab" Src="~/UserControl/MenuTab.ascx" %>
<asp:Content ID="cWorkTitle" ContentPlaceHolderID="WorkTitle" runat="Server">
    <script src="../JS/Common.js" type="text/javascript"></script>
    <script src="../JS/jquery-1.7.2.js" type="text/javascript"></script>
</asp:Content>
<asp:Content ID="cWorkTop" ContentPlaceHolderID="WorkTop" runat="Server">
</asp:Content>
<asp:Content ID="cWorkMenu" ContentPlaceHolderID="WorkMenu" runat="Server">
    <title:menuTab ID="menuTab" runat="server" DefaultSelect="0" MenuName="角色信息" />
</asp:Content>
<asp:Content ID="cWorkButton" ContentPlaceHolderID="WorkButton" runat="Server">
    <asp:Button ID="btnSave" runat="server" CssClass="button2" Text="保 存" OnClick="btnSave_Click" />
    <input id="btnClose" type="button" class="button2" value="关闭" onclick="cm.CloseWindow()" />
</asp:Content>
<asp:Content ID="cWorkForm" ContentPlaceHolderID="WorkForm" runat="Server">
    <table cellspacing="0" cellpadding="2" border="0" width="100%">
        <tr>
            <td width="70px">
                姓名:
            </td>
            <td>
                <asp:TextBox ID="txtName" runat="server" CssClass="Line"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td width="70px">
                帐号:
            </td>
            <td>
                <asp:TextBox ID="txtLoginName" runat="server" CssClass="Line"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td width="70px">
                性别:
            </td>
            <td>


                <asp:RadioButtonList ID="rdbSex" runat="server" RepeatDirection="Horizontal">
                    <asp:ListItem Text="男" Value="1" Selected="True"></asp:ListItem>
                    <asp:ListItem Text="女" Value="0"></asp:ListItem>
                </asp:RadioButtonList>
            </td>
        </tr>
        <tr>
            <td width="70px">
                角色:
            </td>
            <td >
                <asp:DropDownList ID="ddlRole" runat="server" Width="200px">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td width="70px">
                机构:
            </td>
            <td>
                <uc1:OrgDep id="orgdep" runat="server"></uc1:OrgDep>
            </td>
        </tr>
    </table>
</asp:Content>
<asp:Content ID="cWorkGrid" ContentPlaceHolderID="WorkGrid" runat="Server">
</asp:Content>
<asp:Content ID="cWorkEnd" ContentPlaceHolderID="WorkEnd" runat="Server">
</asp:Content>
<asp:Content ID="cWorkFooter" ContentPlaceHolderID="WorkFooter" runat="Server">
</asp:Content>

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Core.IBP;
using Core.Common;
public partial class IBP_UserOpt : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        ViewState["opt"] = Request["operation"] == null ? "view" : Request["operation"];
        ViewState["ID"] = Request["ID"] != null ? Request["ID"] : "0";


        InitData();
        InitInfo();
        InitPower();
    }
    private void InitData()
    {
        BaseSet.Initddl(ddlRole, IBP_T_Role.GetList(), false);
    }
    private void InitPower()
    {
        if (ViewState["opt"].ToString() == "view")
        {
            btnSave.Visible = false;
        }
    }
    private void InitInfo()
    {
        DataTable dt = IBP_T_User.GetInfo(ViewState["ID"].ToString());
        if (dt != null && dt.Rows.Count > 0)
        {
            txtName.Text = dt.Rows[0]["sName"].ToString();
            txtLoginName.Text = dt.Rows[0]["sLoginName"].ToString();
            rdbSex.SelectedValue = dt.Rows[0]["iSex"].ToString();
            ddlRole.SelectedValue = dt.Rows[0]["iRoleID"].ToString();
            orgdep.Orgid = dt.Rows[0]["iOrgid"].ToString();
            orgdep.Depid = dt.Rows[0]["iDepid"].ToString();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string mes = checkValue();
        if (mes.Length > 0)
        {
            this.ShowMessage(mes);
            return;
        }
        if (ViewState["ID"].ToString() == "0")
        {
            bool b = IBP_T_User.CheckSameName(txtLoginName.Text);
            if (b)
            {
                this.SaveClose("该帐号已存在");
                return;
            }
            b = IBP_T_User.Insert(txtLoginName.Text, txtName.Text, int.Parse(rdbSex.SelectedValue), int.Parse(ddlRole.SelectedValue), int.Parse(orgdep.Orgid), int.Parse(orgdep.Depid));
            if (b)


                this.SaveClose("添加成功");
            else
                this.SaveClose("添加失败");
        }
        else
        {
            bool b = IBP_T_User.Save(ViewState["ID"].ToString(), txtLoginName.Text, txtName.Text, int.Parse(rdbSex.SelectedValue), int.Parse(ddlRole.SelectedValue),
               int.Parse(orgdep.Orgid), int.Parse(orgdep.Depid));
            if (b)
                this.SaveClose("保存成功");
            else
                this.SaveClose("保存失败");
        }
    }
    private string checkValue()
    {
        string mes = string.Empty;
        if (txtName.Text.Length == 0)
        {
            mes += "名称不能为空\n";
        }
        if (txtLoginName.Text.Length == 0)
        {
            mes += "账号不能为空\n";
        }
        return mes;
    }
}

[解决办法]

用户控件页面:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="OrgDep.ascx.cs" Inherits="UserControl_OrgDep" %>
<table cellspacing="0" cellpadding="0" width="100%">
    <tr>
        <td>
            <asp:DropDownList ID="ddlOrg" runat="server" onchange="GetDep()">
            </asp:DropDownList>
            <asp:DropDownList ID="ddlDep" runat="server">
            </asp:DropDownList>
        </td>
    </tr>
</table>
<script type="text/javascript">
function GetDep() {
    var id=$("#ctl00_WorkForm_orgdep_ddlOrg").val();
    $("#ctl00_WorkForm_orgdep_ddlDep").html("");
    $.getJSON("../UserControl/Handler.ashx?action=GetDepList&Orgid=" + id, null, function(json) {
        $.each(json, function(i) { 
            $("#ctl00_WorkForm_orgdep_ddlDep").append($("<option></option>").val(json[i].pk_id).html(json[i].sName)) 


        });
    });
    $("<option></option>").val("-1").html("请选择").appendTo("#ctl00_WorkForm_orgdep_ddlDep");
}
</script>

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Core.Common;
public partial class UserControl_OrgDep : System.Web.UI.UserControl
{
    protected override void OnInit(EventArgs e)
    {
        if (Request.Form["ctl00$WorkForm$orgdep$ddlOrg"] != null)
        {
            InitOrg();
            ddlOrg.ClearSelection();
            ddlOrg.SelectedValue = Request.Form["ctl00$WorkForm$orgdep$ddlOrg"].ToString();
            InitDep();
            ddlDep.ClearSelection();
            ddlDep.SelectedValue = Request.Form["ctl00$WorkForm$orgdep$ddlDep"].ToString();
            return;
        }
        InitOrg();
        InitDep();
        base.OnInit(e);
    }
    public void DataBound()
    {
        InitOrg();
        InitDep();
    }
    private void InitOrg()
    {
        string sql = "select * from IBP_T_Organization where 1=1";
        string UserID = SessionUtil.UserID;
        if (UserID != "1")
        {
            sql += " and pk_id in (select iOrgid from IBP_T_User pk_id=" + UserID + " )";
        }

        DataTable dt = EQCCD.DBManagement.DBManager.FillDataTable(sql);
        ddlOrg.DataSource = dt;
        ddlOrg.DataTextField = "sName";
        ddlOrg.DataValueField = "PK_ID";
        ddlOrg.DataBind();
        ddlOrg.Items.Insert(0, new ListItem("请选择", "-1"));
    }
    private void InitDep()
    {
        string sql = "select * from IBP_T_Department where fk_orgid=" + ddlOrg.SelectedValue;


        DataTable dt = EQCCD.DBManagement.DBManager.FillDataTable(sql);
        ddlDep.DataSource = dt;
        ddlDep.DataTextField = "sName";
        ddlDep.DataValueField = "PK_ID";
        ddlDep.DataBind();
        ddlDep.Items.Insert(0, new ListItem("请选择", "-1"));
    }
    public string Orgid
    {
        get { return ddlOrg.SelectedValue; }
        set
        {
            if (ddlOrg.Items.Count > 0)
            {
                ddlOrg.ClearSelection();
                ddlOrg.SelectedValue = value;
                InitDep();
            }
        }
    }
    public string Depid
    {
        get { return ddlDep.SelectedValue; }
        set
        {
            if (ddlDep.Items.Count > 0)
            {
                ddlDep.ClearSelection();
                ddlDep.SelectedValue = value;
            }
        }
    }
}
Handler.ashx页面
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Text;
using System.Data;
using System.Collections.Generic;
public class Handler : IHttpHandler
{
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    public void ProcessRequest(HttpContext context)
    {
        string value=string.Empty;
        string key = context.Request.QueryString["action"];
        var dictionary = new Dictionary<string, Func<HttpContext, string>>();

        dictionary.Add("GetDepList", GetDepList);//获取部门列表

        dictionary.Add("GetOrgList", GetOrgList);//获取机构列表


        
        if(dictionary.ContainsKey(key))
        {
           value=dictionary[key].Invoke(context);
        }
        context.Response.Write(value);   
    }
    private string GetDepList(HttpContext context)
    {
        StringBuilder sb = new StringBuilder();
        string Orgid = context.Request.QueryString["Orgid"].ToString();
        string sql = "select PK_ID,sName from dbo.IBP_T_Department where fk_orgid=" + Orgid;
        DataTable dt = EQCCD.DBManagement.DBManager.FillDataTable(sql);
        if (dt == null 
[解决办法]
 dt.Rows.Count == 0)
        {
            return "";
        }
        else
        {
            sb.Append("[");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //返回JOSN数据
                sb.Append("{"pk_id":"" + dt.Rows[i]["pk_id"].ToString() + "","sName":"" + dt.Rows[i]["sName"].ToString() + ""},");
            }
            sb.Remove(sb.Length - 1, 1);
            sb.Append("]");
        }
        return sb.ToString();
    }
    private string GetOrgList(HttpContext context)
    {
        StringBuilder sb = new StringBuilder();
        string UserID = context.Request.QueryString["UserID"].ToString();   
        string sql = "select * from IBP_T_Organization where 1=1";
        if (UserID != "1")
        {
            sql += " and pk_id in (select iOrgid from IBP_T_User pk_id=" + UserID + " )";
        }
        DataTable dt = EQCCD.DBManagement.DBManager.FillDataTable(sql);
        if (dt == null 


[解决办法]
 dt.Rows.Count == 0)
        {
            return "";
        }
        else
        {
            sb.Append("[");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //返回JOSN数据
                sb.Append("{"pk_id":"" + dt.Rows[i]["pk_id"].ToString() + "","sName":"" + dt.Rows[i]["sName"].ToString() + ""},");
            }
            sb.Remove(sb.Length - 1, 1);
            sb.Append("]");
        }
        return sb.ToString();
    }
}

热点排行