使用listview插入时保持先前的值

时间:2012-08-23 18:54:42

标签: c# asp.net listview

目前我有一个gridview,当我点击select时,它会在listview中填充值。 我在使用selectedindexchanged事件后面的代码中处理此问题。我甚至在insertitem模板中填充了一个新条目的文本框。

protected void GridView9_SelectedIndexChanged(object sender, EventArgs e)
{
// this handles the transmittal costs
SqlDataSource29.SelectParameters.Clear();
SqlDataSource29.SelectParameters.Add("tc_t_id", App_id);        
SqlDataSource29.InsertParameters.Clear();
ListView1.Visible = true;
ListView1.DataBind();
((TextBox)ListView1.InsertItem.FindControl("tc_t_idTextBox")).Text = App_id;

然而,当我进行插入时会出现问题。我失去了我放入listviews insertitem texbox tc_t_idTextBox的价值。实际上,当我编辑和删除时,我也失去了价值。

必须有一种方法可以在插入之间保持该值。

<InsertItemTemplate>
    <tr style="">
        <td>
            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                Text="Insert" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                Text="Clear" />
        </td>
        <td>
            <asp:TextBox ID="tc_dateTextBox" runat="server" Text='<%# Bind("tc_date") %>' />
        </td>
        <td>
            <asp:TextBox ID="tc_costTextBox" runat="server" Text='<%# Bind("tc_cost") %>' />
        </td>
        <td>
            <asp:TextBox ID="tc_typeTextBox" runat="server" Text='<%# Bind("tc_type") %>' />
        </td>
        <td>
            <asp:TextBox ID="tc_commentTextBox" runat="server" Text='<%# Bind("tc_comment") %>' />
        </td>
        <td>
            &nbsp;</td>
        <td>
            <asp:TextBox ID="tc_t_idTextBox" runat="server" 
                Text='<%# Bind("tc_t_id") %>' Enabled="false" Width="15"  />
        </td>
    </tr>
</InsertItemTemplate>

1 个答案:

答案 0 :(得分:0)

通过在每个PostBack上使用DataBind(),每次用户执行和操作时都会重新加载ListView。在您网页的Page_Load中,请改为:

SqlDataSource29.SelectParameters.Clear();
SqlDataSource29.SelectParameters.Add("tc_t_id", App_id);        
SqlDataSource29.InsertParameters.Clear();
ListView1.Visible = true;
if(!IsPostBack)
{
    ListView1.DataBind();
}
((TextBox)ListView1.InsertItem.FindControl("tc_t_idTextBox")).Text = App_id;