How to add View to Multiview dynamically in asp.net

251 Views Asked by At

I have placed MultiView in UpdatePanel and added Views dynamically, each has GridView control. I'm able to display only the View which is set at first time. I have added static button for "Next" & "Back". An error "ActiveViewIndex is being set to '0'. It must be smaller than the current number of View controls '0'. For dynamically added views, make sure they are added before or in Page_PreInit event.Parameter name: value" occurs, when I click any button including these two.

Code-behind:

protected void MultiView1_Load(object sender, EventArgs e)
    {
        try
        {
            if (DropDownList4.SelectedIndex > 0)
            {
                int i = 1;
                SqlCommand cmd = new SqlCommand("select DISTINCT(schedule) from tender where project = '" + DropDownList1.SelectedItem.ToString() + "'", agr);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string schedule = dr[0].ToString().Trim();
                    GridView Gridview1 = new GridView();
                    Gridview1.ID = "Gridview" + i.ToString();
                    Gridview1.RowDataBound +=new GridViewRowEventHandler(Gridview1_RowDataBound);
                    Gridview1.EnableViewState = true;
                    Gridview1.HeaderStyle.BackColor = System.Drawing.Color.Silver;
                    Gridview1.HeaderStyle.ForeColor = System.Drawing.Color.White;
                    Gridview1.HeaderStyle.Font.Bold = true;
                    Gridview1.ForeColor = System.Drawing.Color.Gray;
                    Gridview1.AlternatingRowStyle.BackColor = System.Drawing.Color.White;
                    Gridview1.GridLines = GridLines.None;
                    Gridview1.RowStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#EFF3FB");
                    Gridview1.HorizontalAlign = HorizontalAlign.Center;
                    Gridview1.Width = 900;
                    DataTable dt = new DataTable();
                    dt.Columns.Add("SN", typeof(string));
                    dt.Columns.Add("SCHEDULE", typeof(string));
                    dt.Columns.Add("MATERIAL", typeof(string));                    
                    dt.Columns.Add("UNIT", typeof(string));
                    dt.Columns.Add("PREVIOUS BOQ QTY", typeof(string));
                    dt.Columns.Add("CURRENT QTY", typeof(string));
                    int j = 1;
                    SqlCommand cmd1 = new SqlCommand("select material from tender where project = '" + DropDownList1.SelectedItem.ToString() + "' AND schedule = '" + schedule + "' order by schedulesubsn ", agr);
                    SqlDataReader dr1 = cmd1.ExecuteReader();
                    while (dr1.Read())
                    {
                        DataRow row = dt.NewRow();
                        string mat = dr1[0].ToString().Trim();
                        row["SN"] = j;
                        row["SCHEDULE"] = schedule;
                        row["MATERIAL"] = mat;
                        row["UNIT"] = Calfunc.getunit(mat);
                        row["PREVIOUS BOQ QTY"] = Calfunc.GetLocationBOQ(DropDownList1.SelectedItem.ToString(), DropDownList2.SelectedItem.ToString(), DropDownList3.SelectedItem.ToString(), DropDownList4.SelectedItem.ToString(), schedule, mat);
                        dt.Rows.Add(row);
                        j++;
                    }
                    dr1.Dispose();                 
                    View view = new View();
                    view.ID = schedule;                    
                    view.Controls.Add(Gridview1);
                    MultiView1.Views.Add(view);
                    Gridview1.DataSource = dt;
                    Gridview1.DataBind();
                    i++;                    
                }
                dr.Dispose();
                MultiView1.ActiveViewIndex = i-3;
                
            }
        }
        catch (Exception ex)
        { 
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (MultiView1.ActiveViewIndex > 0)
        {
            MultiView1.ActiveViewIndex = MultiView1.ActiveViewIndex - 1;                
        }
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        if (MultiView1.Views.Count-1 > MultiView1.ActiveViewIndex)
        {
            TextBox2.Text = (MultiView1.ActiveViewIndex + 1).ToString();
        }
    }
    protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox TextBox101 = new TextBox();
            TextBox101.ID = "TextBox_C_QTY";
            TextBox101.Width = 70;
            TextBox101.Text = (e.Row.DataItem as DataRowView).Row["CURRENT QTY"].ToString();
            e.Row.Cells[5].Controls.Add(TextBox101);
        }
    }

aspx:

<asp:UpdatePanel ID="UpdatePanel4" runat="server">
    <ContentTemplate> 
    <div  style="overflow-x:auto;">
        <asp:MultiView ID="MultiView1" runat="server" onload="MultiView1_Load">
           
        </asp:MultiView>        
    </div>
               </ContentTemplate>
    </asp:UpdatePanel>

I've searched for the solution but didn't find any satisfactory or workable (for me).

0

There are 0 best solutions below