PostBack only after first button click when use RegisterPostBack

Rate It (1)

Last post 12-18-2007 4:47 PM by Challenger. 9 replies.

Sort Posts:

  • PostBack only after first button click when use RegisterPostBack

    09-18-2007, 3:37 PM
    • Loading...
    • mluis
    • Joined on 09-18-2007, 6:47 PM
    • Posts 6

    Hello all,

    I am new to ASP.NET and I have a page with an UpdatePanel, inside that I have a GridView and a FormView. When I click select in GridView, I have all details in FormView so I can edit and insert new itens. The problem is I have FileUpload control inside FormView, in Edit and Insert mode and as you know, it does not work inside UpdatePanel. I need to use a button to submit my files and I would like to have a full postback when click this button but if I try to use it to trigger my UpdatePanel with PostBack I get an error because the server cannot find this control. In fact I have this control only in edit and insert mode of FormView. When I change to Edit mode, I am using the "Load" event of my button in order to register it in Scriptmanager to accept a full postback but it works "only" after a first click.

    Protected Sub UploadButton_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim upload As Object = MateriasFormView.FindControl("UploadButton")

        Dim sm As ScriptManager

        sm = CType(Master.FindControl("ScriptManager1"), ScriptManager)

        script.RegisterPostBackControl(upload)

    End Sub

     

    Anybody can explain why it works only after the first click or suggest a way to solve the problem.

    Thank you.

  • Re: PostBack only after first button click when use RegisterPostBack

    09-18-2007, 4:55 PM
    Bashar Kokash, Blog
  • Re: PostBack only after first button click when use RegisterPostBack

    09-19-2007, 9:14 AM
    • Loading...
    • mluis
    • Joined on 09-18-2007, 6:47 PM
    • Posts 6

    Thank you for your reply. I have read that post, I have a similar problem but in a different format. When the page loads, I don´t have my FileUpload control renderized because it´s inside a FormView, in edit mode. When I click in edit LinkButton in FormView, the FileUpload, submit Button and other controls are renderized so at this moment I include my submit Button as a PostBack trigger in ScriptManager but it works only after the first click. As I am trying to show, my FileUpload control and submit Button are not invisible or inside an invisible Panel, they are renderized when the FormView command change to Edit or Insert mode and only in that moment I can include the button in ScriptManager, otherwise, I get an error because the Button does not exist yet.

  • Re: PostBack only after first button click when use RegisterPostBack

    09-19-2007, 5:23 PM

    could you post your HTML/ASP.NET code.

    Bashar Kokash, Blog
  • Re: PostBack only after first button click when use RegisterPostBack

    09-20-2007, 3:06 PM
    • Loading...
    • mluis
    • Joined on 09-18-2007, 6:47 PM
    • Posts 6

    As the code is so long to post here, you can see the full code of aspx page and code behind at: http://code.hostpoa.com.

    Thank you.

  • Re: PostBack only after first button click when use RegisterPostBack

    09-23-2007, 3:33 PM

    Hi mluis

    mluis:
    you can see the full code of aspx page and code behind at: http://code.hostpoa.com.

    After I examined your code, I found that you have to set the UpdateMode property to  "Conditional", I did exactly the same like your code and worked perfect.

    Bashar Kokash, Blog
    Filed under: ,
  • Re: PostBack only after first button click when use RegisterPostBack

    09-23-2007, 8:31 PM
    • Loading...
    • mluis
    • Joined on 09-18-2007, 6:47 PM
    • Posts 6

    Hello BasharKokash,

    First of all, thank you for using your time to examine the code. Before post the problem here, I was using UpdatePanel in "Conditional" mode and I had the same result. I don´t undestand why it works well with you. Now I have my aspx page with the UpdatePanel in conditional mode as you suggest, and the problem remains. You can check it in http://simples.poaweb.com.br (It´s a copy of my web site). There is a LinkButton called "Área Restrita" in the footer menu. When you click there, you will go to login page and should use the name "member" and "%member%" as password. After that, the page is that you have examined. The first news has no image and you can test the new aspx page with UpdatePanel in conditional mode, the commands are: Editar to Edit, Excluir to Delete, Nova to Insert, Modificar to Update and Cancelar to Cancel. At first time, the FileUpload (image) does not work and after that, it works well.

     

  • Re: PostBack only after first button click when use RegisterPostBack

    09-25-2007, 4:53 PM
    Answer

    Hi mluis,

    Sorry for my previous answere was incorrect, but now I guess I fixed the problem,

    you don't have to register the button with the script manager in the load event,

    All I did is: set the updatemode to always not conditional

    add a postback trigger to the update panel and set the controlid for the trigger to formview.id (the id of your formview)

    <Triggers>

    <asp:PostBackTrigger ControlID="FormView1" />

    </Triggers>

    and it worked

    Partial Class _Default Inherits System.Web.UI.Page    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

            FormView1.PageIndex = (GridView1.PageIndex * GridView1.PageSize) + GridView1.SelectedIndex

       End Sub

       Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

          Dim arquivo As FileUpload = FormView1.FindControl("FileUpload1")

          Dim AddressTextBox As TextBox = FormView1.FindControl("AddressTextBox")

          Dim Image1 As Image = FormView1.FindControl("Image1")

          If (arquivo.HasFile = True) Then

             arquivo.SaveAs(Server.MapPath("~\Images\" & arquivo.FileName))          AddressTextBox.Text = "~\Images\" & arquivo.FileName

             Image1.ImageUrl = AddressTextBox.Text

             Image1.Visible = True

         Else

            Image1.Visible = False

            AddressTextBox.Text = ""

       End If

    End Sub

    End Class

    Bashar Kokash, Blog
  • Re: PostBack only after first button click when use RegisterPostBack

    09-26-2007, 8:09 AM
    Answer
    • Loading...
    • mluis
    • Joined on 09-18-2007, 6:47 PM
    • Posts 6

    Hi BasharKokash,

     

    Thank you very much, it worked fine. I am really impressed how you and other people use your experience in order to help people here.

  • Re: PostBack only after first button click when use RegisterPostBack

    12-18-2007, 4:47 PM
    • Loading...
    • Challenger
    • Joined on 08-07-2006, 6:05 AM
    • Posts 3

    Hi,

           I have the same problem. In my case i am not using Formview. I've placed updatepanel and file upload inside the usercontrol. First time the file upload control is not recoginizing the file. Next time it is uploading all the functions.Attached my code inside the usercontrol for your reference.

     <asp:UpdatePanel ID="patchReleaseUpdatePanel" ChildrenAsTriggers="false" runat="server" UpdateMode="Conditional">

    <ContentTemplate>

    <tr>

    <td>

    <asp:Label ID="lblSelectFile" runat="server" CssClass="SCREEN_BOLD_LABEL_SMALL"></asp:Label>

    </td>

    </tr>

    <tr>

    <td>

    <asp:FileUpload ID="fileUpload" runat="server" CssClass="TEXTBOX_LARGE" />

    <asp:TextBox ID="txtFileName" runat="server" CssClass="TEXTBOX_EXTRA_LARGE" Visible="false"></asp:TextBox>

    </td>

    </tr>

    <tr>

    <td class="EmptyRowSpace">

    </td>

    </tr>

    <tr>

    <td>

    <table cellpadding="0" cellspacing="0" width="100%">

    <tr>

    <td width="18%">

    <asp:Label ID="lblPatchReleaseInformation" runat="server" CssClass="PAGE_SUBHEADING"></asp:Label>

    </td>

    <td>

    <hr />

    </td>

    <td width="18%">

    &nbsp;

    </td>

    </tr>

    </table>

    </td>

    </tr>

    <tr>

    <td class="EmptyRowSpace">

    </td>

    </tr>

    <tr>

    <td>

    <asp:Label ID="lblDescription" runat="server" CssClass="SCREEN_BOLD_LABEL_SMALL"></asp:Label>

    </td>

    </tr>

    <tr>

    <td>

    <asp:TextBox ID="txtPatchReleaseDescription" runat="server" TextMode="MultiLine"

    CssClass="TEXTBOX_MULTILINE"></asp:TextBox>

    <asp:RegularExpressionValidator ID="regUpdateDescription" runat="server" CssClass="ScreenLabelValidation"

    ControlToValidate="txtPatchReleaseDescription">

    </asp:RegularExpressionValidator>

    </td>

    </tr>

    <tr>

    <td class="EmptyRowSpace">

    </td>

    </tr>

    <tr>

    <td>

    <asp:Label ID="lblCustomCommand" runat="server" CssClass="SCREEN_BOLD_LABEL_SMALL"></asp:Label>

    </td>

    </tr>

    <tr>

    <td>

    <asp:TextBox ID="txtCustomCommand" runat="server" CssClass="TEXTBOX"></asp:TextBox>

    </td>

    </tr>

    <tr>

    <td class="EmptyRowSpace">

    </td>

    </tr>

    <tr>

    <td>

    <asp:Label ID="lblInstallArgs" runat="server" CssClass="SCREEN_BOLD_LABEL_SMALL"></asp:Label>

    </td>

    </tr>

    <tr>

    <td>

    <asp:TextBox ID="txtInstallArgs" runat="server" CssClass="TEXTBOX"></asp:TextBox>

    </td>

    </tr>

    <tr>

    <td class="EmptyRowSpace">

    </td>

    </tr>

    <tr>

    <td>

    <asp:Label ID="lblPlatformTier" runat="server" CssClass="SCREEN_BOLD_LABEL_SMALL"></asp:Label>

    </td>

    <td>

    </td>

    </tr>

    <tr>

    <td>

    <asp:DropDownList ID="drpPlatformTier" runat="server" CssClass="DROPDOWNLIST">

    </asp:DropDownList>

    <asp:RequiredFieldValidator ID="rfvPlatformTier" ControlToValidate="drpPlatformTier"

    InitialValue="-1" runat="server">

    <asp:Image ID="platformTierValidatorImage" runat="server" />

    </asp:RequiredFieldValidator>

    </td>

    </tr>

    <tr>

    <td>

    <asp:Label ID="lblErrorMessage" runat="server" CssClass="ScreenLabelValidation"></asp:Label>

    </td>

    </tr>

    <tr>

    <td>

    <tr>

    <td align="right">

    <asp:Panel ID="savePanel" runat="server" Visible="true" CssClass="PANEL_BUTTON">

    <table cellpadding="0" cellspacing="0" border="0" width="90%">

    <tr>

    <td align="right">

    <asp:Button ID="btnSave" runat="server" CssClass="BUTTON_SMALL" OnClick="ButtonControls_SaveOptionClicked"

    CausesValidation="true" />

    <asp:Button ID="btnCancel" runat="server" CssClass="BUTTON_SMALL" OnClick="ButtonControls_CancelOptionClicked"

    CausesValidation="false" />

    </td>

    </tr>

    </table>

    </asp:Panel>

    </td>

    </tr>

    </td>

    </tr>

    </ContentTemplate>

    <Triggers>

    <asp:PostBackTrigger ControlID="btnSave" />

    </Triggers>

    </asp:UpdatePanel>

Page 1 of 1 (10 items)