当gridview再次获得数据绑定时,如何在gridview的模板字段中保留在TextBox中输入的值?

时间:2014-06-28 16:10:55

标签: c# asp.net gridview

TextBox再次获得gridview's templatefield时,如何保留在gridview内的databound中输入的值?
谁能告诉我为什么? 也许可以帮我找到解决方案? 例如,我单击A按钮,然后单击事件Gridview1.DataBind(),但我们丢失了TextBox上的所有值。

    <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


让我解释一下,当用户发送新评论时,我将向所有其他用户广播消息(带信号器)以更新他们的网格视图(当客户端获得广播消息button1_Click时,将由jquery调用)。我的问题是,如果其他用户在他们的文本框中键入新评论将丢失他们键入的评论。

1 个答案:

答案 0 :(得分:1)

要在SessionState对象中存储字符串数组,然后将其读回,请参阅以下示例代码段:

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

SessionState读回来:

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

希望这会有所帮助。 RGDS,