2010-04-17 83 views
1

輸入到文本框中的數據沒有在數據庫中得到更新。在調試模式中,我看到ItemUpdating事件中的text1和text2包含與調用ItemUpdating之前相同的值。listview和datalist沒有更新,插入

這裏是我的ListView控件:

<asp:ListView ID="ListView1" runat="server" 
onitemediting="ListView1_ItemEditing" 
onitemupdating="ListView1_ItemUpdating" 
oniteminserting="ListView1_ItemInserting"> 

//LayoutTemplate removed 

<ItemTemplate> 
     <asp:Label ID="Label2" runat="server" Text='<%#Eval("id")%>'></asp:Label> 
     <asp:Label ID="Label3" runat="server" Text='<%#Eval("text1")%>'></asp:Label> 

     <asp:LinkButton ID="LinkButton2" CommandName="Edit" runat="server">Edit</asp:LinkButton> 
     <asp:LinkButton ID="LinkButton4" CommandName="Delete" runat="server">Delete</asp:LinkButton> 
</ItemTemplate> 
<EditItemTemplate> 
    <asp:Label ID="Label1" runat="server" Text='<%#Eval("id")%>'></asp:Label> 
    <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("text1")%>' TextMode="MultiLine" /> 
    <asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("text2")%>' Height="100" TextMode="MultiLine" /> 

    <asp:LinkButton ID="LinkButton1" CommandName="Update" CommandArgument='<%# Eval("id")%>' runat="server">Update</asp:LinkButton> 
    <asp:LinkButton ID="LinkButton5" CommandName="Cancel" runat="server">Cancel</asp:LinkButton> 
</EditItemTemplate> 
<InsertItemTemplate> 
    <asp:TextBox ID="TextBox3" runat="server" TextMode="MultiLine" Height="100"></asp:TextBox> 
    <asp:TextBox ID="TextBox4" runat="server" Height="100" TextMode="MultiLine"></asp:TextBox> 

    <asp:LinkButton ID="LinkButton3" runat="server">Insert</asp:LinkButton> 
</InsertItemTemplate> 
</asp:ListView> 

代碼隱藏文件:

protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e) 
{ 
    ListView1.EditIndex = e.NewEditIndex; 
    BindList(); 
} 
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e) 
{ 
    ListViewItem myItem = ListView1.Items[ListView1.EditIndex]; 

    Label id = (Label)myItem.FindControl("Label1"); 
    int dbid = int.Parse(id.Text); 

    TextBox text1 = (TextBox)myItem.FindControl("Textbox1"); 
    TextBox text2 = (TextBox)myItem.FindControl("Textbox2"); 

    //tried to work withNewValues below, but they don't work, "null reference" error is being thrown 
    //text1.Text = e.NewValues["text1"].ToString(); 
    //text2.Text = e.NewValues["text2"].ToString(); 

    //odbc connection routine removed. 
    //i know that there should be odbc parameteres: 
    comm = new OdbcCommand("UPDATE table SET text1 = '" + text1.Text + "', text2 = '" + text2.Text + "' WHERE id = '" + dbid + "'", connection); 

    comm.ExecuteNonQuery(); 
    conn.Close(); 

    ListView1.EditIndex = -1; 
    //here I databind ListView1 
} 

這有什麼錯在ItemUpdating事件text1.Text,text2.Text的更新?我應該使用e.NewValues屬性嗎?如果是這樣,如何使用它?

在此先感謝您的幫助。

+0

只是注意到我不能用DataList做同樣的操作。似乎我做了一些完全錯誤的事情。請幫忙。 – 2010-04-17 18:19:44

回答

0

Pffff。浪費了2天時間。剛發現我忘了用在Page_Load()中:

if (!IsPostBack) 
    { 
     ListView1.DataSource = ds; 
     ListView1.DataBind(); 
    }