locked
Gridview footer dropdownlist will not reload after insert RRS feed

  • Question

  • User984760449 posted

    I have the following:
    1 - Gridview with a footer used to insert data.
    2 - The footer has a dropdownlist that is loaded via a stored procedure.
    3 - There is also a Insert button in the footer that call a method.
    4 - The Gridview is configured to use a datasource control.

    Both the Gridview and the dropdownlist in the footer load correctly when the page loads. The insert data also works and the gridview refreshes with the new entry.
    However, after a record is inserted using the SQLdatasource INSERT command, the Gridview will add the record to the list, but the dropdown list in the footer is blank and never reloads.  Using the Visual Studio debugger indicates a NULL value. I tried several suggestions on the internet with no luck. Here is my code:
    Gridview:
    <FooterTemplate>
    <asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="btnInsert_Click" />
    </FooterTemplate>
    <FooterTemplate>
    <asp:DropDownList ID="ddlSelectPlayer" runat="server"
    DataTextField="JerseyNo" DataValueField="PlayerId" AutoPostBack="True">
    </asp:DropDownList>
    </FooterTemplate>
    SQLDatasource control:
    InsertCommand="INSERT INTO [tblStats] ([Made2Pt], [Missed2pt], [Made3pt], [Foul], [Missed3pt], [Steal], [Orb], [Drb], [Assist], [TurnOver], [Block], [GameId], [CoachId], [PlayerId], [Scorer]) VALUES (@Made2Pt, @Missed2pt, @Made3pt, @Foul, @Missed3pt, @Steal, @Orb, @Drb, @Assist, @TurnOver, @Block, @GameId, @CoachId, @PlayerId, @Scorer)"
    SelectCommand="SELECT [StatId], [Made2Pt], [Missed2pt], [Made3pt], [Foul], [Missed3pt], [Steal], [Orb], [Drb], [Assist], [TurnOver], [Block], [GameId], [CoachId], [PlayerId], [Scorer] FROM [tblStats]"
    btnInsert_Click event:
    //Used with the SQLDataSource1 control
    protected void btnInsert_Click(object sender, EventArgs e)
    {
    SqlDataSource1.InsertParameters["PlayerId"].DefaultValue =
    ((DropDownList)StatGridview.FooterRow.FindControl("ddlSelectPlayer")).SelectedValue;
    SqlDataSource1.Insert();
    DropDownList ddlJerseyRefresh = ((DropDownList)StatGridview.FooterRow.FindControl("ddlSelectPlayer"));
    ddlJerseyRefresh.DataBind();

    also, I tested by adding a label control outside the Gridview so I know the method is working. However, I cannot get data to reload in the footer.

    Thursday, January 3, 2019 10:58 PM

Answers

  • User-1174608757 posted

    Hi TSQL,

    According to your description, I have  made a sample here. Dropdownlist has no values because you haven't bound the data of Gridview in advance. Gridview control will delete the data before It is bound. So I suggest you to bind the Gridview , then you could bind the dropdownlist. Here  is my demo,I hope it can help you.

     dropdown.aspx:

    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" ShowFooter="True" Height="200px" Width="272px">
                   
                    <Columns>
                        <asp:TemplateField>
                            <FooterTemplate>
                                <asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick ="btnInsert_Click" />
                            </FooterTemplate>                        
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <FooterTemplate>
                                <asp:DropDownList ID="ddlSelectPlayer" runat="server" DataTextField="color" DataValueField="fruit" AutoPostBack="true"></asp:DropDownList>
                            </FooterTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mssqlserver %>" 
                    SelectCommand="SELECT * FROM [db]" InsertCommand="INSERT INTO [db]([fruit],[color])VALUES(@FRUIT,@COLOR)">
                   <InsertParameters>
        <asp:Parameter Name="fruit" Type="String" />
        <asp:Parameter Name="color" Type="String" />
    </InsertParameters>
                </asp:SqlDataSource>
            </div>
        </form>
    </body>
    </html>

    dropdown.aspx.cs

     public partial class dropdown : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                    
                }
    
            }
            public void bind()
            {
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("fruit", typeof(string)));
                dt.Columns.Add(new DataColumn("color", typeof(string)));
                DataRow row1 = dt.NewRow();
                row1["fruit"] = "pine";
                row1["color"] = "white";
                DataRow row2 = dt.NewRow();
                row2["fruit"] = "orange";
                row2["color"] = "orange";
                dt.Rows.Add(row1);
                dt.Rows.Add(row2);
                DropDownList ddlJerseyRefresh = (DropDownList)GridView1.FooterRow.FindControl("ddlSelectPlayer");
                ddlJerseyRefresh.DataSource = dt;
                ddlJerseyRefresh.DataBind();
    
    
            }
    
            protected void btnInsert_Click(object sender, EventArgs e)
            {
                SqlDataSource1.InsertParameters["fruit"].DefaultValue = ((DropDownList)GridView1.FooterRow.FindControl("ddlSelectPlayer")).SelectedValue.ToString();
                SqlDataSource1.InsertParameters["color"].DefaultValue = ((DropDownList)GridView1.FooterRow.FindControl("ddlSelectPlayer")).SelectedItem.Text.ToString();           
                SqlDataSource1.Insert();
                GridView1.DataBind();// bind Gridview in advance
                bind();
            }
        }

    It shows as below:

    Best Regards

    Wei Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 7, 2019 6:47 AM
  • User984760449 posted

    It worked !  Thank you so much.  One line entry resolved the issue after many hours of frustration.
    Before:
    SqlDataSource1.Insert();
    refreshList();

    After:
    SqlDataSource1.Insert();
    StatGridview.DataBind();  \\added this line per your instructions
    refreshList();

    Again:
    The help is appreciated.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 8, 2019 8:25 PM

All replies

  • User-1716253493 posted

    There is no datasource id in the dropdown, so why there is no data displayed.

    Give data first before call databind

    You can add sqldatasourceplayer for the dropdown

    Friday, January 4, 2019 1:53 AM
  • User-1174608757 posted

    Hi TSQL,

    According to your description, I have  made a sample here. Dropdownlist has no values because you haven't bound the data of Gridview in advance. Gridview control will delete the data before It is bound. So I suggest you to bind the Gridview , then you could bind the dropdownlist. Here  is my demo,I hope it can help you.

     dropdown.aspx:

    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" ShowFooter="True" Height="200px" Width="272px">
                   
                    <Columns>
                        <asp:TemplateField>
                            <FooterTemplate>
                                <asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick ="btnInsert_Click" />
                            </FooterTemplate>                        
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <FooterTemplate>
                                <asp:DropDownList ID="ddlSelectPlayer" runat="server" DataTextField="color" DataValueField="fruit" AutoPostBack="true"></asp:DropDownList>
                            </FooterTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mssqlserver %>" 
                    SelectCommand="SELECT * FROM [db]" InsertCommand="INSERT INTO [db]([fruit],[color])VALUES(@FRUIT,@COLOR)">
                   <InsertParameters>
        <asp:Parameter Name="fruit" Type="String" />
        <asp:Parameter Name="color" Type="String" />
    </InsertParameters>
                </asp:SqlDataSource>
            </div>
        </form>
    </body>
    </html>

    dropdown.aspx.cs

     public partial class dropdown : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                    
                }
    
            }
            public void bind()
            {
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("fruit", typeof(string)));
                dt.Columns.Add(new DataColumn("color", typeof(string)));
                DataRow row1 = dt.NewRow();
                row1["fruit"] = "pine";
                row1["color"] = "white";
                DataRow row2 = dt.NewRow();
                row2["fruit"] = "orange";
                row2["color"] = "orange";
                dt.Rows.Add(row1);
                dt.Rows.Add(row2);
                DropDownList ddlJerseyRefresh = (DropDownList)GridView1.FooterRow.FindControl("ddlSelectPlayer");
                ddlJerseyRefresh.DataSource = dt;
                ddlJerseyRefresh.DataBind();
    
    
            }
    
            protected void btnInsert_Click(object sender, EventArgs e)
            {
                SqlDataSource1.InsertParameters["fruit"].DefaultValue = ((DropDownList)GridView1.FooterRow.FindControl("ddlSelectPlayer")).SelectedValue.ToString();
                SqlDataSource1.InsertParameters["color"].DefaultValue = ((DropDownList)GridView1.FooterRow.FindControl("ddlSelectPlayer")).SelectedItem.Text.ToString();           
                SqlDataSource1.Insert();
                GridView1.DataBind();// bind Gridview in advance
                bind();
            }
        }

    It shows as below:

    Best Regards

    Wei Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 7, 2019 6:47 AM
  • User984760449 posted

    It worked !  Thank you so much.  One line entry resolved the issue after many hours of frustration.
    Before:
    SqlDataSource1.Insert();
    refreshList();

    After:
    SqlDataSource1.Insert();
    StatGridview.DataBind();  \\added this line per your instructions
    refreshList();

    Again:
    The help is appreciated.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 8, 2019 8:25 PM