Mohammad OlfatMiri
Mohammad OlfatMiri

Reputation: 53

How do I preserve a value entered in a TextBox inside a gridview's templatefield when the gridview gets databound again?

How do I preserve a value entered in a TextBox inside a gridview's templatefield when the gridview gets databound again?
Can anyone tell me why? And maybe help me in finding a solution?
for example i Click on A button and click event is Gridview1.DataBind() but we lose all values on TextBoxes.

    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
    </Triggers>

    <ContentTemplate>
        <%--post GridView--%>
        <asp:GridView ID="posts" runat="server">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <%--Comments Gridview--%>
                        <asp:GridView ID="comments" runat="server"></asp:GridView>
                        <%--a Textbox and bUtton For sending new Comment--%>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                        <asp:Button ID="Button1" runat="server" Text="Button" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel> 


    protected void Button1_Click(object sender, EventArgs e)
{
    posts.DataSource = GetData();
    posts.DataBind();
}  

Edit1


let me explain more , when a user send a new comment, i will broadcast a message ( with signalr) to all other users to update their Gridviews ( when a client get broadcast message button1_Click, will be call by a jquery ). my problem is if other users are typing new comments on their textboxes will lose their typed comments.

Upvotes: 2

Views: 771

Answers (1)

Alexander Bell
Alexander Bell

Reputation: 7918

To store array of strings in SessionState object and then read it back refer to the following sample code snippets:

string[] _arr = {"a", "b", "c" }; // sample array - replace by 15 entries pertinent to your case
Session["arrString"] = _arr ;

Read it back from SessionState as:

string[] arrFromSession = (string[])Session["arrString"];

Hope this will help. Rgds,

Upvotes: 1

Related Questions