Analytics

2017年2月14日 星期二

[ListView]使用 ListView維護表單 (Use the ListView to maintain the form)


問題
使用 ListView維護表單



解決方法
<%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI.WebControls" TagPrefix="asp" %>
.aspx
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
  <asp:UpdatePanel ID="UP_Modify" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:ListView ID="lv_Modify" runat="server" InsertItemPosition="FirstItem" EnableModelValidation="True"
                OnItemCanceling="lv_Modify_ItemCanceling" OnItemDeleting="lv_Modify_ItemDeleting"
                OnItemInserting="lv_Modify_ItemInserting" OnItemUpdating="lv_Modify_ItemUpdating"
                OnItemEditing="lv_Modify_ItemEditing" 
                OnItemDataBound="lv_Modify_ItemDataBound" onitemcreated="lv_Modify_ItemCreated">
                <LayoutTemplate>
                    <table id="Table1" runat="server">
                        <tr id="Tr1" runat="server">
                            <td id="Td1" runat="server">
                                <table id="itemPlaceholderContainer" runat="server" align="left" border="1" cellpadding="3"
                                    cellspacing="0" style="margin-left: 0px; border-collapse: collapse; border-color: #696969;
                                    font-size: small;">
                                    <tr id="Tr2" runat="server" style="color: #FFFFFF; background-color: #0000FF">
                                        <th id="Th2" runat="server">
                                            FUNCTION
                                        </th>
                                        <th id="Th19" runat="server">
                                            rule_seq
                                        </th>
                                        <th id="Th8" runat="server">
                                            system_key
                                        </th>
                                        <th id="Th20" runat="server">
                                            table_column
                                        </th>
                                        <th id="Th21" runat="server">
                                            column_value
                                        </th>
                                    </tr>
                                    <tr id="itemPlaceholder" runat="server">
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr id="Tr3" runat="server">
                            <td id="Td2" runat="server" style="">
                            </td>
                        </tr>
                    </table>
                </LayoutTemplate>
                <ItemTemplate>
                    <tr style="background-color: #FFFFCC">
                        <td>
                            <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="編輯" />
                            <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="刪除" />
                        </td>
                        <td>
                            <asp:Label ID="lblrule_seq" runat="server" Text='<%# Eval("rule_seq") %>' />
                        </td>
                        <td>
                            <asp:Label ID="lblsystem_key" runat="server" Text='<%# Eval("system_key") %>' />
                        </td>
                        <td>
                            <asp:DropDownList ID="ddltable_column" runat="server">
                            </asp:DropDownList>
                            <asp:HiddenField ID="hdtable_column" runat="server" Value='<%# Eval("table_column") %>' />
                        </td>
                        <td>
                            <asp:Label ID="lblcolumn_value" runat="server" Text='<%# Eval("column_value") %>' />
                        </td>
                    </tr>
                </ItemTemplate>
                <EditItemTemplate>
                    <tr style="background-color: #FFFF66">
                        <td>
                            <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />
                            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />
                        </td>
                        <td>
                            <asp:Label ID="lblrule_seq" runat="server" Text='<%# Eval("rule_seq") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="txtsystem_key" runat="server" Text='<%# Bind("system_key") %>' />
                        </td>
                        <td>
                            <asp:DropDownList ID="ddltable_column" runat="server">
                            </asp:DropDownList>
                            <asp:HiddenField ID="hdtable_column" runat="server" Value='<%# Eval("table_column") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="txtcolumn_value" runat="server" Text='<%# Bind("column_value") %>' />
                        </td>
                    </tr>
                </EditItemTemplate>
                <EmptyDataTemplate>
                    <table id="Table2" runat="server" style="">
                        <tr>
                            <td>
                                未傳回資料。
                            </td>
                        </tr>
                    </table>
                </EmptyDataTemplate>
                <InsertItemTemplate>
                    <tr style="background-color: #00FF00">
                        <td>
                            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" />
                            <asp:Button ID="CancelButton0" runat="server" CommandName="Cancel" Text="清除" />
                        </td>
                        <td>
                            系統處理
                        </td>
                        <td>
                            <asp:TextBox ID="txtsystem_key" runat="server" Text='<%# Bind("system_key") %>' />
                        </td>
                        <td>
                            <asp:DropDownList ID="ddltable_column" runat="server">
                            </asp:DropDownList>
                        </td>
                        <td>
                            <asp:TextBox ID="txtcolumn_value" runat="server" Text='<%# Bind("column_value") %>' />
                        </td>
                    </tr>
                </InsertItemTemplate>
            </asp:ListView>
        </ContentTemplate>
    </asp:UpdatePanel>
.cs
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            initRuleDropDownList();
            GetModifyData();
        }
    }
 protected void lv_Modify_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        lv_Modify.EditIndex = -1;
        DataBindListView(lv_Modify, GetData());
    }
 protected void lv_Modify_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        if (lv_Modify.EditIndex >= 0) return;
        ListViewDataItem DItem = lv_Modify.Items[e.ItemIndex];

        DBItem.obj1 obj = new DBItem.obj1();
        obj.rule_seq = Convert.ToInt32(((Label)DItem.FindControl("lblrule_seq")).Text.ToString().Trim());
        obj.system_key = ((Label)DItem.FindControl("lblsystem_key")).Text.ToString().Trim();
        obj.table_column = ((DropDownList)DItem.FindControl("ddltable_column")).SelectedValue.Trim();
        obj.column_value = ((Label)DItem.FindControl("lblcolumn_value")).Text.ToString().Trim();
        //process...
        //reget data
    }
 protected void lv_Modify_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        lv_Modify.EditIndex = e.NewEditIndex;
        DataBindListView(lv_Modify, GetData());
    }
 protected void lv_Modify_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        if (lv_Modify.EditIndex >= 0) return;

        string system_key = ((TextBox)e.Item.FindControl("txtsystem_key")).Text.ToString().Trim();
        string table_column = ((DropDownList)e.Item.FindControl("ddltable_column")).SelectedValue.Trim();
        string column_value = ((TextBox)e.Item.FindControl("txtcolumn_value")).Text.ToString().Trim();
        //process...
        //reget data
    }
 protected void lv_Modify_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        ListViewDataItem DItem = lv_Modify.Items[e.ItemIndex];
  
        string system_key = ((TextBox)DItem.FindControl("txtsystem_key")).Text.ToString().Trim();
        string table_column = ((DropDownList)DItem.FindControl("ddltable_column")).SelectedValue.Trim();
        string column_value = ((TextBox)DItem.FindControl("txtcolumn_value")).Text.ToString().Trim();
        int rule_seq = Convert.ToInt32(((Label)DItem.FindControl("lblrule_seq")).Text);
        //process...
        //reget data
    }
 private DataTable GetData()
    {
       return data....
    }
 protected void DataBindListView(ListView lv, Object list)
    {
        lv.DataSource = list;
        lv.DataBind();
    }
 protected void GetModifyData()
    {
        lv_Modify.EditIndex = -1;
        DataBindListView(lv_Modify, GetData());
    }
 private void initRuleDropDownList()
    {
        List<string> ruleList = dropdownlist source....
        ViewState["RuleDropDownList"] = ruleList;
    }
 protected void lv_Modify_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType != ListViewItemType.EmptyItem)
        {
            DropDownList ddl = (DropDownList)e.Item.FindControl("ddltable_column");
            if (ViewState["RuleDropDownList"] == null)
                initRuleDropDownList();
            ddl.DataSource = ViewState["RuleDropDownList"];
            ddl.DataBind();
        }
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            DropDownList ddl = (DropDownList)e.Item.FindControl("ddltable_column");
            HiddenField hd = (HiddenField)e.Item.FindControl("hdtable_column");
            ddl.SelectedValue = hd.Value;
        }
    }
 protected void lv_Modify_ItemCreated(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.InsertItem)
        {
            DropDownList ddl = (DropDownList)e.Item.FindControl("ddltable_column");
            if (ViewState["RuleDropDownList"] == null)
                initRuleDropDownList();
            ddl.DataSource = ViewState["RuleDropDownList"];
            ddl.DataBind();
        }
    }

沒有留言:

熱門文章