Problem with Repeater control in UpdatePanel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JFKJr
    New Member
    • Jul 2008
    • 126

    #1

    Problem with Repeater control in UpdatePanel

    Hello Guys,

    I have been trying to solve the following problem from past 3 days with no luck.

    This is what I am trying to do:

    I want to allow user (Professor) to choose a course material using "asp:FileUpload " control, upload into the database by clicking "asp:Button " control and allow Professor to view the files he/she uploaded using "asp:Repeat er" control.

    The procedure is working perfectly fine. But when I place the "asp:Repeat er" control in Ajax UpdatePanel, I am unable to retrieve file from "asp:FileUpload " control.

    Please help me out. Thanks a million in advance.

    1. The following is "CreateSchedule .aspx" page, using this page a professor can upload course materials.

    Code:
    <asp: Content ID="Content1"  ContentPlaceHolderID="ContentPlaceholder1" Runat="Server">
    
    <table>
    <tr>
    <td>Select File:</td>
    <td><asp:FileUpload ID="CMFileUpload" runat="server" /></td>
    </tr>
    <tr>
    <td colspan=2>
    <asp:Button ID="CMUploadButton" runtat="server" Text="Upload" onClick="CMUploadButton_Click">
    </td>
    </tr>
    <td>Course Materials Uploaded:</td>
    <td>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode=Conditional>
    <ContentTemplate>
        <asp:Repeater ID="CMRepeater" runat="server"><ItemTemplate>   
        <asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Click" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"File_UID") %>'><%#DataBinder.Eval(Container.DataItem,"File_Name2") %></asp:LinkButton>
        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/wrong-icon.gif" OnCommand="ImageButton1_Click" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"File_UID") %>' /> 
        </ItemTemplate></asp:Repeater>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="CMUploadButton" EventName="Click" />
    </Triggers>
    </asp:UpdatePanel>
    </td>
    <tr>
    </tr>
    </table>
    </asp:Content>
    2. "CreateSchedule .aspx.cs" page:

    Code:
    protected void CMUploadButton_Click(object sender, EventArgs e)
        {
            if (!CMFileUpload.HasFile) return;
            int weekNo = 1;
            byte[] binary = new byte[CMFileUpload.PostedFile.ContentLength];
            binary = CMFileUpload.FileBytes;
            SqlParameter param = null;
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=CSMS;Integrated Security=true";
            SqlCommand cmd = new SqlCommand("UploadCourseMaterials", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            param = new SqlParameter("@File_Name1", SqlDbType.NVarChar, 50,
                    ParameterDirection.Input, false, 0, 0, "",
                    DataRowVersion.Default, CMFileUpload.FileName);
            cmd.Parameters.Add(param);
            param = new SqlParameter("@File_Name2", SqlDbType.NVarChar, 50,
                    ParameterDirection.Input, false, 0, 0, "",
                    DataRowVersion.Default, CMNameTextBox.Text);
            cmd.Parameters.Add(param);
            param = new SqlParameter("@File_Content", SqlDbType.Image);
            param.Direction = ParameterDirection.Input;
            param.Value = binary;
            cmd.Parameters.Add(param);
            param = new SqlParameter("@Content_Type", SqlDbType.NVarChar, 50,
                    ParameterDirection.Input, false, 0, 0, "",
                    DataRowVersion.Default, CMFileUpload.PostedFile.ContentType);
            cmd.Parameters.Add(param);
            param = new SqlParameter("@WeekID", SqlDbType.Int, 4);
            param.Value = weekNo;
            cmd.Parameters.Add(param);
    
            conn.Open();
            cmd.ExecuteNonQuery();
            DisplayCourseMaterials();
        }
    
    private void DisplayCourseMaterials()
        {
            SqlConnection mySQLconnection = new SqlConnection();
            mySQLconnection.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=CSMS;Integrated Security=true";
    
            if (mySQLconnection.State == ConnectionState.Closed)
            {
                mySQLconnection.Open();
            }
    
            SqlCommand mySqlSelect = new SqlCommand("select * from CourseMaterials", mySQLconnection);
            mySqlSelect.CommandType = CommandType.Text;
    
            SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlSelect);
            DataSet myDataSet = new DataSet();
            mySqlAdapter.Fill(myDataSet);
    
            CMRepeater.DataSource = myDataSet;
            CMRepeater.DataBind();
    
            if (mySQLconnection.State == ConnectionState.Open)
            {
                mySQLconnection.Close();
            }
        }
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    The FileUpload control does not work in an UpdatePanel.

    Here is a list of controls that do not work within an update panel (according to the Documentation for ASP.NET Ajax Version 1.0
    Originally posted by Documentation for: ASP.NET Ajax Version 1.0
    The following ASP.NET controls are not compatible with partial-page updates, and are therefore not supported inside an UpdatePanel control:

    TreeView and Menu controls.


    FileUpload controls.

    GridView and DetailsView controls when their EnableSortingAn dPagingCallback s property is set to true.

    Login, PasswordRecover y, ChangePassword, and CreateUserWizar d controls whose contents have not been converted to editable templates.


    The Substitution control.

    Validation controls, which includes the BaseCompareVali dator, BaseValidator, CompareValidato r, CustomValidator , RangeValidator, RegularExpressi onValidator, RequiredFieldVa lidator, and ValidationSumma ry control.
    Since the FileUpload control requires a full page postback you could consider placing your FileUpload within an IFrame.

    -Frinny

    Comment

    • JFKJr
      New Member
      • Jul 2008
      • 126

      #3
      Hello Frinny,

      Thanks for the reply.

      I changed the above "CreateSchedule .aspx" page a little bit as follows:

      1. "CreateSchedule .aspx" page

      Code:
      <asp: Content ID="Content1"  ContentPlaceHolderID="ContentPlaceholder1" Runat="Server">  
      <table> 
      <tr> 
      <td>Select File:</td> 
      <td>
      <asp:ScriptManager ID="ScriptManager1" runat="server"> 
      </asp:ScriptManager> 
      <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode=Conditional> 
      <ContentTemplate> 
          <asp:FileUpload ID="CMFileUpload" runat="server" />
      </ContentTemplate> 
      </asp:UpdatePanel> 
      </td> 
      </tr> 
      <tr> 
      <td colspan=2> 
      <asp:Button ID="CMUploadButton" runtat="server" Text="Upload" onClick="CMUploadButton_Click"> 
      </td> 
      </tr> 
      </table> 
      </asp:Content>
      2. "CreateSchedule .aspx.cs" page:

      Code:
      protected void CMUploadButton_Click(object sender, EventArgs e) 
          { 
               UpdatePanel1.Update();
               if (!CMFileUpload.HasFile) return; 
          }
      And whenever I click "Upload" button, it is calling "CMUploadButton _Click" event.

      For the first time, the button is unable to retrieve file from "FileUpload " control and coming out of the event procedure (at line #4).

      In the following attempts, I am able to retrieve file from "FileUpload " control.

      Please kindly let me know how to solve this issue?

      Thanks.

      Comment

      • muthu krishnan
        New Member
        • Mar 2012
        • 1

        #4
        Hi all,

        Only the solution use AsyncFileUpload instead of asp:fileupload control

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Thank you for the reply Muthu.
          For those who are unaware of this control, it's part of the free AjaxControlTool kit Library.

          -Frinny

          Comment

          Working...