Morteza Jangjoo
Morteza Jangjoo

Reputation: 1790

How to store the values of ASPxTextBoxes inside DevExpress GridView DataItemTemplate during paging

I have a gridview which I had attached there are columns which allow user to input data into textbox.

when user input/enter data in any of the textbox and then if he change the page index and then when he come back to old page, the data inputed in the textbox has been lost.

so is there any way so that I can maintain the value/data inputed in the textbox eventhogh user can visit the different page by changing page index of the grid view?

<dx:ASPxGridView EnablePagingGestures="True" SettingsPager-PageSize="5" ID="grdSelectedItem" runat="server" OnPageIndexChanged="grdSelectedItem_PageIndexChanged" AutoGenerateColumns="False" KeyFieldName="ProductID">
    <Columns>
        <dx:GridViewDataTextColumn FieldName="Title" Caption="Product Tittle" Settings-AllowAutoFilter="False">
        </dx:GridViewDataTextColumn>

        <dx:GridViewDataTextColumn FieldName="CountItem" Caption="Count Items" Settings-AllowAutoFilter="False" Width="75px">
            <DataItemTemplate>
                <asp:TextBox  runat="server" ID="txtCountItem" Text='<%# Eval("CountItem") %>'></asp:TextBox>
            </DataItemTemplate>
        </dx:GridViewDataTextColumn>                
    </Columns>
</dx:ASPxGridView>

Upvotes: 2

Views: 2225

Answers (1)

Tetsuya Yamamoto
Tetsuya Yamamoto

Reputation: 24957

This issue occurs because paging (also with sorting and grouping) updates ASPxGridView data with a callback sent to server, and all editors inside DataItemTemplate lose their value afterwards due to ASP.NET page life cycle. Hence, you need another way to store the textbox value inside item template, such like a hidden field or Session state variable by handling Init handler of corresponding control (also using CustomUnboundColumnData handler if the column with DataItemTemplate is an unbound column).

If you want to use hidden field (I think this is the easiest way), put a hidden field like this:

<dx:ASPxHiddenField ID="hiddenField" runat="server" ClientInstanceName="hf"></dx:ASPxHiddenField>

Then, update the markup & code behind so that textbox inside DataItemTemplate has event handlers in both server and client-side by handling Init and ValueChanged (or TextChanged) events, which stores textbox value and set its value after callback:

Markup

<script>
function grdSelectedItem_Init(s, e) {
    if (hf.Contains(s.cpHFKey))
        s.SetValue(hf.Get(s.cpHFKey));
}

function grdSelectedItem_ValueChanged(s, e) {
    hf.Set(s.cpHFKey, s.GetValue());
}
</script>

<!-- other stuff -->

<dx:ASPxGridView EnablePagingGestures="True" SettingsPager-PageSize="5" ID="grdSelectedItem" runat="server" OnPageIndexChanged="grdSelectedItem_PageIndexChanged" AutoGenerateColumns="False" KeyFieldName="ProductID">
    <Columns>
        <dx:GridViewDataTextColumn FieldName="Title" Caption="Product Tittle" Settings-AllowAutoFilter="False">
        </dx:GridViewDataTextColumn>

        <dx:GridViewDataTextColumn FieldName="CountItem" Caption="Count Items" Settings-AllowAutoFilter="False" Width="75px">
            <DataItemTemplate>
                <dx:ASPxTextBox ID="txtCountItem" runat="server" Text='<%# Eval("CountItem") %>' OnInit="txtCountItem_Init">
                    <ClientSideEvents Init="grdSelectedItem_Init" ValueChanged="grdSelectedItem_ValueChanged" />
                </dx:ASPxTextBox>
            </DataItemTemplate>
        </dx:GridViewDataTextColumn>                
    </Columns>
</dx:ASPxGridView>

Code behind

protected void txtCountItem_Init(object sender, EventArgs e) 
{
    ASPxTextBox textBox = sender as ASPxTextBox;
    GridViewDataItemTemplateContainer container = textBox.NamingContainer as GridViewDataItemTemplateContainer;

    textBox.JSProperties["cpHFKey"] = String.Format("{0}_txtCountItem_{1}", container.Grid.ClientID, container.KeyValue);

    textBox.ClientIDMode = ClientIDMode.Static;

    // other stuff
}

Related topics:

Upvotes: 2

Related Questions