列表视图更改是否适用于第二次回发后?

时间:2013-05-12 17:19:32

标签: asp.net listview postback lifecycle

我有一个列表视图和一个按钮列表视图,按钮单击我想添加在InsertItemTemplate中定义的“插入”行。问题是,当我点击按钮时,这一行被添加(我知道这是因为当这行真正显示后做任何回发时),但没有显示/渲染。所以问题是:为什么这个改变不适用于第一次回发 - 按钮点击?这是我的代码:

编辑:
整个Codebehind:

protected void Page_Load(object sender, EventArgs e)
    {                        
        if (!IsPostBack)
        {


            ListItem ByName = new ListItem("By name", "Name");
            ListItem ByPhone = new ListItem("By phone", "Phone");
            ListItem ByEmail = new ListItem("By email", "Email");

            FilterTypeDDL.Items.Add(ByName);
            FilterTypeDDL.Items.Add(ByPhone);
            FilterTypeDDL.Items.Add(ByEmail);
            FilterTypeDDL.DataBind();
        }
    }

    //protected void ListView_ItemCommand(object sender, ListViewCommandEventArgs e)
    //{            
        //switch (e.CommandName)
        //{
            //case "EditItem":                    
            //    break;

            //case "InsertItem":
            //    if (Page.IsValid)
            //    {
            //        string NameTxt = ((TextBox)(ListView.InsertItem.FindControl("NameTextBox"))).Text.Trim();
            //        string PhoneTxt = ((TextBox)(ListView.InsertItem.FindControl("PhoneTextBox"))).Text.Trim();
            //        string EmailTxt = ((TextBox)(ListView.InsertItem.FindControl("EmailTextBox"))).Text.Trim();
            //        DAORestaurant.InsertRestaurant(NameTxt, PhoneTxt, EmailTxt);


            //        ListView.InsertItemPosition = InsertItemPosition.None;
            //        ListView.DataSource = DAORestaurant.GetRestaurants();
            //        ListView.DataBind();
            //        break;
            //    }
            //    break;

            //case "CancelCreation":
            //    ListView.InsertItemPosition = InsertItemPosition.None;
            //    ListView.DataSource = DAORestaurant.GetRestaurants();
            //    ListView.DataBind();
            //    break;

            //case "Articles":
            //    Session["Restaurant"] = e.CommandArgument.ToString();
            //    Control ArticlesCtrl = LoadControl("~/Controls/Article.ascx");
            //    ListViewItem Item = (ListViewItem)e.Item;
            //    Item.FindControl("CtrlPlaceHolder").Controls.Add(ArticlesCtrl);
        //}

    //}

    protected void closeButton_Click(object sender, EventArgs e)
    {

    }

    protected void newArticleButton_Click(object sender, EventArgs e)
    {

    }

    protected void NewRestaurantBtn_Click(object sender, EventArgs e)
    {
        ListView.InsertItemPosition = InsertItemPosition.LastItem;
        //SetDataSource();
        //ListView.DataBind();        
    }

    protected void ValidateName(object source, ServerValidateEventArgs args)
    {
        string NameTxt = ((TextBox)(ListView.InsertItem.FindControl("NameTextBox"))).Text.Trim();
        args.IsValid = (NameTxt.Length > 2 && NameTxt.Length < 51);
    }

    protected void ValidateUniqueness(object source, ServerValidateEventArgs args)
    {
        string NameTxt = ((TextBox)(ListView.InsertItem.FindControl("NameTextBox"))).Text.Trim();
        args.IsValid = DAORestaurant.IsUnique(NameTxt);
    }

    protected void ValidatePhone(object source, ServerValidateEventArgs args)
    {
        string PhoneTxt = ((TextBox)(ListView.InsertItem.FindControl("PhoneTextBox"))).Text.Trim();
        Regex regex = new Regex(@"^\d{3}\s\d{3}\s\d{3}$");
        args.IsValid = regex.IsMatch(PhoneTxt);
    }

    protected void ValidateEmail(object source, ServerValidateEventArgs args)
    {
        string EmailTxt = ((TextBox)(ListView.InsertItem.FindControl("EmailTextBox"))).Text.Trim();
        Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
        args.IsValid = regex.IsMatch(EmailTxt);
    }

    protected void ShowAllBtn_Click(object sender, EventArgs e)
    {            
        Session["ALL"] = true;
        ListView.DataSource = DAORestaurant.GetRestaurants();
        ListView.DataBind();
    }

    protected void FilterBtn_Click(object sender, EventArgs e)
    {
        string filterType = FilterTypeDDL.SelectedValue;
        string substring = StringTB.Text.Trim().ToUpper();
        Session["ALL"] = false;
        Session["FilterType"] = filterType;
        Session["Substring"] = substring;


        ListView.DataSource = DAORestaurant.GetRestaurants(substring, filterType);
        ListView.DataBind();
    }

    protected void ListView_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        ListView.EditIndex = e.NewEditIndex;

        //SetDataSource();
        //ListView.DataBind();
    }

    protected void ListView_ItemInserting(object sender, ListViewInsertEventArgs e)
    {

    }

    protected void ListView_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        if (e.CancelMode == ListViewCancelMode.CancelingInsert)
        {
            ListView.InsertItemPosition = InsertItemPosition.None;
        }
        else
        {
            ListView.EditIndex = -1;                
        }

        //SetDataSource();
        //ListView.DataBind();
    }

    private void SetDataSource()
    {
        if ((bool)Session["ALL"])
        {
            ListView.DataSource = DAORestaurant.GetRestaurants();
        }
        else
        {
            ListView.DataSource = DAORestaurant.GetRestaurants((string)Session["Substring"], (string)Session["FilterType"]);
        }            
    }

以前用过的代码注释了,我已经切换到你现在可以看到的东西,但问题仍然存在。只有当我在每个事件中取消注释这两个注释行时,更改才会立即应用,但我知道我不能多次使用这样的方法,甚至不应该在那里。

1 个答案:

答案 0 :(得分:0)

由于执行的顺序。尝试在Page_Load事件中进行设置:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack &&
        !string.IsNullOrEmpty(Request.Form[NewRestaurantBtn.ClientID]))
    {
        ListView.InsertItemPosition = InsertItemPosition.LastItem;
    }
}