0

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

编辑:
整个代码隐藏:

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"]);
        }            
    }

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

4

1 回答 1

0

因为执行顺序。尝试在Page_Load事件中设置它:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack &&
        !string.IsNullOrEmpty(Request.Form[NewRestaurantBtn.ClientID]))
    {
        ListView.InsertItemPosition = InsertItemPosition.LastItem;
    }
}
于 2013-05-12T19:59:20.407 回答