DropDownList控件運用

       當我們利用DropDownList控件,我們會需要根據上一級的編號,來靈活的指定下一級的編號,或者是初始化下一級的數據信息。在正常的情況下,我們的數據時正常的顯示,如果我們對上一級的信息信息了因此,也就是說,作爲下一級的數據源發上了變化,但是我們的信息已經是已經保存了,當我們還是這樣的去加載信息,肯定爲因爲我們的數據集中不存在這一項,從而導致了運行頁面的錯誤信息。
例如,下面的是我們需要調用父級的數據源信息,數據表結構如下所示:
--=============================================================================
/****** 對象:表 dbo.FN_Subject ******/
/****** 說明:憑證管理-科目管理******/
--=============================================================================
If Exists (Select * From sysobjects where id = object_id('dbo.FN_Subject'))
Drop table dbo.FN_Subject
GO
Create Table dbo.FN_Subject (
    SubCode 
varchar(10Not Null,        --科目代碼
    SubName varchar(50Not Null,        --科目名稱
    SubEName varchar(50Null,        --科目英文名稱
    SubType varchar(10Not Null,        --科目類型(往來借、往來貨、應收帳款、營業費用、
                        --管理費用、財務費用、產品銷售、進項稅、銷項稅
                        --其它稅、普通
    SubInter varchar(10Not Null,        --科目所屬(資產類、負債類、權益類、損益類)
    UseDirec varchar(10Not Null,        --可用方向(借貨方、僅借方、僅貸方)
    PreCode varchar(10Null,            --上級代碼;
    IfTop bit Not Null,            --最高級標識
    BalanceDirec varchar(10Not Null,    --餘額方向(借方、貨方)
    IfUsing bit Not Null,            --啓用標識
    IfCapital bit Not null,            --資金實績標識
    --顯示在頁面,但不用輸入,通過默認值寫入數據庫
    RegDate smalldatetime Not null        --登記日期
)
GO
Alter table dbo.FN_Subject with Nocheck Add
    
Constraint PK_FN_Subject Primary Key Clustered
    (
        SubCode
    )
GO
ALTER TABLE dbo.FN_Subject ADD 
    
CONSTRAINT DF_FN_Subject_RegDate DEFAULT (getdate()) FOR RegDate
GO
我們的下一級代碼需要利用上面表中的SubCode,SubName作爲DropDownList的數據集,條件是,判斷IfUsing=0或者是等於1。當我們進行添加操作的時候,因爲是添加操作,因此信息不會出錯,以下代碼是需要調用上級的SubCode的數據表的結構:
--=============================================================================
/**//****** 對象:表 dbo.FN_Catalog ******/
/**//****** 說明:憑證管理-項目管理******/
--=============================================================================
If Exists (Select * From sysobjects where id = object_id('dbo.FN_Catalog'))
Drop table dbo.FN_Catalog
GO
Create Table dbo.FN_Catalog (
    FnCode 
varchar(10Not null,        --憑證代碼
    FnName varchar(50Not Null,        --憑證名稱
    Debit1 varchar(10Not Null,        --借方科目1
    Debit2 varchar(10Null,        --借方科目2
    Debit3 varchar(10Null,        --借方科目3
    Credit1 varchar(10Not Null,        --貨方科目1
    Credit2 varchar(10Null,        --貨方科目2
    Credit3 varchar(10Null,        --貨方科目3
)
GO
Alter table dbo.FN_Catalog with Nocheck Add
    
Constraint PK_FN_Catalog Primary Key Clustered
    (
        FnCode
    )
GO

表結構中的Debit1,Debit2,Debit3,Credit1,Credit2,Credit3填充的值是FN_Subject 表中的SubCode值。
添加操作,一般不會出現什麼問題,現在關鍵把編輯時,進行加載的進行說明,因爲是關鍵,先將編輯頁面的HTML代碼帖出來(CSDN添加圖片比較的難,因此只能把HTMLCopy出來):

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Catalogedit.aspx.cs" StylesheetTheme="Default"
    Inherits
="Admin_Catalogedit" 
%>

<!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>
</head>
<body>
    
<form id="form1" runat="server">
        
<fieldset>
            
<legend>菜單</legend>
            
<div id="tabsF">
                
<ul>
                    
<li><href="FNCatalog.aspx" title="科目數據列表"><span>科目數據列表</span></a></li>
                    
<li><href="Default.aspx" title="返回"><span>返回</span></a></li>
                
</ul>
            
</div>
        
</fieldset>
        
<div>
            
<fieldset>
                
<legend>科目管理詳細信息</legend>
                
<table cellpadding="4" style="width: 100%">
                    
<tr>
                        
<td align="left" class="tdbg">
                            
<span style="color: #ff0000">*</span>憑證編號:</td>
                        
<td align="left">
                            
<asp:TextBox ID="txtfnCode" runat="server" ReadOnly="True"></asp:TextBox>
                            
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtfnCode"
                                ErrorMessage
="憑證編號不能爲空">*</asp:RequiredFieldValidator></td>
                        
<td align="left" class="tdbg">
                            
<span style="color: #ff0000">*</span>憑證名稱:</td>
                        
<td align="left">
                            
<asp:TextBox ID="txtfnName" runat="server"></asp:TextBox>
                            
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtfnName"
                                ErrorMessage
="憑證名稱不能爲空">*</asp:RequiredFieldValidator></td>
                    
</tr>
                    
<tr>
                
<td align="left" class="tdbg">
                    
<span style="color: #ff0000">*</span>借方科目1:</td>
                
<td align="left">
                    
<asp:DropDownList ID="txtDebit1" runat="server"/><asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtDebit1"
                        ErrorMessage
="借方科目1不能爲空">*</asp:RequiredFieldValidator></td>
                
<td align="left" class="tdbg">
                    
<span style="color: #ff0000">*</span>貨方科目1:</td>
                
<td align="left">
                    
<asp:DropDownList ID="txtCredit1" runat="server"/><asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtCredit1"
                        ErrorMessage
="貸方科目1不能爲空">*</asp:RequiredFieldValidator></td>
            
</tr>
            
<tr>
                
<td align="left" class="tdbg">
                    借方科目2:
</td>
                
<td align="left">
                    
<asp:DropDownList ID="txtDebit2" runat="server"/></td>
                
<td align="left" class="tdbg">
                    貨方科目2:
                
</td>
                
<td align="left">
                    
<asp:DropDownList ID="txtCredit2" runat="server"/></td>
            
</tr>
            
<tr>
                
<td align="left" class="tdbg">
                    
<span style="color: #000000; background-color: #edf2fc;">
                    借方科目3:
</span></td>
                
<td align="left">
                    
<asp:DropDownList ID="txtDebit3" runat="server"/></td>
                
<td align="left" class="tdbg">
                    貨方科目3:
</td>
                
<td align="left">
                    
<asp:DropDownList ID="txtCredit3" runat="server"/></td>
            
</tr>
                    
<tr>
                        
<td align="center" colspan="4">
                            
<asp:Button ID="btnAdd" runat="server" Text="更新" OnClick="btnAdd_Click" /></td>
                    
</tr>
                
</table>
            
</fieldset>
        
</div>
        
<fieldset style="text-align: left">
            
<legend>業務邏輯</legend>
            
<ul>
                
<li><span style="color: red">*</span>項不能爲空</li>
            
</ul>
        
</fieldset>
        
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"
            ShowSummary
="False" />
    
</form>
</body>
</html>
以下是頁面對應的後臺編碼:
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;

public partial class Admin_Catalogedit : System.Web.UI.Page
{
    WebUtility.FNCataLog cata;
    WebUtility.FNSubject subject;
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!Page.IsPostBack)
        
{
            
string fncode = Request.QueryString["fncode"].ToString();
            cata 
= new WebUtility.FNCataLog(fncode);
            subject 
= new WebUtility.FNSubject();

            
this.txtfnCode.Text = cata.FNCode;
            
this.txtfnName.Text = cata.FNName;

            
this.txtCredit1.DataSource = subject.DataSetSubject(WebUtility.FNSubject.UseDirect.貸方);
            
this.txtCredit1.DataTextField = "SubName";
            
this.txtCredit1.DataValueField = "SubCode";
            
this.txtCredit1.DataBind();
            
this.txtCredit1.Items.Insert(0new ListItem("--請選擇--"""));

            
this.txtCredit2.DataSource = subject.DataSetSubject(WebUtility.FNSubject.UseDirect.貸方);
            
this.txtCredit2.DataTextField = "SubName";
            
this.txtCredit2.DataValueField = "SubCode";
            
this.txtCredit2.DataBind();
            
this.txtCredit2.Items.Insert(0new ListItem("--請選擇--"""));

            
this.txtCredit3.DataSource = subject.DataSetSubject(WebUtility.FNSubject.UseDirect.貸方);
            
this.txtCredit3.DataTextField = "SubName";
            
this.txtCredit3.DataValueField = "SubCode";
            
this.txtCredit3.DataBind();
            
this.txtCredit3.Items.Insert(0new ListItem("--請選擇--"""));

            
this.txtDebit1.DataSource = subject.DataSetSubject(WebUtility.FNSubject.UseDirect.借方);
            
this.txtDebit1.DataTextField = "SubName";
            
this.txtDebit1.DataValueField = "SubCode";
            
this.txtDebit1.DataBind();
            
this.txtDebit1.Items.Insert(0new ListItem("--請選擇"""));

            
this.txtDebit2.DataSource = subject.DataSetSubject(WebUtility.FNSubject.UseDirect.借方);
            
this.txtDebit2.DataTextField = "SubName";
            
this.txtDebit2.DataValueField = "SubCode";
            
this.txtDebit2.DataBind();
            
this.txtDebit2.Items.Insert(0new ListItem("--請選擇"""));

            
this.txtDebit3.DataSource = subject.DataSetSubject(WebUtility.FNSubject.UseDirect.借方);
            
this.txtDebit3.DataTextField = "SubName";
            
this.txtDebit3.DataValueField = "SubCode";
            
this.txtDebit3.DataBind();
            
this.txtDebit3.Items.Insert(0new ListItem("--請選擇"""));


            
//this.txtCredit1.Items.FindByValue(cata.Credit1).Selected = true;

            
//判斷是否存在值,不存在,將設置爲請選擇狀態
            
//原因可能是以前使用的一項已經被禁止了

            
this.BeginExecuteCircle(this.txtCredit1,cata.Credit1);
            
this.BeginExecuteCircle(this.txtCredit2,cata.Credit2);
            
this.BeginExecuteCircle(this.txtCredit3,cata.Credit3);
            
this.BeginExecuteCircle(this.txtDebit1,cata.Debit1);
            
this.BeginExecuteCircle(this.txtDebit2,cata.Debit2);
            
this.BeginExecuteCircle(this.txtDebit3,cata.Debit3);
        }

        
    }


    
private void BeginExecuteCircle(DropDownList ddlList,string validateValue)
    
{
        
int count = 0;
        ListItem GetItem
=new ListItem();
        
foreach (ListItem item in ddlList.Items)
        
{
            
if (validateValue == item.Value)
            
{
                count 
= 1;
                GetItem 
= item;
            }

        }

        
if (count > 0)
        
{
            
this.SetDropDownListStat(true, ddlList, GetItem);
        }

        
else
        
{
            
this.SetDropDownListStat(false, ddlList, GetItem);
        }

        
    }


    
private void SetDropDownListStat(bool IsValdate, DropDownList ddl, ListItem NO)
    
{
        
if (IsValdate)
        
{
            ddl.Items.FindByValue(NO.Value).Selected 
= true;
        }

        
else
        
{
            ddl.Items[
0].Selected = true;
        }

    }


    
protected void btnAdd_Click(object sender, EventArgs e)
    
{
        cata 
= new WebUtility.FNCataLog();
        cata.Credit1 
= this.txtCredit1.SelectedValue;
        cata.Credit2 
= this.txtCredit2.SelectedValue;
        cata.Credit3 
= this.txtCredit3.SelectedValue;
        cata.Debit1 
= this.txtDebit1.SelectedValue;
        cata.Debit2 
= this.txtDebit2.SelectedValue;
        cata.Debit3 
= this.txtDebit3.SelectedValue;
        cata.FNCode 
= this.txtfnCode.Text;
        cata.FNName 
= this.txtfnName.Text;

        
int i = cata.Update();
        WebUtility.Define.MessageBox(
this.Page, i);
    }

}

       上面的加重斜體紅色的字,是我以前用的方法,怎麼說呢,比較省事,可是在上級的查詢條件IFUsing=1 如果是這條數據已經存在了,現在查詢不能把這個這一項加載上去,但是從數據庫讀出來的卻有這項值,因此加載頁面就會出錯。那句話我已經註釋掉了,改用下面的方法,通過傳遞參數進行循環判斷,如果加載的數據源中存在從數據庫中讀出來的值,將該值加載上去,如果不存在將默認的第0項進行加載。
      其實,這也不是高明之處,只是在工作中遇到的問題。認真細緻的對待每一個問題,不要偷懶,相信已經能把工作做好。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章