子窗口選擇多值返回至父窗口的文本框中


本次開發的專案中,有涉及至讓步用戶在子窗口選擇一個或多個值之后,並返回至父窗口的文本框中。開發環境是Windows8 64bit + vs2012 + asp.net 4.5+ Ajax。下面是gif演示:

 

用戶可以根據不同的品號選擇,出現相對應的異常編號可供選擇。這部分當然還有另外的功能,是用戶首先是對品號對異常編號分配與綁定好。異常描述這個文本框,設為只讀,也就是不讓用戶手動去更改。只能讓用戶選擇來更改文本框的值。

品號的下拉框菜單,設好一個屬性與一個事件 AutoPostBack="true" OnSelectedIndexChanged="DropDownListPinHao_SelectedIndexChanged"

DropDownListPinHao_SelectedIndexChanged
  protected  void DropDownListPinHao_SelectedIndexChanged( object sender, EventArgs e)
    {
        DropDownList DDL = (DropDownList)sender;
         // 如果用戶設有選擇任何,return這個事件
         if (DDL.SelectedIndex == - 1return
         // 以下是為子窗口的Repeater控件綁定數據。
        objExceptional.PinHaoId = ConvertData.ToSmallInt(DDL.SelectedItem.Value);
         this.RepeaterobjExceptionalList.DataSource = objExceptional.GetExceptionalByPinHaoPrimaryKey();
         this.RepeaterobjExceptionalList.DataBind();
    }

 

異常文本框,為了顯示子窗口,使用了Ajax的 ajaxToolkit:ModalPopupExtender控件。詳細Html代碼如下,

View Code
 <asp:TextBox ID= " TextBoxExceptionalDescription " runat= " server " CssClass= " textbox "
                                        Width= " 99% " ReadOnly= " true "></asp:TextBox>                                   
                                    <asp:Panel ID= " pnlPopupWindown " runat= " server " Style= " display: none; background-color: #ffffdd; border-width: 3px; border-style: solid; border-color: Gray; padding: 3px; width: 500px; ">
                                        <asp:Panel ID= " Panel3 " runat= " server " Style= " float: left; margin-bottom: 5px; cursor: move; background-color: #DDDDDD; border: solid 1px Gray; color: Black; height: 20px; width: 475px; text-align: center; line-height: 20px; ">
                                            異常項目列表
                                        </asp:Panel>
                                        <asp:Panel ID= " Panel4 " runat= " server " Style= " float: right; margin-bottom: 5px; background-color: #DDDDDD; border: solid 1px Gray; color: Black; height: 20px; text-align: center; line-height: 20px; ">
                                            <asp:LinkButton ID= " btnClose " runat= " server " Style= " margin-right: 4px; margin-left: 4px; "
                                                OnClientClick= " return false; " Text= " × " ForeColor= " Red " ToolTip= " Close " />
                                        </asp:Panel>
                                        <div>
                                            <asp:Panel ID= " Panel1 " runat= " server " ScrollBars= " Vertical " Height= " 199px " Width= " 100% ">
                                                <asp:Repeater ID= " RepeaterobjExceptionalList " runat= " server ">
                                                    <HeaderTemplate>
                                                        <table border= " 1 " cellpadding= " 1 " cellspacing= " 0 " width= " 96% ">
                                                            <tr>
                                                                <td>&nbsp; </td>
                                                                <td>ID</td>
                                                                <td>Exceptional Name </td>
                                                            </tr>
                                                    </HeaderTemplate>
                                                    <ItemTemplate>
                                                        <tr id= " tr1 " runat= " server " style= " height: 10px; line-height: 10px; ">
                                                            <td style= " width: 8px; ">
                                                                <asp:CheckBox ID= " CheckBoxId " runat= " server " onclick= " Javascript:changeRowBgColor(this) " />
                                                            </td>
                                                            <td>
                                                                <asp:Label ID= " Label_nbr " runat= " server " Text= ' <%# Eval("Exceptional_nbr")%> '></asp:Label>
                                                            </td>
                                                            <td>
                                                                <asp:Label ID= " label_Name " runat= " server " Text= '  <%# Eval("ExceptionalName")%> '></asp:Label>
                                                            </td>
                                                        </tr>
                                                    </ItemTemplate>
                                                    <FooterTemplate>
                                                        </table>
                                                    </FooterTemplate>
                                                </asp:Repeater>
                                            </asp:Panel>
                                            <div style= " height: 3px; ">
                                            </div>
                                            <asp:Panel ID= " Panel2 " runat= " server ">
                                                <asp:Button ID= " btnSelected " runat= " server " OnClick= " btnSelected_Click " Text= " 選擇 "
                                                    CausesValidation= " false " CssClass= " button " />
                                            </asp:Panel>
                                        </div>
                                    </asp:Panel>
                                    <ajaxToolkit:ModalPopupExtender ID= " ModalPopupExtender1 " runat= " server " TargetControlID= " TextBoxExceptionalDescription "
                                        PopupControlID= " pnlPopupWindown " BackgroundCssClass= " modalBackground " CancelControlID= " btnClose "
                                        DropShadow= " true " PopupDragHandleControlID= " Panel3 " />

 

還有一段Ajax的Javascript代碼,需要帖至網頁的結尾處。 如果同一網頁中有多個子窗口,可以共用這段JavaScript 代碼:

View Code
  <script type="text/javascript">
                 function setBodyHeightToContentHeight() {
                    document.body.style.height = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight) + "px";
                }
                setBodyHeightToContentHeight();
                $addHandler(window, "resize", setBodyHeightToContentHeight);
            </script>

 

 為了保存用戶選擇的值,也就是說,用戶沒有真正保存入數據庫,需要存儲臨時的選擇值。因此你需要在程序中,宣告一個全局static屬性,本例中Insus.NET是寫在異常[Exceptional]類別中。

View Code
  public  static SortedList< intstring> ExceptionalCollection
        {
             get
            {
                 if (HttpContext.Current.Session[ " ExceptionalCollection "] !=  null)
                {
                     return (SortedList< intstring>)HttpContext.Current.Session[ " ExceptionalCollection "];
                }
                 else
                {
                     return  new SortedList< intstring>();
                }
            }
             set
            {
                HttpContext.Current.Session[ " ExceptionalCollection "] = value;
            }
        }

 

接下來,我們看看選擇按鈕的事件:

View Code
  SortedList< intstring> ExceptionalCollection =  new SortedList< intstring>();

     protected  void btnSelected_Click( object sender, EventArgs e)
    {
         // 循環Repeater控件
         foreach (RepeaterItem item  in  this.RepeaterobjExceptionalList.Items)
        {
            // 只對Item和AlternatingItem數據行進行處理
             if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                // 判斷相關的控件是否為空,避免當控件為空,產生異常規性異常。
                 if (item.FindControl( " tr1 ") !=  null && item.FindControl( " CheckBoxId ") !=  null && item.FindControl( " Label_nbr ") !=  null && item.FindControl( " label_Name ") !=  null)
                {
                     // 找出相關的控件
                    HtmlTableRow htr = (HtmlTableRow)item.FindControl( " tr1 ");
                     var cb = item.FindControl( " CheckBoxId "as CheckBox;
                     var lblnbr = item.FindControl( " Label_nbr "as Label;
                     var lblname = item.FindControl( " label_Name "as Label;

                     // 獲取相關控件的值
                     short nbr = Convert.ToInt16(lblnbr.Text);
                     string name = lblname.Text;

                     // 如果用戶選擇了和集合中沒有包含有對應的值,更改背景顏色和選擇的值存入集合中。
                     if (cb.Checked && !ExceptionalCollection.ContainsKey(nbr))
                    {
                        htr.Attributes.CssStyle.Add( " background-color "" #ffdab9 ");
                        ExceptionalCollection.Add(nbr, name);
                    }
                     // 如果用戶沒作選擇和集合中包含有對應的值,更改背景顏色默認顏色和從集合移除相對應的值。
                     if (!cb.Checked && ExceptionalCollection.ContainsKey(nbr))
                    {
                        htr.Attributes.CssStyle.Add( " background-color """);
                        ExceptionalCollection.Remove(nbr);
                    }

                    // 最后是更新靜態屬性。
                    Exceptional.ExceptionalCollection = ExceptionalCollection;
                }
            }
        }

         string ed_Value =  string.Empty;

         // 循環集合
         foreach (KeyValuePair< intstring> kvp  in ExceptionalCollection)
        {
            ed_Value +=  " " + kvp.Value;
        }

         // 如果集合有值,截除開始的兩個字符"; "。
         if (ed_Value.Length >  0)
            ed_Value = ed_Value.Substring( 2);

         // 把值賦給文本框。
         this.TextBoxExceptionalDescription.Text = ed_Value;
    }

 

本例只作為備忘,因此Insus.NET只供重點部分代碼,直接帖至專案中,有可能不能直接運行。如果任何問題與意見,可以留言。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM