I have some troubles with the textbox or my brain. I can't understand what's the reason of unedittable textbox. I mean, i have listview with 3 columns:"Name","Code","Binding". By clicking on the "Name", the first and the second column become editable. After my edit, i push button "Save"...and nothing is happened. Can't understand such a simple thing. Why it's not working? Sorry for mistakes, i'm here for the 1st time

<asp:Content ID="ContentMain" ContentPlaceHolderID="centerContent" runat="server">
    <asp:Panel ID="i_ListContainer" runat="server" HorizontalAlign="Center">
        <asp:ListView ID="CpzListView" runat="server" DataKeyNames="id" InsertItemPosition="FirstItem"
            OnItemCreated="i_UserLV_ItemCreated" OnItemInserted="i_UserLV_ItemInserted" OnItemEditing="i_UserLV_ItemEditing"
            OnItemInserting="i_UserLV_ItemInserting" OnItemUpdated="i_UserLV_ItemUpdated"
            OnItemUpdating="i_UserLV_ItemUpdating">
            <LayoutTemplate>
                <table cellpadding="0" cellspacing="0" class="mGrid">
                    <th style="width: 150px">
                        Name
                    </th>
                    <th style="width: 150px">
                        Code
                    </th>
                    <th style="width: 50px">
                        Binding
                    </th>
                    </thead>
                    <tbody>
                        <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
                    </tbody>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:LinkButton ID="i_viewEditLB" runat="server" CommandName="Edit" Text='<%#Bind("cpzname") %>' />
                    </td>
                    <td>
                        <asp:Label ID="i_viewUserFioLabel" runat="server" Text='<%#Bind("code") %>' />
                    </td>
                    <td style="text-align: center; width: 80px">
                        <a href='<%# GetUrl(Eval("cpzname"), Eval("code"))%>'>Bindindg</a>
                    </td>
                </tr>
            </ItemTemplate>
            <InsertItemTemplate>
            </InsertItemTemplate>
            <EditItemTemplate>
                <tr>
                    <td>
                        <asp:TextBox ID="tbCpzName" runat="server" Text='<%# Bind("cpzname") %>'></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="tbCpzCode" runat="server" Text='<%# Bind("code") %>'></asp:TextBox>
                    </td>
                    <td>
                        <asp:LinkButton ID="lbSave" runat="server" CommandName="Update" Text="Save" />
                    </td>
                </tr>
            </EditItemTemplate>
        </asp:ListView>
    </asp:Panel>
</asp:Content>

Here is the c# code, hope you'll help me.

    namespace WebApplication2.MemberPages
{
        protected void Page_Load(object sender, EventArgs e)
        {
            UpdateBinding();
        }

        private void UpdateBinding()
        {
            DataTable dt = AOneConnectDB.ExecuteQuery("select id, cpzname, code from cpz");
            if (dt == null) return;

            CpzListView.DataSource = dt;
            CpzListView.DataBind();
        }

        protected void i_UserLV_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            CpzListView.EditIndex = -1;
            UpdateBinding();
        }

        private string GetValue(users key)
        {
            return CpzListView.Items.Count > 0 ? CpzListView.SelectedDataKey[key.ToString()].ToString() : string.Empty;
        }

        protected void i_UserLV_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            CpzListView.EditIndex = e.NewEditIndex;
            CpzListView.SelectedIndex = e.NewEditIndex;
            UpdateBinding();
        }

        protected void i_UserLV_ItemInserted(object sender, ListViewInsertedEventArgs e)
        {
            // must empty
        }

        private string ControlValue(string controlName, ListViewItem item)
        {
            object c = item.FindControl(controlName);
            if (c is TextBox) return (c as TextBox).Text;
            if (c is CheckBox) return (c as CheckBox).Checked ? ADataBase.TRUE : ADataBase.FALSE;
            if (c is DropDownList) return (c as DropDownList).SelectedValue;
            return null;
        }

        protected void i_UserLV_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
        {
            // must empty
        }

        protected void i_UserLV_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            if (CpzListView.SelectedDataKey != null && !string.IsNullOrEmpty(CpzListView.SelectedDataKey["id"].ToString()))
            {
                var cpzId = CpzListView.SelectedDataKey["id"];
                var cpzName = ControlValue("tbCpzName", CpzListView.EditItem);
                var cpzCode = ControlValue("tbCpzCode", CpzListView.EditItem);
                string updateCpzQuery = string.Format(@"update cpz c
                                            set c.cpzname = {0}
                                            and c.code = {1}
                                            where c.id = {2}", cpzName, cpzCode, cpzId);
                if (AOneConnectDB.ExecuteNonQuery(updateCpzQuery))
                {
                    AGUI.ShowMessage(MessageType.Success, "CPZ Modified.");
                }
            }
        }

        protected void i_UserLV_ItemCreated(object sender, ListViewItemEventArgs e)
        {
            // must empty
        }

    }
}

Related posts

Recent Viewed