2013-10-11 18 views
0

我有以下代碼:DataList控件不採取價值上的UpdateCommand

 protected void Page_Load(object sender, System.EventArgs e) 
     { 
      if (!IsPostBack) { 
       bindList(); 
      } 
     } 

    protected void dlAgents_EditCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e) 
    { 
     dlAgents.EditItemIndex = e.Item.ItemIndex; 
     bindList(); 
    } 

protected void dlAgents_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.AlternatingItem | e.Item.ItemType == ListItemType.Item) { 
     ((Label)e.Item.FindControl("lblName")).Text = e.Item.DataItem("AgentName"); 
     ((Label)e.Item.FindControl("lblAddress")).Text = e.Item.DataItem("Address"); 
     ((Label)e.Item.FindControl("lblContactNo")).Text = e.Item.DataItem("ContactNo"); 
     ((LinkButton)e.Item.FindControl("lnkLoginID")).Text = e.Item.DataItem("LoginEmailID"); 
    } 
} 

protected void dlAgents_UpdateCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e) 
{ 


    GC.ExecuteCommand("update AgentMaster set Address='" + ((TextBox)e.Item.FindControl("txtAddress")).Text + "' , ContactNo='" + ((TextBox)e.Item.FindControl("txtContact")).Text + "' where agentid='" + e.CommandArgument + "'"); 
    dlAgents.EditItemIndex = -1; 
    bindList(); 
} 

在這段代碼,用於更新命令,它總是把空值的文本框..如。 (TextBox)e.Item.FindControl("txtAddress")).Text這個文本框是空白的,這就是爲什麼不能用適當的值進行更新。

請幫幫我。

+0

看到ASPX:http://pastie.org/8394228 –

+0

我從來沒有在很長一段時間使用的數據列表,我建議你不要使用內聯的sql語句,這對於將來來說是非常糟糕的。 – Sakthivel

回答

1

如果在ItemTemplate中直接使用了這些控件,這可以很好地工作,但如果不是,那麼它就不是。據我所知,FindControl方法不是遞歸的。

試圖找到控制使用這種方法:使用文本框的編輯模式

public static Control FindControlRecursive(Control control, string id) 
    { 

     if (control == null) return null; 


     Control c = control.FindControl(id); 

     if (c == null) 
     { 
      foreach (Control child in control.Controls) 
      { 
       c = FindControlRecursive(child, id); 
       if (c != null) break; 
      } 
     } 

     return c; 
    }