tags:

views:

95

answers:

0

button inside gridview is not working when gridview is inside updatepanel

aspx code

  <asp:GridView2 ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
                            ShowHeaderWhenEmpty="True" EmptyDataText="No Uploads are found" ShowEmptyTable="True"
                            ShowFooterWhenEmpty="False" Style="margin-top: 20px" OnSelectedIndexChanged="GridView2_SelectedIndexChanged"
                            OnRowDataBound="GridView2_RowDataBound"
:
:
:
:
:
:
   <asp:TemplateField ShowHeader="False" HeaderText="Receive">
                                    <ItemTemplate>

                                        <asp:UpdatePanel ID="UpdatePanel22" runat="server">

                                        <ContentTemplate>
                                         <asp:Button ID="Button2" runat="server" CausesValidation="false" CommandName="select"
                                                    Text="Receive" />
                                                    </ContentTemplate>
                                            </asp:UpdatePanel>  
                                    </ItemTemplate>
                                </asp:TemplateField>

Receive button is a download button,it retrieves the url from the table when user clicks on it,it works fine when gridview is not inside updatepanel but error is generated when use gridview inside updatepanel

protected void GridView2_SelectedIndexChanged(object sender, EventArgs e) {

        string filename = "";
        GridViewRow row = GridView2.SelectedRow;
        Label url = (Label)row.FindControl("Label3");
        //get the Id of the selected row
        int ID = Convert.ToInt32(GridView2.DataKeys[row.RowIndex].Value);

        SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["DSN"]);
        SqlCommand comm = new SqlCommand("select FileName from image where Id=@Id", conn);
        comm.Parameters.Add("@Id", System.Data.SqlDbType.Int);
        comm.Parameters["@Id"].Value = ID;
        conn.Open();
        SqlDataReader read = comm.ExecuteReader();
        read.Read();
        object obValue = read.GetValue(0);
        //get the filename of the corresponding Id
        filename = obValue.ToString();
        conn.Close();
        Label f = (Label)row.FindControl("Label1");
        string path = url.Text.ToString();

        Response.ContentType = "application/octet-stream";
        Response.AddHeader("content-disposition", "attachment; filename=" + filename);
        FileStream sourceFile = new FileStream(path, FileMode.Open);
        long FileSize;
        FileSize = sourceFile.Length;
        byte[] getContent = new byte[(int)FileSize];
        sourceFile.Read(getContent, 0, (int)sourceFile.Length);
        sourceFile.Close();
        Response.BinaryWrite(getContent);

        GridView2.DataBind();
    }