0

I'm trying to load/reload a GridView based on the OnSelectedIndexChanged event of a DropDownList. The ddl has AutoPostBack set to true, but still the Grid won't load, unless I encapsulate it within an UpdatePanel. But once I do that, my FileUpload control stops working... What would be the best solution for this problem?

**Edit ** Relevant code:

aspx file

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <asp:UpdatePanel ID="upProva" runat="server">
                <ContentTemplate>
                <%--user control for data selection--%>
                <asp:DropDownList ID="ddlAula" runat="server" DataTextField="nmAula" DataValueField="idAula"
                    CssClass="medio" Enabled="false" AutoPostBack="true" OnSelectedIndexChanged="ddlAula_OnSelectedIndexChanged">
                </asp:DropDownList>
            </ContentTemplate>
        </asp:UpdatePanel>
        <asp:GridView ID="gvQuestoes" runat="server" AutoGenerateColumns="False" CssClass="gv"
            AllowSorting="false" DataKeyNames="idQuestao" OnRowCommand="gvQuestao_RowCommand">
            <Columns>
                <%--(...)--%>
            </Columns>
        </asp:GridView>
<asp:Button ID="btnSalvar" runat="server" ToolTip="Salvar" CssClass="botao40 salv40"
                    OnClick="btnSalvar_Click" ValidationGroup="trabalho" />
        <asp:FileUpload ID="fuAnexo" runat="server" CssClass="fileOriginal" />
    </asp:Content>

codebehind

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        btnArquivo.OnClientClick = "document.getElementById('" + fuAnexo.ClientID + "').click(); return false;";
        txtAnexo.Attributes.Add("onclick", "document.getElementById('" + fuAnexo.ClientID + "').click(); return false;");
        fuAnexo.Attributes.Add("onchange", "document.getElementById('" + txtAnexo.ClientID + "').value = this.value;");

    }
}

protected void ddlAula_OnSelectedIndexChanged(object sender, EventArgs e)
{
    gvQuestoes.DataSource = Questao.CarregarPorAula(Int32.Parse(ddlAula.SelectedValue));
    gvQuestoes.DataBind();
}

The DataSource/Databinding is correct (I know because I've added a button to the page, and used the same binding code on the Button _Click event and it works).

5
  • can you show your relevant code? Commented Aug 15, 2014 at 18:05
  • Hi add a code please. So it may be possible to show you an example. Commented Aug 15, 2014 at 18:06
  • You need to rebind the gridview. Commented Aug 15, 2014 at 18:06
  • Sure, adding relevant code now. Commented Aug 15, 2014 at 18:16
  • 1
    can you show page_load()?? Commented Aug 15, 2014 at 18:34

3 Answers 3

1

You need to rebind the gridview in OnSelectedIndexChanged event. Something like

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
  //Do your processing logic

  gridview1.DataSource = new_modified_datasource;
  gridview1.DataBind();  
}
Sign up to request clarification or add additional context in comments.

1 Comment

My code already includes that, but unfortunately it won't work unless I encapsulate the GridView inside the UpdatePanel. Have a look at the code I just posted. Thanks
0

I found a fairly simple (though not ideal in all cases) with PostBackTrigger:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <asp:UpdatePanel ID="upProva" runat="server">
                <ContentTemplate>
                <%--user control for data selection--%>
                <asp:DropDownList ID="ddlAula" runat="server" DataTextField="nmAula" DataValueField="idAula"
                    CssClass="medio" Enabled="false" AutoPostBack="true" OnSelectedIndexChanged="ddlAula_OnSelectedIndexChanged">
                </asp:DropDownList>

        <asp:GridView ID="gvQuestoes" runat="server" AutoGenerateColumns="False" CssClass="gv"
            AllowSorting="false" DataKeyNames="idQuestao" OnRowCommand="gvQuestao_RowCommand">
            <Columns>
                <%--(...)--%>
            </Columns>
        </asp:GridView>
        <asp:FileUpload ID="fuAnexo" runat="server" CssClass="fileOriginal" />
   </ContentTemplate>
    <Triggers>
    <asp:PostBackTrigger ControlID="btnSalvar" />    
    </Triggers>
        </asp:UpdatePanel>
    </asp:Content>

Comments

0

Nowhere in your code do you show how you populate ddlAula. So here is my answer: the Page_Load you've shown is abbreviated, and actually you are populating ddlAula there. Furthermore, you are doing it each time, rather than checking IsPostBack. Therefore by the time your eventhandler for ddlAula is hit, the list has been reset and the value you selected is no longer selected.

If my answer is correct, you need to add the check in Page_Load:

if (!IsPostBack)
{
    //populate ddlAula
}

1 Comment

No, it's populated somewhere else and I didn't include it because it wasn't necessary. My problem was specifically about the FileUploader control not working with asynchronous postback (when inside the UpdatePanel. Thanks anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.