Translate

2013年5月31日 星期五

Binding TextBox Null Value

當你的資料庫欄位給予你的string→其實一開始就是null

這時後當你更新之後,你會發現總是跳出錯誤訊息,解決方法如下

note這個字串一開始初始化是null

  <asp:TextBox ID="txtinput" runat="server"  Text='<%# Bind("note") %>' ></asp:TextBox>

解法方法:只要判定為舊值為null時給予空白字串即可更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (e.OldValues["note"] == null) e.OldValues["note"] = "";
        }


參考連結:

Binding a nullable int to an TextBox

沒有留言:

張貼留言