locked
MutiltView, View1 is not "active" when view is loaded yet gridview data is rendered. RRS feed

  • Question

  • User-1196696827 posted

    I have a fairly complicated .net page with C# code behind that I am having issue with multiview, views. being active after rendered. Here's the basics of the page setup and code behind. When the grid is loaded on view 2 and you hover the mouse over a row the row color changes as it should. But when you click a row nothing happens until I click a dummy btn on the view and a postback occurs. Then the row can be selected. Seems to me the view should be active.

    Is there a way to force a postback on multiviews.ActiveViewIndex = X? Or maybe auto click a dummy button?

    FYI all functions and methods work fine except this.

    <div>
    <asp:MultiView ID="mutliview" runat="server">
    <asp:View ID="mainview1" runat="server">
    <asp:GridView ID="MainGrid" runat="server">
    </asp:GridView>

    --When a row is selected in maingrid its related data is displayed in the subgrid (works fine)--

    <asp:Button ID="viewsubgriddatabtn" runat="server" OnClick="viewsubgriddatabtn_clk" />
    <asp:GridView ID="subgrid" runat="server">
    </asp:GridView>
    </asp:View>
    </div>

    --you then select a row in the subgrid and click viewsubgriddatabtn which calls method in code behind viewsubgriddatabtn_clk

    protected void viewsubgriddatabtn_clk(object sender, EventArgs e)
    {
    multiviews.ActiveViewIndex = 2; --Calls active view 2---
    }
    <asp:View ID="View2" runat="server">
    <asp:Button ID="dunnmybtn" runat="server" />
    <asp:GridView ID="View2grid" runat="server">
    </asp:GridView>
    </asp:View>

    -- view2 grid renders correctly but is not active until after a post back (Click the dummybtn) placed in view2. Then the code below works fine.

    Code behind to select and highlight rows.

    protected void grid_OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(grid, "Select$" + e.Row.RowIndex);

    }

    }
    protected void grid_SelectedIndexChanged(object sender, EventArgs e)
    {
    grid.SelectedRowStyle.CssClass = "selectedrow";
    }

    Please let me know if I need to explain more. SO never responded to me on this one so I thought I would try here.
    Thanks.




    Saturday, August 13, 2016 9:56 PM

All replies

  • User-707554951 posted

    Hi Tomaric,

    According to your code and description, the View2gird and the dummybtn are in the View2, if the GridView(View2grid) render correctly or the button (dummybtn) is clickable, it means the view2’s state is active.

    Based on your code, I make the sample below. You could refer to it.

    <asp:MultiView ID="mutliview" runat="server">
                    <asp:View ID="mainview1" runat="server">
                        <h3>This is View 1</h3>
                        <asp:GridView ID="MainGrid" runat="server" OnSelectedIndexChanged="MainGrid_SelectedIndexChanged" AutoGenerateSelectButton="true">
                        </asp:GridView>
                        <br />
                        <asp:Button ID="viewsubgriddatabtn" runat="server" OnClick="viewsubgriddatabtn_clk"
                            Text="viewsubgriddatabtn" />
                        <br />
                        <asp:GridView ID="subgrid" runat="server" OnSelectedIndexChanged="grid_SelectedIndexChanged" AutoGenerateSelectButton="true">
                        </asp:GridView>
                    </asp:View>
                    <asp:View ID="View2" runat="server">
                        <h3>This is View 2</h3>
                        <asp:Button ID="dunnmybtn" runat="server" Text="dunnmybtn" />
                        <asp:GridView ID="View2grid" runat="server" OnRowDataBound="View2grid_OnRowDataBound">
                        </asp:GridView>
                    </asp:View>
                </asp:MultiView>
    

    CodeBehind:

    using System.Data;
    using System.Drawing;
    protected void Page_Load(object sender, EventArgs e)
            {
                mutliview.ActiveViewIndex = 0;
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Id", typeof(int)),
                        new DataColumn("Name", typeof(string)) });
                dt.Rows.Add(1, "lisa");
                dt.Rows.Add(2, "bana");
                dt.Rows.Add(3, "jular");
                MainGrid.DataSource = dt;
                MainGrid.DataBind();
                mutliview.ActiveViewIndex = 0;
            }
    
            protected void viewsubgriddatabtn_clk(object sender, EventArgs e)
            {
                string id = subgrid.SelectedRow.Cells[1].Text;
                string name = subgrid.SelectedRow.Cells[2].Text;
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Id", typeof(int)),
                        new DataColumn("Name", typeof(string)) });
    
                dt.Rows.Add(id, name);
                View2grid.DataSource = dt;
                View2grid.DataBind();
                mutliview.ActiveViewIndex = 1;        
            }
    
            protected void grid_SelectedIndexChanged(object sender, EventArgs e)
            {
                subgrid.SelectedRowStyle.BackColor = Color.Gray;
            }
            protected void View2grid_OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.BackColor = Color.LightPink;
    
                }
            }
    
            protected void MainGrid_SelectedIndexChanged(object sender, EventArgs e)
            {
                string id = MainGrid.SelectedRow.Cells[1].Text;
                string name = MainGrid.SelectedRow.Cells[2].Text;           
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Id", typeof(int)),
                        new DataColumn("Name", typeof(string)) });
    
                dt.Rows.Add(id, name);
                subgrid.DataSource = dt;
                subgrid.DataBind();
            }
    

    Screenshots as below:

    Hope this can help you. If you have any question and confusion about the problem. Please don't hesitate to let me know.

    Best regards
    Cathy

     

    Tuesday, August 16, 2016 1:45 AM
  • User-1196696827 posted

    Cathy, Thank you very much for your detailed response much appreciated.

    My views are working exactly the way your example is working. Once my view two grid is rendered I can not select the row with a button click. That's where the bug is. On mouse over the row in view two changes color (as it should) but clicking the row does not send the click event to the code. Clicking the row in view two should execute a stored procedure to display details of the selected row in view two. No event is fired.

    I have to click the dummy button (which initiates a post back and has no code behind to execute) Once that dummy button is clicked the view two selected row now is clickable, event fires and executes the code behind.

    That make sense?

    Thanks again

    Tuesday, August 16, 2016 1:17 PM
  • User-707554951 posted

    Hi Tomaric the...,

    I have read your description carefully, however, I not quite clear about you problem, would you please provide us with relevant code or display us with a dynamic image you want to achieve, so that we could restore your problem and find a solution for your problem timely.

    Best regards
    Cathy

    Wednesday, August 24, 2016 8:35 AM