Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用listview插入时保留先前的值_C#_Asp.net_Listview - Fatal编程技术网

C# 使用listview插入时保留先前的值

C# 使用listview插入时保留先前的值,c#,asp.net,listview,C#,Asp.net,Listview,目前我有一个gridview,当我点击select时,它会填充listview中的值。 我使用selectedindexchanged事件在代码隐藏中处理此问题。我甚至在insertitem模板中为新条目填充一个文本框 protected void GridView9_SelectedIndexChanged(object sender, EventArgs e) { // this handles the transmittal costs SqlDataSource29.SelectPara

目前我有一个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文本框tc\u t\u 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>

通过在每次回发时使用
DataBind()
,每次用户执行操作时都会重新加载ListView。在页面的
页面\u 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;

很抱歉它不在页面加载中。它位于gridview的选定索引中。受保护的void GridView9\u SelectedIndexChanged(对象发送方,事件参数e){当插入发生时,listview中的值会被添加。但我正在尝试保留以前插入的值,而不是将其设置为空。这应该没问题,因为现在代码正在回发时重新绑定数据,这是不应该发生的。我相信放入事件中的代码应该可以工作,因为当GridView9发布ba时ck,ListView将不会重新绑定。负。这不起作用。当我在!IsPostBack中包装ListView数据绑定时,出现错误。异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。当用户向此列表框添加项时,是否希望数据库更新,还是仅保留该项e数据。如果您希望保存数据,则应在页面中创建一个变量,例如每次重新填充ListView时使用的
列表
。在这种情况下,一旦事件触发,您应在开始时将新数据插入数据库,然后在插入完成后将其数据绑定。然后您将正确地拉出数据。