从上面一个例子 子窗口选择多值返回至父窗口的文本框中 中看到,用户看到的和选择的始终是友善的数据。我们不能可能选择的是名称,显示的是ID,或是存入数据库的是ID,显示的还是ID。下图是沿继上一例子,进入编辑状态。需要把存在数据库的数据显示出来。在子窗口的记录,把已经选择的值,还是实现Highlight出来。
存在数据库中的数据,只是存入异常编码的ID,用";"分隔。
现在我们要处理这个ID,转换为真实名称,并显示于编辑页面的文本框中。我们在数据库(本演示数据实例是SQL Server2012)中,写一个函数:
View Code
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[udf_ExceptionalDescription]
(
@ExceptionalDescription NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
--宣告一个表变量
DECLARE @t AS TABLE ([en] NVARCHAR(MAX))
--使用一个自定义函数,处理分隔符的字符串,并转为表记录。然后与异常表的记录JOIN起来。这样就可以获取异常名称。
-- http://www.cnblogs.com/insus/archive/2012/02/26/2368283.html
INSERT INTO @t SELECT [ExceptionalName] FROM [dbo].[udf_SplitStringToTable](@ExceptionalDescription,';') AS nbr LEFT JOIN [dbo].[Exceptional] AS e ON (nbr.[KeyWord] = e.[Exceptional_nbr])
RETURN (SELECT STUFF((SELECT '; ' + CAST([en] AS NVARCHAR(MAX))
FROM @t FOR XML PATH ('')),1,2,''))
END
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[udf_ExceptionalDescription]
(
@ExceptionalDescription NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
--宣告一个表变量
DECLARE @t AS TABLE ([en] NVARCHAR(MAX))
--使用一个自定义函数,处理分隔符的字符串,并转为表记录。然后与异常表的记录JOIN起来。这样就可以获取异常名称。
-- http://www.cnblogs.com/insus/archive/2012/02/26/2368283.html
INSERT INTO @t SELECT [ExceptionalName] FROM [dbo].[udf_SplitStringToTable](@ExceptionalDescription,';') AS nbr LEFT JOIN [dbo].[Exceptional] AS e ON (nbr.[KeyWord] = e.[Exceptional_nbr])
RETURN (SELECT STUFF((SELECT '; ' + CAST([en] AS NVARCHAR(MAX))
FROM @t FOR XML PATH ('')),1,2,''))
END
下面尝试运行一个这个函数。
这样子,我们就可以在编辑的记录的SELECT时,使用到这个函数了。
ok,前台绑定文本框时,
this.TextBoxExceptionalDescription.Text = objDataRow["ED_Collections"].ToString();
接下来,我们需要处理子窗的数据与选中的值并Highlight至Repeater控件中。我们首选要让系统知道已经有哪些记录是选中的值(也就是文本框的值)。
View Code
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_Exceptional_GetByPinZhongPrimaryKey]
(
@PinZhongId TINYINT,
@Selected NVARCHAR(MAX)
)
AS
--宣告一个表变量,其中一个字段[Opt],就是为文本框的值准备。如果有其值,那这个字段值为1,反之为0
DECLARE @t AS TABLE([Opt] BIT DEFAULT(0),[Key_Id] INT)
--把文本框的值拆分并插入这个表变量中。有关这个函数,可以参考 http://www.cnblogs.com/insus/archive/2012/02/26/2368283.html
INSERT INTO @t SELECT 1, [KeyWord] FROM udf_SplitStringToTable(REPLACE(@Selected,'; ',';'),';')
IF OBJECT_ID('#exceptional') IS NOT NULL
DROP TABLE #exceptional
CREATE TABLE #exceptional(
[Exceptional_nbr] [smallint],
[ExceptionalName] [nvarchar](50),
[Description] [nvarchar](100),
[IsActive] [bit],
)
INSERT INTO #exceptional SELECT e.[Exceptional_nbr],[ExceptionalName],e.[Description],[IsActive] FROM [dbo].[AllocationExceptional] AS ae
INNER JOIN [dbo].[Exceptional] AS e ON (ae.[Exceptional_nbr] = e.[Exceptional_nbr] AND ae.[IsEnable] = 1 AND ae.[PinZhongId] = @PinZhongId)
--下面的SELECT语句所得到的结果,就是前台子窗口的Repeater控件的数据源。
SELECT [Exceptional_nbr],[ExceptionalName],[Description],[IsActive], (CASE [Opt] WHEN 1 THEN 1 WHEN NULL THEN 0 ELSE 0 END) AS [Opt] FROM #exceptional AS e
LEFT JOIN @t AS t ON (e.[Exceptional_nbr] = t.[Key_Id])
WHERE [IsActive] = 1
ORDER BY [ExceptionalName]
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_Exceptional_GetByPinZhongPrimaryKey]
(
@PinZhongId TINYINT,
@Selected NVARCHAR(MAX)
)
AS
--宣告一个表变量,其中一个字段[Opt],就是为文本框的值准备。如果有其值,那这个字段值为1,反之为0
DECLARE @t AS TABLE([Opt] BIT DEFAULT(0),[Key_Id] INT)
--把文本框的值拆分并插入这个表变量中。有关这个函数,可以参考 http://www.cnblogs.com/insus/archive/2012/02/26/2368283.html
INSERT INTO @t SELECT 1, [KeyWord] FROM udf_SplitStringToTable(REPLACE(@Selected,'; ',';'),';')
IF OBJECT_ID('#exceptional') IS NOT NULL
DROP TABLE #exceptional
CREATE TABLE #exceptional(
[Exceptional_nbr] [smallint],
[ExceptionalName] [nvarchar](50),
[Description] [nvarchar](100),
[IsActive] [bit],
)
INSERT INTO #exceptional SELECT e.[Exceptional_nbr],[ExceptionalName],e.[Description],[IsActive] FROM [dbo].[AllocationExceptional] AS ae
INNER JOIN [dbo].[Exceptional] AS e ON (ae.[Exceptional_nbr] = e.[Exceptional_nbr] AND ae.[IsEnable] = 1 AND ae.[PinZhongId] = @PinZhongId)
--下面的SELECT语句所得到的结果,就是前台子窗口的Repeater控件的数据源。
SELECT [Exceptional_nbr],[ExceptionalName],[Description],[IsActive], (CASE [Opt] WHEN 1 THEN 1 WHEN NULL THEN 0 ELSE 0 END) AS [Opt] FROM #exceptional AS e
LEFT JOIN @t AS t ON (e.[Exceptional_nbr] = t.[Key_Id])
WHERE [IsActive] = 1
ORDER BY [ExceptionalName]
在前台的编辑页面中,我就可以参考下面的方法进行对子窗口的Repeater控件数据绑定,由于多个地方绑定数据源,因此Insus.NET重构了这个方法,然后在Page_Load,拉号下拉菜单时,都可以使用到这个方法,这样不必在这两个地方,都写同样的代码。
View Code
private void ExceptionalDataBinding(string pinHaoId)
{
objExceptional.PinHaoId = ConvertData.ToSmallInt(pinHaoId);
objExceptional.ExceptionalCollections = Session["ExceptionalCollections"].ToString();
this.RepeaterobjExceptionalList.DataSource = objExceptional.GetExceptionalByPinHaoPrimaryKeyAndOption();
this.RepeaterobjExceptionalList.DataBind();
}
{
objExceptional.PinHaoId = ConvertData.ToSmallInt(pinHaoId);
objExceptional.ExceptionalCollections = Session["ExceptionalCollections"].ToString();
this.RepeaterobjExceptionalList.DataSource = objExceptional.GetExceptionalByPinHaoPrimaryKeyAndOption();
this.RepeaterobjExceptionalList.DataBind();
}
在Page_Load事件中写:
Session["ExceptionalCollections"] = objDataRow["ExceptionalDescription"].ToString();
ExceptionalDataBinding(objDataRow["PinHaoId"].ToString());
ExceptionalDataBinding(objDataRow["PinHaoId"].ToString());
在下拉菜单中的选择事件中:
View Code
protected void DropDownListPinHao_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList DDL = (DropDownList)sender;
if (DDL.SelectedIndex == -1) return;
ExceptionalDataBinding(DDL.SelectedItem.Value);
}
{
DropDownList DDL = (DropDownList)sender;
if (DDL.SelectedIndex == -1) return;
ExceptionalDataBinding(DDL.SelectedItem.Value);
}
到此为止,只是数据绑定,我们要Highlight子窗口的记录。为子窗口的Repeater控件写一个事件:OnItemDataBound="RepeaterobjExceptionalList_ItemDataBound"
View Code
protected void RepeaterobjExceptionalList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
if (e.Item.FindControl("tr1") != null && e.Item.FindControl("CheckBoxId") != null)
{
HtmlTableRow htr = (HtmlTableRow)e.Item.FindControl("tr1");
CheckBox cb = (CheckBox)e.Item.FindControl("CheckBoxId");
if (drv["Opt"].ToString() == "1")
{
cb.Checked = true;
htr.Attributes.CssStyle.Add("background-color", "#ffdab9");
}
else
{
cb.Checked = false;
htr.Attributes.CssStyle.Add("background-color", "");
}
}
}
}
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
if (e.Item.FindControl("tr1") != null && e.Item.FindControl("CheckBoxId") != null)
{
HtmlTableRow htr = (HtmlTableRow)e.Item.FindControl("tr1");
CheckBox cb = (CheckBox)e.Item.FindControl("CheckBoxId");
if (drv["Opt"].ToString() == "1")
{
cb.Checked = true;
htr.Attributes.CssStyle.Add("background-color", "#ffdab9");
}
else
{
cb.Checked = false;
htr.Attributes.CssStyle.Add("background-color", "");
}
}
}
}
上面的事件中,就用上了数据库附添加的字段[Opt]。如果Opt的值是1,就设置行的背景色,反之设为""。
本例也只是提供大致的流程与重点代码。