C# datalist未在updatecommand上获取值

C# datalist未在updatecommand上获取值,c#,asp.net,.net,visual-studio-2008,C#,Asp.net,.net,Visual Studio 2008,我有以下代码: protected void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack) { bindList(); } } protected void dlAgents_EditCommand(object source, System.Web.UI.WebControls.

我有以下代码:

       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();
}
在这段代码中,对于updatecommand,文本框的值总是为空。。例如
(TextBox)e.Item.FindControl(“txtAddress”)。Text
此文本框为空,因此无法使用正确的值进行更新


请帮助我。

如果在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;
    }

在编辑模式下使用文本框参见aspx:我很久没有使用过datalist了,我建议您不要使用内联sql语句,这对未来非常不利。